2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCScene.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLayer.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "2d/CCSpriteBatchNode.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "physics/CCPhysicsWorld.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::Scene()
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
: _physicsWorld(nullptr)
|
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ignoreAnchorPointForPosition = true;
|
2014-04-15 18:13:57 +08:00
|
|
|
setAnchorPoint(Vector2(0.5f, 0.5f));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::~Scene()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
CC_SAFE_DELETE(_physicsWorld);
|
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Scene::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Director * director;
|
|
|
|
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
|
|
|
this->setContentSize(director->getWinSize());
|
2012-04-19 14:35:52 +08:00
|
|
|
// success
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-10 17:38:47 +08:00
|
|
|
Scene *Scene::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Scene *ret = new Scene();
|
|
|
|
if (ret && ret->init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string Scene::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<Scene | tag = %d>", _tag);
|
|
|
|
}
|
|
|
|
|
2013-12-22 04:28:49 +08:00
|
|
|
Scene* Scene::getScene()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-05-02 07:42:35 +08:00
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
void Scene::addChild(Node* child, int zOrder, int tag)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder, tag);
|
|
|
|
addChildToPhysicsWorld(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::update(float delta)
|
|
|
|
{
|
|
|
|
Node::update(delta);
|
|
|
|
if (nullptr != _physicsWorld)
|
|
|
|
{
|
|
|
|
_physicsWorld->update(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scene *Scene::createWithPhysics()
|
|
|
|
{
|
|
|
|
Scene *ret = new Scene();
|
|
|
|
if (ret && ret->initWithPhysics())
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::initWithPhysics()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Director * director;
|
|
|
|
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
|
|
|
this->setContentSize(director->getWinSize());
|
|
|
|
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
|
|
|
|
|
|
|
this->scheduleUpdate();
|
|
|
|
// success
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::addChildToPhysicsWorld(Node* child)
|
|
|
|
{
|
|
|
|
if (_physicsWorld)
|
|
|
|
{
|
|
|
|
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
|
|
|
|
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
|
|
|
|
{
|
|
|
|
if (node->getPhysicsBody())
|
|
|
|
{
|
|
|
|
_physicsWorld->addBody(node->getPhysicsBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& children = node->getChildren();
|
|
|
|
for( const auto &n : children) {
|
|
|
|
addToPhysicsWorldFunc(n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addToPhysicsWorldFunc(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|