mirror of https://github.com/axmolengine/axmol.git
issue #2771: Add method Scene::createWithPhysics() end Scene::initWithPhysics()
This commit is contained in:
parent
bcab90ddc4
commit
7bba5f1b01
|
@ -52,8 +52,6 @@ class ActionManager;
|
|||
class Component;
|
||||
class Dictionary;
|
||||
class ComponentContainer;
|
||||
class Scene;
|
||||
class PhysicsBody;
|
||||
|
||||
/**
|
||||
* @addtogroup base_nodes
|
||||
|
|
|
@ -33,6 +33,9 @@ THE SOFTWARE.
|
|||
NS_CC_BEGIN
|
||||
|
||||
Scene::Scene()
|
||||
#ifdef _physicsWorld
|
||||
: _physicsWorld(nullptr)
|
||||
#endif
|
||||
{
|
||||
_ignoreAnchorPointForPosition = true;
|
||||
setAnchorPoint(Point(0.5f, 0.5f));
|
||||
|
@ -56,7 +59,7 @@ bool Scene::init()
|
|||
return bRet;
|
||||
}
|
||||
|
||||
Scene *Scene::create(bool usePhysics/* = false*/)
|
||||
Scene *Scene::create()
|
||||
{
|
||||
Scene *pRet = new Scene();
|
||||
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)
|
||||
{
|
||||
Node::addChild(child);
|
||||
|
@ -85,30 +120,35 @@ void Scene::addChild(Node* child, int zOrder, int 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 (sp && sp->getPhysicsBody())
|
||||
if (typeid(Sprite).hash_code() == typeid(node).hash_code())
|
||||
{
|
||||
_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())
|
||||
{
|
||||
Object* subChild = nullptr;
|
||||
CCARRAY_FOREACH(child->getChildren(), subChild)
|
||||
};
|
||||
|
||||
if(typeid(Layer).hash_code() == typeid(child).hash_code())
|
||||
{
|
||||
addToPhysicsWorldFunc(subChild);
|
||||
Object* subChild = nullptr;
|
||||
CCARRAY_FOREACH(child->getChildren(), subChild)
|
||||
{
|
||||
addToPhysicsWorldFunc(subChild);
|
||||
}
|
||||
}else
|
||||
{
|
||||
addToPhysicsWorldFunc(child);
|
||||
}
|
||||
}else
|
||||
{
|
||||
addToPhysicsWorldFunc(child);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -28,11 +28,10 @@ THE SOFTWARE.
|
|||
#define __CCSCENE_H__
|
||||
|
||||
#include "base_nodes/CCNode.h"
|
||||
#include "physics/CCPhysicsWorld.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class PhysicsWorld;
|
||||
|
||||
/**
|
||||
* @addtogroup scene
|
||||
* @{
|
||||
|
@ -52,21 +51,31 @@ class CC_DLL Scene : public Node
|
|||
{
|
||||
public:
|
||||
/** creates a new Scene object */
|
||||
static Scene *create(bool usePhysics = false);
|
||||
static Scene *create();
|
||||
#ifdef CC_USE_PHYSICS
|
||||
static Scene *createWithPhysics();
|
||||
#endif
|
||||
|
||||
Scene();
|
||||
virtual ~Scene();
|
||||
|
||||
bool init();
|
||||
#ifdef CC_USE_PHYSICS
|
||||
bool initWithPhysics();
|
||||
#endif
|
||||
|
||||
virtual void addChild(Node* child) override;
|
||||
virtual void addChild(Node* child, int zOrder) override;
|
||||
virtual void addChild(Node* child, int zOrder, int tag) override;
|
||||
|
||||
#ifdef CC_USE_PHYSICS
|
||||
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
#ifdef CC_USE_PHYSICS
|
||||
PhysicsWorld* _physicsWorld;
|
||||
#endif
|
||||
};
|
||||
|
||||
// end of scene group
|
||||
|
|
|
@ -36,6 +36,7 @@ THE SOFTWARE.
|
|||
#ifdef EMSCRIPTEN
|
||||
#include "base_nodes/CCGLBufferedNode.h"
|
||||
#endif // EMSCRIPTEN
|
||||
#include "physics/CCPhysicsBody.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -443,6 +444,7 @@ public:
|
|||
*/
|
||||
void setFlipY(bool bFlipY);
|
||||
|
||||
#ifdef CC_USE_PHYSICS
|
||||
/**
|
||||
* set the PhysicsBody that let the sprite effect with physics
|
||||
*/
|
||||
|
@ -452,6 +454,7 @@ public:
|
|||
* get the PhysicsBody the sprite have
|
||||
*/
|
||||
PhysicsBody* getPhysicsBody() const;
|
||||
#endif
|
||||
|
||||
/// @} End of Sprite properties getter/setters
|
||||
|
||||
|
@ -550,7 +553,9 @@ protected:
|
|||
bool _flipX; /// Whether the sprite is flipped horizaontally or not.
|
||||
bool _flipY; /// Whether the sprite is flipped vertically or not.
|
||||
|
||||
#ifdef CC_USE_PHYSICS
|
||||
PhysicsBody* _physicsBody; ///< the physicsBody the node have
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue