From cb08f2baf2026662a05e8458de526e2e1b1ad87f Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 16:36:19 +0800 Subject: [PATCH] issue #2771: Change _physicsBody from node to sprite. Reimplement Scene::addChild. Adjust some API. --- cocos2dx/base_nodes/CCNode.cpp | 23 ------------- cocos2dx/base_nodes/CCNode.h | 33 ------------------- .../CCScene.cpp | 29 +++++++++++++++- cocos2dx/physics/CCPhysicsBody.h | 26 +++++++++------ cocos2dx/physics/CCPhysicsContactDelegate.h | 10 +++--- cocos2dx/physics/CCPhysicsJoint.h | 2 +- cocos2dx/physics/CCPhysicsWorld.cpp | 1 - cocos2dx/physics/CCPhysicsWorld.h | 24 +++++++++----- cocos2dx/sprite_nodes/CCSprite.cpp | 13 +++++++- cocos2dx/sprite_nodes/CCSprite.h | 12 +++++++ 10 files changed, 89 insertions(+), 84 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index b6ab5fa20e..ef111dfbce 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -120,8 +120,6 @@ Node::Node(void) , _isTransitionFinished(false) , _updateScriptHandler(0) , _componentContainer(NULL) -, _scene(nullptr) -, _physicsBody(nullptr) { // set default scheduler and actionManager Director *director = Director::getInstance(); @@ -590,7 +588,6 @@ void Node::addChild(Node *child, int zOrder, int tag) child->_tag = tag; child->setParent(this); - child->setScene(_scene); child->setOrderOfArrival(s_globalOrderOfArrival++); if( _running ) @@ -1274,26 +1271,6 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } -void Node::setScene(Scene* scene) -{ - _scene = scene; -} - -Scene* Node::getScene() -{ - return _scene; -} - -void Node::setPhysicsBody(PhysicsBody* body) -{ - _physicsBody = body; -} - -PhysicsBody* Node::getPhysicsBody() -{ - return _physicsBody; -} - // NodeRGBA NodeRGBA::NodeRGBA() : _displayedOpacity(255) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 389218453a..d3ea08952d 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -1302,35 +1302,6 @@ public: */ virtual void removeAllComponents(); /// @} end of component functions - - /// @{ - /// @name scene functions - - /** - * set the scene the node belongs to - * DO NOT call it manually - */ - virtual void setScene(Scene* scene); - - /** - * get the scene the node belongs to - */ - virtual Scene* getScene(); - /// @} end of scene functions - - /// @{ - /// @name physics functions - - /** - * set the PhysicsBody that let the node effect with physics - */ - virtual void setPhysicsBody(PhysicsBody* body); - - /** - * get the PhysicsBody the node have - */ - PhysicsBody* getPhysicsBody(); - /// @} end of physics functions protected: /// lazy allocs @@ -1409,10 +1380,6 @@ protected: ccScriptType _scriptType; ///< type of script binding, lua or javascript ComponentContainer *_componentContainer; ///< Dictionary of components - - Scene* _scene; ///< the scene the node belongs to - PhysicsBody* _physicsBody; ///< the physicsBody the node have - }; //#pragma mark - NodeRGBA diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index 3c3e2faf1a..a2bc6bb4ed 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -26,6 +26,9 @@ THE SOFTWARE. #include "CCScene.h" #include "CCDirector.h" +#include "CCLayer.h" +#include "sprite_nodes/CCSprite.h" +#include "physics/CCPhysicsWorld.h" NS_CC_BEGIN @@ -81,7 +84,31 @@ void Scene::addChild(Node* child, int zOrder) void Scene::addChild(Node* child, int zOrder, int tag) { Node::addChild(child, zOrder, tag); - child->setScene(this); + + auto addToPhysicsWorldFunc = [this](Object* node) -> void + { + if (typeid(Sprite).hash_code() == typeid(node).hash_code()) + { + 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) + { + addToPhysicsWorldFunc(subChild); + } + }else + { + addToPhysicsWorldFunc(child); + } } diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index f880a7d36f..ee0adeba0e 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -28,13 +28,15 @@ #include "CCObject.h" #include "CCGeometry.h" #include "CCPhysicsSetting.h" +#include NS_CC_BEGIN -class Node; +class Sprite; class PhysicsWorld; +class PhysicsJoint; class PhysicsFixture; -class PhysicsBody : public Object +class PhysicsBody : public Object, public Clonable { public: static PhysicsBody* createCircle(Point centre, float radius); @@ -55,19 +57,23 @@ public: virtual void applyAngularImpulse(float impulse); void addFixture(PhysicsFixture* fixture); - inline Array* getFixtures() { return _fixtures; } + inline Array* getFixtures() const { return _fixtures; } void removeFixture(PhysicsFixture* fixture); void removeAllFixtures(); - inline PhysicsWorld* getWorld() { return _physicsWorld; } - inline Array* getJoints() { return _joints; } + inline PhysicsWorld* getWorld() const { return _physicsWorld; } + inline const std::vector* getJoints() const { return &_joints; } + + inline Sprite* getOwner() const { return _owner; } void setCategoryBitmask(int bitmask); - inline int getCategoryBitmask() { return _categoryBitmask; } + inline int getCategoryBitmask() const { return _categoryBitmask; } void setContactTestBitmask(int bitmask); - inline int getContactTestBitmask() { return _contactTestBitmask; } + inline int getContactTestBitmask() const { return _contactTestBitmask; } void setCollisionBitmask(int bitmask); - inline int getCollisionBitmask() { return _collisionBitmask; } + inline int getCollisionBitmask() const { return _collisionBitmask; } + + virtual Clonable* clone() const override; protected: bool init(); @@ -81,7 +87,7 @@ private: float _density; float _area; float _friction; - Node* _node; + Sprite* _owner; Point _velocity; float _angularVelocity; bool _resting; @@ -90,7 +96,7 @@ private: int _contactTestBitmask; int _collisionBitmask; - Array* _joints; + std::vector _joints; Array* _fixtures; PhysicsWorld* _physicsWorld; }; diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index 0e5f137ef8..1bead362d7 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -33,15 +33,15 @@ NS_CC_BEGIN class PhysicsBody; -class PhysicsContactDelegate : public Object +class PhysicsContactDelegate { public: - virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); - virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); - -protected: PhysicsContactDelegate(); virtual ~PhysicsContactDelegate(); + +public: + virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; + virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; }; NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 8e687d8ab9..964f718135 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -33,7 +33,7 @@ NS_CC_BEGIN class PhysicsBody; -class PhysicsJoint : public Object +class PhysicsJoint { protected: PhysicsJoint(); diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index e53958286b..7ca586a2b1 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -70,7 +70,6 @@ PhysicsWorld* PhysicsWorld::create() PhysicsWorld * physicsWorld = new PhysicsWorld(); if(physicsWorld && physicsWorld->init()) { - physicsWorld->autorelease(); return physicsWorld; } diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 82981dcd7e..bd450e49c2 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -29,6 +29,7 @@ #include "CCGeometry.h" #include "CCPhysicsSetting.h" + NS_CC_BEGIN class PhysicsBody; @@ -36,35 +37,40 @@ class PhysicsJoint; class PhysicsWorldInfo; class PhysicsContactDelegate; -class PhysicsWorld : public Object +class Sprite; +class Scene; + +class PhysicsWorld { public: - static PhysicsWorld* create(); - void addChild(PhysicsBody* body); - void addJoint(PhysicsJoint* joint); void removeJoint(PhysicsJoint* joint); void removeAllJoints(); - Array* bodysAlongRay(Point start, Point end); - Array* bodysAtPoint(Point point); - Array* bodysInRect(Rect rect); - Array* getAllBody(); + Array* getBodysAlongRay(Point start, Point end) const; + Array* getBodysAtPoint(Point point) const; + Array* getBodysInRect(Rect rect) const; + Array* getAllBody() const; void registerContactDelegate(PhysicsContactDelegate* delegate); protected: + static PhysicsWorld* create(); bool init(); + virtual void addChild(PhysicsBody* body); protected: Point _gravity; float _speed; - class PhysicsWorldInfo* _worldInfo; + PhysicsWorldInfo* _worldInfo; protected: PhysicsWorld(); virtual ~PhysicsWorld(); + + friend class Sprite; + friend class Scene; }; NS_CC_END diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 272605d861..ed8f0460c3 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -295,7 +295,8 @@ Sprite* Sprite::initWithCGImage(CGImageRef pImage, const char *pszKey) Sprite::Sprite(void) : _shouldBeHidden(false) -, _texture(NULL) +, _texture(nullptr) +, _physicsBody(nullptr) { } @@ -446,6 +447,16 @@ void Sprite::setTextureCoords(Rect rect) } } +void Sprite::setPhysicsBody(PhysicsBody* body) +{ + _physicsBody = body; +} + +PhysicsBody* Sprite::getPhysicsBody() const +{ + return _physicsBody; +} + void Sprite::updateTransform(void) { CCASSERT(_batchNode, "updateTransform is only valid when Sprite is being rendered using an SpriteBatchNode"); diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 20872e33a9..16162b810b 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -443,6 +443,16 @@ public: */ void setFlipY(bool bFlipY); + /** + * set the PhysicsBody that let the sprite effect with physics + */ + virtual void setPhysicsBody(PhysicsBody* body); + + /** + * get the PhysicsBody the sprite have + */ + PhysicsBody* getPhysicsBody() const; + /// @} End of Sprite properties getter/setters @@ -539,6 +549,8 @@ protected: // image is flipped bool _flipX; /// Whether the sprite is flipped horizaontally or not. bool _flipY; /// Whether the sprite is flipped vertically or not. + + PhysicsBody* _physicsBody; ///< the physicsBody the node have };