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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCScene.h"
|
|
|
|
#include "CCDirector.h"
|
2013-09-09 16:36:19 +08:00
|
|
|
#include "CCLayer.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCSpriteBatchNode.h"
|
|
|
|
#include "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()
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-10 17:38:47 +08:00
|
|
|
: _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
|
|
|
{
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_SAFE_DELETE(_physicsWorld);
|
2013-10-11 10:08:51 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2013-12-27 15:49:08 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-12-26 23:55:05 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:38:47 +08:00
|
|
|
Scene *Scene::createWithPhysics()
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Scene *ret = new Scene();
|
|
|
|
if (ret && ret->initWithPhysics())
|
2013-09-10 17:38:47 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2013-09-10 17:38:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2013-09-10 17:38:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::initWithPhysics()
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2013-09-10 17:38:47 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Director * director;
|
|
|
|
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
|
|
|
this->setContentSize(director->getWinSize());
|
2013-11-08 16:17:56 +08:00
|
|
|
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
this->scheduleUpdate();
|
2013-09-10 17:38:47 +08:00
|
|
|
// success
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = true;
|
2013-09-10 17:38:47 +08:00
|
|
|
} while (0);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2013-09-10 17:38:47 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
void Scene::addChildToPhysicsWorld(Node* child)
|
|
|
|
{
|
2013-09-10 17:38:47 +08:00
|
|
|
if (_physicsWorld)
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-11-28 16:02:03 +08:00
|
|
|
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
|
|
|
|
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-11-28 16:02:03 +08:00
|
|
|
if (node->getPhysicsBody())
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-11-28 16:02:03 +08:00
|
|
|
_physicsWorld->addBody(node->getPhysicsBody());
|
2013-09-09 16:36:19 +08:00
|
|
|
}
|
2013-11-28 16:02:03 +08:00
|
|
|
|
2013-12-17 17:45:29 +08:00
|
|
|
auto& children = node->getChildren();
|
2013-12-20 05:34:41 +08:00
|
|
|
for( const auto &n : children) {
|
2013-11-28 16:02:03 +08:00
|
|
|
addToPhysicsWorldFunc(n);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-09-10 17:38:47 +08:00
|
|
|
};
|
|
|
|
|
2013-10-28 16:17:19 +08:00
|
|
|
addToPhysicsWorldFunc(child);
|
2013-09-09 16:36:19 +08:00
|
|
|
}
|
2013-09-09 10:29:02 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|