mirror of https://github.com/axmolengine/axmol.git
issue #2771: Edit delegate to listener.
This commit is contained in:
parent
a48fee881d
commit
8ec7996dcd
|
@ -128,10 +128,19 @@ bool PhysicsContactPostSolve::init()
|
|||
return true;
|
||||
}
|
||||
|
||||
PhysicsContactListener::PhysicsContactListener()
|
||||
: onContactBegin(nullptr)
|
||||
, onContactPreSolve(nullptr)
|
||||
, onContactPostSolve(nullptr)
|
||||
, onContactEnd(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
||||
#endif
|
||||
PhysicsContactListener::~PhysicsContactListener()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
#endif // CC_USE_PHYSICS
|
||||
|
|
|
@ -36,16 +36,6 @@ NS_CC_BEGIN
|
|||
class PhysicsShape;
|
||||
class PhysicsWorld;
|
||||
|
||||
namespace PhysicsInnerCallbackFunctions
|
||||
{
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
}
|
||||
|
||||
class PhysicsContactInfo;
|
||||
|
||||
class PhysicsContact
|
||||
|
@ -70,13 +60,6 @@ private:
|
|||
void* _data;
|
||||
|
||||
friend class PhysicsWorld;
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
};
|
||||
|
||||
class PhysicsContactPreSolve
|
||||
|
@ -89,13 +72,6 @@ private:
|
|||
bool init();
|
||||
|
||||
friend class PhysicsWorld;
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
};
|
||||
|
||||
class PhysicsContactPostSolve
|
||||
|
@ -108,26 +84,20 @@ private:
|
|||
bool init();
|
||||
|
||||
friend class PhysicsWorld;
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
};
|
||||
|
||||
class PhysicsContactDelegate
|
||||
class PhysicsContactListener
|
||||
{
|
||||
public:
|
||||
PhysicsContactDelegate();
|
||||
virtual ~PhysicsContactDelegate();
|
||||
PhysicsContactListener();
|
||||
virtual ~PhysicsContactListener();
|
||||
|
||||
public:
|
||||
virtual bool onContactBegin(const PhysicsContact& contact) = 0;
|
||||
virtual bool onContactPreSolve(const PhysicsContact& contact, const PhysicsContactPreSolve& solve) = 0;
|
||||
virtual void onContactPostSove(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)= 0;
|
||||
virtual void onContactEnd(const PhysicsContact& contact) = 0;
|
||||
std::function<bool(const PhysicsContact& contact)> onContactBegin;
|
||||
std::function<bool(const PhysicsContact& contact, const PhysicsContactPreSolve& solve)> onContactPreSolve;
|
||||
|
||||
std::function<void(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)> onContactPostSolve;
|
||||
std::function<void(const PhysicsContact& contact)> onContactEnd;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -48,47 +48,44 @@ NS_CC_BEGIN
|
|||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
|
||||
namespace PhysicsInnerCallbackFunctions
|
||||
int PhysicsWorld::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data)
|
||||
{
|
||||
int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
|
||||
CP_ARBITER_GET_SHAPES(arb, a, b);
|
||||
|
||||
auto ita = PhysicsShapeInfo::map.find(a);
|
||||
auto itb = PhysicsShapeInfo::map.find(b);
|
||||
CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end());
|
||||
|
||||
PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape);
|
||||
arb->data = contact;
|
||||
|
||||
return world->collisionBeginCallback(*static_cast<PhysicsContact*>(arb->data));
|
||||
}
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
|
||||
int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
return world->collisionPreSolveCallback(*static_cast<PhysicsContact*>(arb->data),
|
||||
PhysicsContactPreSolve());
|
||||
}
|
||||
CP_ARBITER_GET_SHAPES(arb, a, b);
|
||||
|
||||
void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
world->collisionPostSolveCallback(*static_cast<PhysicsContact*>(arb->data),
|
||||
PhysicsContactPostSolve());
|
||||
}
|
||||
auto ita = PhysicsShapeInfo::map.find(a);
|
||||
auto itb = PhysicsShapeInfo::map.find(b);
|
||||
CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end());
|
||||
|
||||
void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
PhysicsContact* contact = static_cast<PhysicsContact*>(arb->data);
|
||||
|
||||
world->collisionSeparateCallback(*contact);
|
||||
|
||||
delete contact;
|
||||
}
|
||||
PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape);
|
||||
arb->data = contact;
|
||||
|
||||
return world->collisionBeginCallback(*static_cast<PhysicsContact*>(arb->data));
|
||||
}
|
||||
|
||||
int PhysicsWorld::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
return world->collisionPreSolveCallback(*static_cast<PhysicsContact*>(arb->data),
|
||||
PhysicsContactPreSolve());
|
||||
}
|
||||
|
||||
void PhysicsWorld::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
world->collisionPostSolveCallback(*static_cast<PhysicsContact*>(arb->data),
|
||||
PhysicsContactPostSolve());
|
||||
}
|
||||
|
||||
void PhysicsWorld::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
PhysicsContact* contact = static_cast<PhysicsContact*>(arb->data);
|
||||
|
||||
world->collisionSeparateCallback(*contact);
|
||||
|
||||
delete contact;
|
||||
}
|
||||
|
||||
bool PhysicsWorld::init()
|
||||
|
@ -98,10 +95,10 @@ bool PhysicsWorld::init()
|
|||
cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity));
|
||||
|
||||
cpSpaceSetDefaultCollisionHandler(_info->space,
|
||||
PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc,
|
||||
PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc,
|
||||
PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc,
|
||||
PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc,
|
||||
PhysicsWorld::collisionBeginCallbackFunc,
|
||||
PhysicsWorld::collisionPreSolveCallbackFunc,
|
||||
PhysicsWorld::collisionPostSolveCallbackFunc,
|
||||
PhysicsWorld::collisionSeparateCallbackFunc,
|
||||
this);
|
||||
|
||||
return true;
|
||||
|
@ -246,9 +243,9 @@ void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape)
|
|||
|
||||
int PhysicsWorld::collisionBeginCallback(const PhysicsContact& contact)
|
||||
{
|
||||
if (_delegate)
|
||||
if (_listener && _listener->onContactBegin)
|
||||
{
|
||||
return _delegate->onContactBegin(contact);
|
||||
return _listener->onContactBegin(contact);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -256,9 +253,9 @@ int PhysicsWorld::collisionBeginCallback(const PhysicsContact& contact)
|
|||
|
||||
int PhysicsWorld::collisionPreSolveCallback(const PhysicsContact& contact, const PhysicsContactPreSolve& solve)
|
||||
{
|
||||
if (_delegate)
|
||||
if (_listener && _listener->onContactPreSolve)
|
||||
{
|
||||
return _delegate->onContactPreSolve(contact, solve);
|
||||
return _listener->onContactPreSolve(contact, solve);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -266,17 +263,17 @@ int PhysicsWorld::collisionPreSolveCallback(const PhysicsContact& contact, const
|
|||
|
||||
void PhysicsWorld::collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)
|
||||
{
|
||||
if (_delegate)
|
||||
if (_listener && _listener->onContactPreSolve)
|
||||
{
|
||||
_delegate->onContactPostSove(contact, solve);
|
||||
_listener->onContactPostSolve(contact, solve);
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsWorld::collisionSeparateCallback(const PhysicsContact& contact)
|
||||
{
|
||||
if (_delegate)
|
||||
if (_listener && _listener->onContactEnd)
|
||||
{
|
||||
_delegate->onContactEnd(contact);
|
||||
_listener->onContactEnd(contact);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -300,7 +297,7 @@ PhysicsWorld::PhysicsWorld()
|
|||
: _gravity(Point(0.0f, -98.0f))
|
||||
, _speed(1.0f)
|
||||
, _info(nullptr)
|
||||
, _delegate(nullptr)
|
||||
, _listener(nullptr)
|
||||
, _bodys(nullptr)
|
||||
, _scene(nullptr)
|
||||
, _debugDraw(false)
|
||||
|
|
|
@ -40,23 +40,13 @@ class PhysicsShape;
|
|||
class PhysicsContact;
|
||||
class PhysicsContactPreSolve;
|
||||
class PhysicsContactPostSolve;
|
||||
class PhysicsContactDelegate;
|
||||
class PhysicsContactListener;
|
||||
class Array;
|
||||
|
||||
class Sprite;
|
||||
class Scene;
|
||||
class DrawNode;
|
||||
|
||||
namespace PhysicsInnerCallbackFunctions
|
||||
{
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
}
|
||||
|
||||
class PhysicsWorld
|
||||
{
|
||||
public:
|
||||
|
@ -69,8 +59,8 @@ public:
|
|||
Array* getBodysInRect(Rect rect) const;
|
||||
Array* getAllBody() const;
|
||||
|
||||
inline void registerContactDelegate(PhysicsContactDelegate* delegate) { _delegate = delegate; }
|
||||
inline void unregisterContactDelegate() { _delegate = nullptr; }
|
||||
inline void registerContactListener(PhysicsContactListener* delegate) { _listener = delegate; }
|
||||
inline void unregisterContactListener() { _listener = nullptr; }
|
||||
|
||||
inline Point getGravity() { return _gravity; }
|
||||
inline void setGravity(Point gravity) { _gravity = gravity; }
|
||||
|
@ -97,11 +87,18 @@ protected:
|
|||
virtual void collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve);
|
||||
virtual void collisionSeparateCallback(const PhysicsContact& contact);
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
static int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
static int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
static void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
Point _gravity;
|
||||
float _speed;
|
||||
PhysicsWorldInfo* _info;
|
||||
PhysicsContactDelegate* _delegate;
|
||||
PhysicsContactListener* _listener;
|
||||
|
||||
|
||||
Array* _bodys;
|
||||
|
@ -117,13 +114,6 @@ protected:
|
|||
friend class Sprite;
|
||||
friend class Scene;
|
||||
friend class PhysicsBody;
|
||||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
|
||||
friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
|
||||
#endif
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
Loading…
Reference in New Issue