增加Collision类,去除Node::onCollide和Scene::onCollide方法

This commit is contained in:
Nomango 2018-05-19 00:36:32 +08:00
parent 551cbaa2b2
commit ec1cbaec13
9 changed files with 111 additions and 105 deletions

View File

@ -0,0 +1,34 @@
#include "..\e2dcollider.h"
#include "..\e2dnode.h"
e2d::Node * e2d::Collision::__activeNode = nullptr;
e2d::Node * e2d::Collision::__passiveNode = nullptr;
e2d::Node * e2d::Collision::getActiveNode()
{
return __activeNode;
}
e2d::Node * e2d::Collision::getPassiveNode()
{
return __passiveNode;
}
e2d::Node* e2d::Collision::isCausedBy(Node * node)
{
if (__activeNode == node)
return __passiveNode;
if (__passiveNode == node)
return __activeNode;
return nullptr;
}
e2d::Node* e2d::Collision::isCausedBy(const String& name)
{
if (__activeNode->getName() == name)
return __activeNode;
if (__passiveNode->getName() == name)
return __passiveNode;
return nullptr;
}

View File

@ -42,9 +42,6 @@ static std::vector<e2d::Collider*> s_vColliders;
static std::vector<Listener*> s_vListeners; static std::vector<Listener*> s_vListeners;
// 碰撞触发状态 // 碰撞触发状态
static bool s_bCollisionEnable = false; static bool s_bCollisionEnable = false;
// 发生碰撞的节点
static e2d::Node * s_pActiveNode = nullptr;
static e2d::Node * s_pPassiveNode = nullptr;
void e2d::ColliderManager::setEnable(bool enable) void e2d::ColliderManager::setEnable(bool enable)
@ -84,6 +81,16 @@ void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
Node* pActiveNode = pActiveCollider->_parentNode; Node* pActiveNode = pActiveCollider->_parentNode;
if (pActiveNode) if (pActiveNode)
{ {
// 判断两物体是否是相互冲突的物体
auto IsCollideWith = [](Node * active, Node * passive) -> bool
{
unsigned int hash = passive->getHashName();
for (auto collider : active->_colliders)
if (collider == hash)
return true;
return false;
};
// 获取节点所在场景 // 获取节点所在场景
Scene* pCurrentScene = pActiveNode->getParentScene(); Scene* pCurrentScene = pActiveNode->getParentScene();
@ -101,36 +108,23 @@ void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
if (pPassiveNode && if (pPassiveNode &&
pPassiveNode->getParentScene() == pCurrentScene) pPassiveNode->getParentScene() == pCurrentScene)
{ {
// 判断两物体是否是相互冲突的物体
auto IsCollideWith = [](Node * active, Node * passive) -> bool
{
unsigned int hash = passive->getHashName();
for (auto collider : active->_colliders)
if (collider == hash)
return true;
return false;
};
if (IsCollideWith(pActiveNode, pPassiveNode)) if (IsCollideWith(pActiveNode, pPassiveNode))
{ {
// 判断两碰撞体交集情况 // 判断两碰撞体交集情况
Collider::Relation relation = pActiveCollider->getRelationWith(pPassiveCollider); Collider::Relation relation = pActiveCollider->getRelationWith(pPassiveCollider);
// 忽略 UNKNOWN 和 DISJOINT 情况 // 忽略 UNKNOWN 和 DISJOIN 情况
if (relation != Collider::Relation::UNKNOWN && relation != Collider::Relation::DISJOINT) if (relation != Collider::Relation::UNKNOWN && relation != Collider::Relation::DISJOIN)
{ {
s_pActiveNode = pActiveNode; Collision::__activeNode = pActiveNode;
s_pPassiveNode = pPassiveNode; Collision::__passiveNode = pPassiveNode;
pActiveNode->onCollide(pPassiveNode);
pPassiveNode->onCollide(pActiveNode);
pCurrentScene->onCollide(pActiveNode, pPassiveNode);
ColliderManager::__update(); ColliderManager::__update();
} }
Collision::__activeNode = nullptr;
Collision::__passiveNode = nullptr;
} }
} }
} }
} }
s_pActiveNode = nullptr;
s_pPassiveNode = nullptr;
} }
void e2d::ColliderManager::add(const Function& func, const String& name, bool paused) void e2d::ColliderManager::add(const Function& func, const String& name, bool paused)
@ -196,34 +190,6 @@ void e2d::ColliderManager::stopAll()
} }
} }
e2d::Node * e2d::ColliderManager::getActiveNode()
{
return s_pActiveNode;
}
e2d::Node * e2d::ColliderManager::getPassiveNode()
{
return s_pPassiveNode;
}
e2d::Node* e2d::ColliderManager::isCausedBy(Node * node)
{
if (s_pActiveNode == node)
return s_pPassiveNode;
if (s_pPassiveNode == node)
return s_pActiveNode;
return nullptr;
}
e2d::Node* e2d::ColliderManager::isCausedBy(const String& name)
{
if (s_pActiveNode->getName() == name)
return s_pActiveNode;
if (s_pPassiveNode->getName() == name)
return s_pPassiveNode;
return nullptr;
}
void e2d::ColliderManager::__addCollider(Collider * pCollider) void e2d::ColliderManager::__addCollider(Collider * pCollider)
{ {
if (pCollider) if (pCollider)

View File

@ -905,14 +905,14 @@ bool e2d::Node::isPointIn(Point point) const
return false; return false;
} }
bool e2d::Node::isIntersectWith(const Node * node) const bool e2d::Node::isOverlappedWith(const Node * node) const
{ {
// 如果存在碰撞体,用碰撞体判断 // 如果存在碰撞体,用碰撞体判断
if (this->_collider && node->_collider) if (this->_collider && node->_collider)
{ {
Collider::Relation relation = this->_collider->getRelationWith(node->_collider); Collider::Relation relation = this->_collider->getRelationWith(node->_collider);
if ((relation != Collider::Relation::UNKNOWN) && if ((relation != Collider::Relation::UNKNOWN) &&
(relation != Collider::Relation::DISJOINT)) (relation != Collider::Relation::DISJOIN))
{ {
return true; return true;
} }

View File

@ -6,6 +6,37 @@ namespace e2d
{ {
// 碰撞事件
class Collision
{
friend ColliderManager;
public:
// 获取碰撞发生时的主动体
static Node * getActiveNode();
// 获取碰撞发生时的被动体
static Node * getPassiveNode();
// 判断碰撞是否由该节点引发
// 如果是,返回与其相撞的节点指针,否则返回空
static Node * isCausedBy(
Node * node
);
// 判断发生碰撞的节点名称是否相同
// 若相同,返回其指针,否则返回空
static Node * isCausedBy(
const String& name
);
private:
static Node * __activeNode;
static Node * __passiveNode;
};
class ColliderManager; class ColliderManager;
// 碰撞体 // 碰撞体
@ -28,7 +59,7 @@ public:
enum class Relation : int enum class Relation : int
{ {
UNKNOWN = 0, /* 关系不确定 */ UNKNOWN = 0, /* 关系不确定 */
DISJOINT = 1, /* 没有交集 */ DISJOIN = 1, /* 没有交集 */
IS_CONTAINED = 2, /* 完全被包含 */ IS_CONTAINED = 2, /* 完全被包含 */
CONTAINS = 3, /* 完全包含 */ CONTAINS = 3, /* 完全包含 */
OVERLAP = 4 /* 部分重叠 */ OVERLAP = 4 /* 部分重叠 */

View File

@ -6,9 +6,28 @@ namespace e2d
{ {
// 方向
enum class Direction : int
{
UP, /* 上 */
DOWN, /* 下 */
LEFT, /* 左 */
RIGHT /* 右 */
};
// 线条相交样式
enum class LineJoin : int
{
MITER = 0, /* 斜切 */
BEVEL = 1, /* 斜角 */
ROUND = 2 /* 圆角 */
};
class Size; class Size;
// 表示坐标的结构体 // 坐标
class Point class Point
{ {
public: public:
@ -32,7 +51,7 @@ public:
// 二维向量 // 二维向量
typedef Point Vector; typedef Point Vector;
// 表示大小的结构体 // 大小
class Size class Size
{ {
public: public:
@ -295,25 +314,6 @@ public:
}; };
// 方向
enum class Direction : int
{
UP, /* 上 */
DOWN, /* 下 */
LEFT, /* 左 */
RIGHT /* 右 */
};
// 线条相交样式
enum class LineJoin : int
{
MITER = 0,
BEVEL = 1,
ROUND = 2
};
// 函数对象 // 函数对象
class Function class Function
{ {
@ -622,12 +622,6 @@ public:
// 重写这个函数,它将在离开这个场景时自动执行 // 重写这个函数,它将在离开这个场景时自动执行
virtual void onExit() {} virtual void onExit() {}
// 重写这个函数,它将在碰撞发生时自动执行
virtual void onCollide(
Node* pActiveNode, /* 碰撞发生时的主动体 */
Node* pPassiveNode /* 碰撞发生时的被动体 */
) {}
// 重写这个函数,它将在关闭窗口时执行(返回 false 将阻止窗口关闭) // 重写这个函数,它将在关闭窗口时执行(返回 false 将阻止窗口关闭)
virtual bool onCloseWindow() { return true; } virtual bool onCloseWindow() { return true; }

View File

@ -265,24 +265,6 @@ public:
// 停止所有监听器 // 停止所有监听器
static void stopAll(); static void stopAll();
// 获取碰撞发生时的主动体
static Node * getActiveNode();
// 获取碰撞发生时的被动体
static Node * getPassiveNode();
// 判断碰撞是否由该节点引发
// 如果是,返回与其相撞的节点指针,否则返回空
static Node * isCausedBy(
Node * node
);
// 判断发生碰撞的节点名称是否相同
// 若相同,返回其指针,否则返回空
static Node * isCausedBy(
const String& name
);
private: private:
// 更新监听器 // 更新监听器
static void __update(); static void __update();

View File

@ -51,11 +51,6 @@ public:
// 渲染节点 // 渲染节点
virtual void onRender() {} virtual void onRender() {}
// 碰撞处理
virtual void onCollide(
Node* node /* 发生碰撞的节点 */
) {}
// 获取节点显示状态 // 获取节点显示状态
virtual bool isVisiable() const; virtual bool isVisiable() const;
@ -64,8 +59,8 @@ public:
Point point Point point
) const; ) const;
// 判断两节点是否相交 // 判断两节点是否重叠
virtual bool isIntersectWith( virtual bool isOverlappedWith(
const Node * node const Node * node
) const; ) const;

View File

@ -223,6 +223,7 @@
<ClCompile Include="..\..\core\Collider\ColliderCircle.cpp" /> <ClCompile Include="..\..\core\Collider\ColliderCircle.cpp" />
<ClCompile Include="..\..\core\Collider\ColliderEllipse.cpp" /> <ClCompile Include="..\..\core\Collider\ColliderEllipse.cpp" />
<ClCompile Include="..\..\core\Collider\ColliderRect.cpp" /> <ClCompile Include="..\..\core\Collider\ColliderRect.cpp" />
<ClCompile Include="..\..\core\Collider\Collision.cpp" />
<ClCompile Include="..\..\core\Common\Animation.cpp" /> <ClCompile Include="..\..\core\Common\Animation.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" /> <ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" /> <ClCompile Include="..\..\core\Common\Function.cpp" />

View File

@ -222,6 +222,9 @@
<ClCompile Include="..\..\core\Tool\Music.cpp"> <ClCompile Include="..\..\core\Tool\Music.cpp">
<Filter>Tool</Filter> <Filter>Tool</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Collider\Collision.cpp">
<Filter>Collider</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" /> <ClInclude Include="..\..\core\easy2d.h" />