2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:50:41 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene::Scene()
|
2013-10-10 18:31:43 +08:00
|
|
|
#ifdef 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;
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(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-10-11 10:08:51 +08:00
|
|
|
#ifdef 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
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Director * pDirector;
|
2013-07-12 06:24:23 +08:00
|
|
|
CC_BREAK_IF( ! (pDirector = Director::getInstance()) );
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(pDirector->getWinSize());
|
|
|
|
// success
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:38:47 +08:00
|
|
|
Scene *Scene::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Scene *pRet = new Scene();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:38:47 +08:00
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
Scene *Scene::createWithPhysics()
|
|
|
|
{
|
|
|
|
Scene *pRet = new Scene();
|
|
|
|
if (pRet && pRet->initWithPhysics())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::initWithPhysics()
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Director * pDirector;
|
|
|
|
CC_BREAK_IF( ! (pDirector = Director::getInstance()) );
|
|
|
|
this->setContentSize(pDirector->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
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
void Scene::addChild(Node* child)
|
|
|
|
{
|
|
|
|
Node::addChild(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::addChild(Node* child, int zOrder)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::addChild(Node* child, int zOrder, int tag)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder, tag);
|
2013-09-09 16:36:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
addChildToPhysicsWorld(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::addChildToPhysicsWorld(Node* child)
|
|
|
|
{
|
2013-09-10 17:38:47 +08:00
|
|
|
if (_physicsWorld)
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-10-10 18:31:43 +08:00
|
|
|
std::function<void(Object*)> addToPhysicsWorldFunc = nullptr;
|
2013-11-11 12:49:38 +08:00
|
|
|
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Object* obj) -> void
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-10-28 16:17:19 +08:00
|
|
|
|
2013-11-11 12:49:38 +08:00
|
|
|
if (dynamic_cast<Node*>(obj) != nullptr)
|
2013-09-09 16:36:19 +08:00
|
|
|
{
|
2013-11-11 12:49:38 +08:00
|
|
|
Node* node = dynamic_cast<Node*>(obj);
|
2013-09-10 17:38:47 +08:00
|
|
|
|
2013-09-30 13:02:17 +08:00
|
|
|
if (node->getPhysicsBody())
|
2013-09-10 17:38:47 +08:00
|
|
|
{
|
2013-09-30 13:02:17 +08:00
|
|
|
_physicsWorld->addBody(node->getPhysicsBody());
|
2013-09-10 17:38:47 +08:00
|
|
|
}
|
2013-10-28 16:17:19 +08:00
|
|
|
|
|
|
|
Object* subChild = nullptr;
|
|
|
|
CCARRAY_FOREACH(node->getChildren(), subChild)
|
|
|
|
{
|
|
|
|
addToPhysicsWorldFunc(subChild);
|
|
|
|
}
|
2013-09-09 16:36:19 +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
|
|
|
void Scene::update(float delta)
|
|
|
|
{
|
|
|
|
Node::update(delta);
|
|
|
|
|
2013-11-11 17:03:43 +08:00
|
|
|
if (nullptr != _physicsWorld)
|
|
|
|
{
|
|
|
|
_physicsWorld->update(delta);
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|