issue #2771: Change _physicsBody from node to sprite. Reimplement Scene::addChild. Adjust some API.

This commit is contained in:
boyu0 2013-09-09 16:36:19 +08:00
parent dce205f688
commit cb08f2baf2
10 changed files with 89 additions and 84 deletions

View File

@ -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)

View File

@ -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

View File

@ -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<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)
{
addToPhysicsWorldFunc(subChild);
}
}else
{
addToPhysicsWorldFunc(child);
}
}

View File

@ -28,13 +28,15 @@
#include "CCObject.h"
#include "CCGeometry.h"
#include "CCPhysicsSetting.h"
#include <vector>
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<PhysicsJoint*>* 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<PhysicsJoint*> _joints;
Array* _fixtures;
PhysicsWorld* _physicsWorld;
};

View File

@ -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

View File

@ -33,7 +33,7 @@ NS_CC_BEGIN
class PhysicsBody;
class PhysicsJoint : public Object
class PhysicsJoint
{
protected:
PhysicsJoint();

View File

@ -70,7 +70,6 @@ PhysicsWorld* PhysicsWorld::create()
PhysicsWorld * physicsWorld = new PhysicsWorld();
if(physicsWorld && physicsWorld->init())
{
physicsWorld->autorelease();
return physicsWorld;
}

View File

@ -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

View File

@ -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");

View File

@ -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
};