diff --git a/core/Collider/Collision.cpp b/core/Collider/Collision.cpp new file mode 100644 index 00000000..17633103 --- /dev/null +++ b/core/Collider/Collision.cpp @@ -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; +} \ No newline at end of file diff --git a/core/Manager/ColliderManager.cpp b/core/Manager/ColliderManager.cpp index ddf3cb1d..7548fcbb 100644 --- a/core/Manager/ColliderManager.cpp +++ b/core/Manager/ColliderManager.cpp @@ -42,9 +42,6 @@ static std::vector s_vColliders; static std::vector s_vListeners; // 碰撞触发状态 static bool s_bCollisionEnable = false; -// 发生碰撞的节点 -static e2d::Node * s_pActiveNode = nullptr; -static e2d::Node * s_pPassiveNode = nullptr; void e2d::ColliderManager::setEnable(bool enable) @@ -84,6 +81,16 @@ void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider) Node* pActiveNode = pActiveCollider->_parentNode; 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(); @@ -101,36 +108,23 @@ void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider) if (pPassiveNode && 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)) { // 判断两碰撞体交集情况 Collider::Relation relation = pActiveCollider->getRelationWith(pPassiveCollider); - // 忽略 UNKNOWN 和 DISJOINT 情况 - if (relation != Collider::Relation::UNKNOWN && relation != Collider::Relation::DISJOINT) + // 忽略 UNKNOWN 和 DISJOIN 情况 + if (relation != Collider::Relation::UNKNOWN && relation != Collider::Relation::DISJOIN) { - s_pActiveNode = pActiveNode; - s_pPassiveNode = pPassiveNode; - pActiveNode->onCollide(pPassiveNode); - pPassiveNode->onCollide(pActiveNode); - pCurrentScene->onCollide(pActiveNode, pPassiveNode); + Collision::__activeNode = pActiveNode; + Collision::__passiveNode = pPassiveNode; 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) @@ -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) { if (pCollider) diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 55987386..a74dddd7 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -905,14 +905,14 @@ bool e2d::Node::isPointIn(Point point) const return false; } -bool e2d::Node::isIntersectWith(const Node * node) const +bool e2d::Node::isOverlappedWith(const Node * node) const { // 如果存在碰撞体,用碰撞体判断 if (this->_collider && node->_collider) { Collider::Relation relation = this->_collider->getRelationWith(node->_collider); if ((relation != Collider::Relation::UNKNOWN) && - (relation != Collider::Relation::DISJOINT)) + (relation != Collider::Relation::DISJOIN)) { return true; } diff --git a/core/e2dcollider.h b/core/e2dcollider.h index 18b4f3c2..539715ba 100644 --- a/core/e2dcollider.h +++ b/core/e2dcollider.h @@ -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; // 碰撞体 @@ -28,7 +59,7 @@ public: enum class Relation : int { UNKNOWN = 0, /* 关系不确定 */ - DISJOINT = 1, /* 没有交集 */ + DISJOIN = 1, /* 没有交集 */ IS_CONTAINED = 2, /* 完全被包含 */ CONTAINS = 3, /* 完全包含 */ OVERLAP = 4 /* 部分重叠 */ diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 773c3d3e..cf2a7132 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -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 Point { public: @@ -32,7 +51,7 @@ public: // 二维向量 typedef Point Vector; -// 表示大小的结构体 +// 大小 class Size { 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 { @@ -622,12 +622,6 @@ public: // 重写这个函数,它将在离开这个场景时自动执行 virtual void onExit() {} - // 重写这个函数,它将在碰撞发生时自动执行 - virtual void onCollide( - Node* pActiveNode, /* 碰撞发生时的主动体 */ - Node* pPassiveNode /* 碰撞发生时的被动体 */ - ) {} - // 重写这个函数,它将在关闭窗口时执行(返回 false 将阻止窗口关闭) virtual bool onCloseWindow() { return true; } diff --git a/core/e2dmanager.h b/core/e2dmanager.h index 33108f3d..ee06abe7 100644 --- a/core/e2dmanager.h +++ b/core/e2dmanager.h @@ -265,24 +265,6 @@ public: // 停止所有监听器 static void stopAll(); - // 获取碰撞发生时的主动体 - static Node * getActiveNode(); - - // 获取碰撞发生时的被动体 - static Node * getPassiveNode(); - - // 判断碰撞是否由该节点引发 - // 如果是,返回与其相撞的节点指针,否则返回空 - static Node * isCausedBy( - Node * node - ); - - // 判断发生碰撞的节点名称是否相同 - // 若相同,返回其指针,否则返回空 - static Node * isCausedBy( - const String& name - ); - private: // 更新监听器 static void __update(); diff --git a/core/e2dnode.h b/core/e2dnode.h index 16993c3a..7df1b46f 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -51,11 +51,6 @@ public: // 渲染节点 virtual void onRender() {} - // 碰撞处理 - virtual void onCollide( - Node* node /* 发生碰撞的节点 */ - ) {} - // 获取节点显示状态 virtual bool isVisiable() const; @@ -64,8 +59,8 @@ public: Point point ) const; - // 判断两节点是否相交 - virtual bool isIntersectWith( + // 判断两节点是否重叠 + virtual bool isOverlappedWith( const Node * node ) const; diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index f9fdaf49..a175a84f 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -223,6 +223,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 2c355783..b4559294 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -222,6 +222,9 @@ Tool + + Collider +