issue #2771: Add method Scene::createWithPhysics() end Scene::initWithPhysics()

This commit is contained in:
boyu0 2013-09-10 17:38:47 +08:00
parent bcab90ddc4
commit 7bba5f1b01
4 changed files with 75 additions and 23 deletions

View File

@ -52,8 +52,6 @@ class ActionManager;
class Component; class Component;
class Dictionary; class Dictionary;
class ComponentContainer; class ComponentContainer;
class Scene;
class PhysicsBody;
/** /**
* @addtogroup base_nodes * @addtogroup base_nodes

View File

@ -33,6 +33,9 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
Scene::Scene() Scene::Scene()
#ifdef _physicsWorld
: _physicsWorld(nullptr)
#endif
{ {
_ignoreAnchorPointForPosition = true; _ignoreAnchorPointForPosition = true;
setAnchorPoint(Point(0.5f, 0.5f)); setAnchorPoint(Point(0.5f, 0.5f));
@ -56,7 +59,7 @@ bool Scene::init()
return bRet; return bRet;
} }
Scene *Scene::create(bool usePhysics/* = false*/) Scene *Scene::create()
{ {
Scene *pRet = new Scene(); Scene *pRet = new Scene();
if (pRet && pRet->init()) if (pRet && pRet->init())
@ -71,6 +74,38 @@ Scene *Scene::create(bool usePhysics/* = false*/)
} }
} }
#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());
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::create()));
// success
bRet = true;
} while (0);
return bRet;
}
#endif
void Scene::addChild(Node* child) void Scene::addChild(Node* child)
{ {
Node::addChild(child); Node::addChild(child);
@ -85,30 +120,35 @@ void Scene::addChild(Node* child, int zOrder, int tag)
{ {
Node::addChild(child, zOrder, tag); Node::addChild(child, zOrder, tag);
auto addToPhysicsWorldFunc = [this](Object* node) -> void #ifdef CC_USE_PHYSICS
if (_physicsWorld)
{ {
if (typeid(Sprite).hash_code() == typeid(node).hash_code()) auto addToPhysicsWorldFunc = [this](Object* node) -> void
{ {
Sprite* sp = dynamic_cast<Sprite*>(node); if (typeid(Sprite).hash_code() == typeid(node).hash_code())
if (sp && sp->getPhysicsBody())
{ {
_physicsWorld->addChild(sp->getPhysicsBody()); Sprite* sp = dynamic_cast<Sprite*>(node);
if (sp && sp->getPhysicsBody())
{
_physicsWorld->addChild(sp->getPhysicsBody());
}
} }
} };
};
if(typeid(Layer).hash_code() == typeid(child).hash_code())
if(typeid(Layer).hash_code() == typeid(child).hash_code())
{
Object* subChild = nullptr;
CCARRAY_FOREACH(child->getChildren(), subChild)
{ {
addToPhysicsWorldFunc(subChild); Object* subChild = nullptr;
CCARRAY_FOREACH(child->getChildren(), subChild)
{
addToPhysicsWorldFunc(subChild);
}
}else
{
addToPhysicsWorldFunc(child);
} }
}else
{
addToPhysicsWorldFunc(child);
} }
#endif
} }

View File

@ -28,11 +28,10 @@ THE SOFTWARE.
#define __CCSCENE_H__ #define __CCSCENE_H__
#include "base_nodes/CCNode.h" #include "base_nodes/CCNode.h"
#include "physics/CCPhysicsWorld.h"
NS_CC_BEGIN NS_CC_BEGIN
class PhysicsWorld;
/** /**
* @addtogroup scene * @addtogroup scene
* @{ * @{
@ -52,21 +51,31 @@ class CC_DLL Scene : public Node
{ {
public: public:
/** creates a new Scene object */ /** creates a new Scene object */
static Scene *create(bool usePhysics = false); static Scene *create();
#ifdef CC_USE_PHYSICS
static Scene *createWithPhysics();
#endif
Scene(); Scene();
virtual ~Scene(); virtual ~Scene();
bool init(); bool init();
#ifdef CC_USE_PHYSICS
bool initWithPhysics();
#endif
virtual void addChild(Node* child) override; virtual void addChild(Node* child) override;
virtual void addChild(Node* child, int zOrder) override; virtual void addChild(Node* child, int zOrder) override;
virtual void addChild(Node* child, int zOrder, int tag) override; virtual void addChild(Node* child, int zOrder, int tag) override;
#ifdef CC_USE_PHYSICS
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
#endif
protected: protected:
#ifdef CC_USE_PHYSICS
PhysicsWorld* _physicsWorld; PhysicsWorld* _physicsWorld;
#endif
}; };
// end of scene group // end of scene group

View File

@ -36,6 +36,7 @@ THE SOFTWARE.
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
#include "base_nodes/CCGLBufferedNode.h" #include "base_nodes/CCGLBufferedNode.h"
#endif // EMSCRIPTEN #endif // EMSCRIPTEN
#include "physics/CCPhysicsBody.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -443,6 +444,7 @@ public:
*/ */
void setFlipY(bool bFlipY); void setFlipY(bool bFlipY);
#ifdef CC_USE_PHYSICS
/** /**
* set the PhysicsBody that let the sprite effect with physics * set the PhysicsBody that let the sprite effect with physics
*/ */
@ -452,6 +454,7 @@ public:
* get the PhysicsBody the sprite have * get the PhysicsBody the sprite have
*/ */
PhysicsBody* getPhysicsBody() const; PhysicsBody* getPhysicsBody() const;
#endif
/// @} End of Sprite properties getter/setters /// @} End of Sprite properties getter/setters
@ -550,7 +553,9 @@ protected:
bool _flipX; /// Whether the sprite is flipped horizaontally or not. bool _flipX; /// Whether the sprite is flipped horizaontally or not.
bool _flipY; /// Whether the sprite is flipped vertically or not. bool _flipY; /// Whether the sprite is flipped vertically or not.
#ifdef CC_USE_PHYSICS
PhysicsBody* _physicsBody; ///< the physicsBody the node have PhysicsBody* _physicsBody; ///< the physicsBody the node have
#endif
}; };