From 7bba5f1b01e275d64d7bd66def2a3d0c393de3fe Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 17:38:47 +0800 Subject: [PATCH] issue #2771: Add method Scene::createWithPhysics() end Scene::initWithPhysics() --- cocos2dx/base_nodes/CCNode.h | 2 - .../CCScene.cpp | 76 ++++++++++++++----- .../layers_scenes_transitions_nodes/CCScene.h | 15 +++- cocos2dx/sprite_nodes/CCSprite.h | 5 ++ 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index d3ea08952d..dd6384e724 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -52,8 +52,6 @@ class ActionManager; class Component; class Dictionary; class ComponentContainer; -class Scene; -class PhysicsBody; /** * @addtogroup base_nodes diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index a2bc6bb4ed..589a77d60b 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -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(node); - - if (sp && sp->getPhysicsBody()) + if (typeid(Sprite).hash_code() == typeid(node).hash_code()) { - _physicsWorld->addChild(sp->getPhysicsBody()); + Sprite* sp = dynamic_cast(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 } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h index c084852513..4b5b27f4a4 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h @@ -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 diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 16162b810b..4d4d0e7bdf 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -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 };