From 0a80483ba6a90c28f46d6a07debdbc62403d5e5f Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 13 Mar 2018 13:13:26 +0800 Subject: [PATCH] Collision detection will not be opened by default, and use ShapeManager::setCollisionEnable to enable it. --- core/Base/Input.cpp | 2 +- core/Common/Listener.cpp | 4 ++-- core/Manager/ActionManager.cpp | 4 ++-- core/Manager/ShapeManager.cpp | 27 +++++++++++++++++----- core/Manager/TimerManager.cpp | 3 +-- core/Node/Node.cpp | 16 ++++++------- core/Tool/{Data.cpp => Path.cpp} | 0 core/Tool/Timer.cpp | 33 --------------------------- core/ebase.h | 2 +- core/ecommon.h | 11 +++++---- core/emanagers.h | 14 ++++++++---- core/etools.h | 12 +--------- project/vs2012/Easy2D.vcxproj | 3 ++- project/vs2012/Easy2D.vcxproj.filters | 7 +++--- project/vs2013/Easy2D.vcxproj | 2 +- project/vs2013/Easy2D.vcxproj.filters | 6 ++--- project/vs2017/Easy2D.vcxproj | 2 +- project/vs2017/Easy2D.vcxproj.filters | 6 ++--- 18 files changed, 68 insertions(+), 86 deletions(-) rename core/Tool/{Data.cpp => Path.cpp} (100%) diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 1e1d0a48..7f00d455 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -186,7 +186,7 @@ void e2d::Input::__add(Listener * pListener) } } -void e2d::Input::add(VoidFunction callback, const String & name) +void e2d::Input::add(ListenerCallback callback, const String & name) { auto pListener = new Listener(callback, name); pListener->start(); diff --git a/core/Common/Listener.cpp b/core/Common/Listener.cpp index 32c55eb8..2be72528 100644 --- a/core/Common/Listener.cpp +++ b/core/Common/Listener.cpp @@ -9,7 +9,7 @@ e2d::Listener::Listener() Input::__add(this); } -e2d::Listener::Listener(VoidFunction callback, const String & name) +e2d::Listener::Listener(ListenerCallback callback, const String & name) : m_bRunning(false) , m_sName(name) , m_callback(callback) @@ -49,7 +49,7 @@ void e2d::Listener::setName(const String & name) m_sName = name; } -void e2d::Listener::setCallback(VoidFunction callback) +void e2d::Listener::setCallback(ListenerCallback callback) { m_callback = callback; } diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index bce5df29..7dcbe7bc 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -191,7 +191,7 @@ void e2d::ActionManager::stopAll() } } -std::vector e2d::ActionManager::getActions(const String & strActionName) +std::vector e2d::ActionManager::get(const String & strActionName) { std::vector vActions; for (const auto action : s_vActions) @@ -204,7 +204,7 @@ std::vector e2d::ActionManager::getActions(const String & strActio return std::move(vActions); } -std::vector e2d::ActionManager::getAllActions() +std::vector e2d::ActionManager::getAll() { return s_vActions; } diff --git a/core/Manager/ShapeManager.cpp b/core/Manager/ShapeManager.cpp index 70bc1f35..c2175b0a 100644 --- a/core/Manager/ShapeManager.cpp +++ b/core/Manager/ShapeManager.cpp @@ -3,19 +3,35 @@ #include "..\eshape.h" // 形状集合 -std::vector s_vShapes; +static std::vector s_vShapes; +// 碰撞触发状态 +static bool s_bCollisionEnable = false; +void e2d::ShapeManager::setCollisionEnable(bool bEnable) +{ + s_bCollisionEnable = bEnable; +} + void e2d::ShapeManager::__updateShape(e2d::Shape * pActiveShape) { + // 判断碰撞触发是否打开 + if (!s_bCollisionEnable) + return; + Node* pActiveNode = pActiveShape->m_pParentNode; if (pActiveNode) { // 获取节点所在场景 Scene* pCurrentScene = pActiveNode->getParentScene(); + // 判断与其他形状的交集情况 - for (auto pPassiveShape : s_vShapes) + for (size_t i = 0; i < s_vShapes.size(); i++) { + auto pPassiveShape = s_vShapes[i]; + // 判断两个形状是否是同一个对象 + if (pActiveShape == pPassiveShape) + return; // 判断两物体是否是相互冲突的物体 if (pActiveShape->m_nCollisionBitmask & pPassiveShape->m_nCategoryBitmask) { @@ -23,7 +39,6 @@ void e2d::ShapeManager::__updateShape(e2d::Shape * pActiveShape) Node* pPassiveNode = pPassiveShape->m_pParentNode; // 判断两节点是否处于同一场景中 if (pPassiveNode && - pPassiveNode != pActiveNode && pPassiveNode->getParentScene() == pCurrentScene) { // 判断两形状交集情况 @@ -41,13 +56,13 @@ void e2d::ShapeManager::__updateShape(e2d::Shape * pActiveShape) } } -void e2d::ShapeManager::__addShape(Shape * pShape) +void e2d::ShapeManager::__add(Shape * pShape) { if (pShape) { if (pShape->m_pParentNode) { - WARN_IF(true, "ShapeManager::__addShape Failed! The shape is already added."); + WARN_IF(true, "ShapeManager::__add Failed! The shape is already added."); return; } pShape->retain(); @@ -55,7 +70,7 @@ void e2d::ShapeManager::__addShape(Shape * pShape) } } -void e2d::ShapeManager::__delShape(Shape * pShape) +void e2d::ShapeManager::__remove(Shape * pShape) { if (pShape) { diff --git a/core/Manager/TimerManager.cpp b/core/Manager/TimerManager.cpp index 9cc57a72..2bef25e1 100644 --- a/core/Manager/TimerManager.cpp +++ b/core/Manager/TimerManager.cpp @@ -30,8 +30,7 @@ void e2d::TimerManager::__update() void e2d::TimerManager::start(double timeOut, TimerCallback callback) { - auto pTimer = new Timer(callback, timeOut, 1, false, true); - pTimer->start(); + (new Timer(L"", callback, timeOut, 1, false, true))->start(); } void e2d::TimerManager::__add(Timer * pTimer) diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 616922ee..f2971f3c 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -47,7 +47,7 @@ e2d::Node::Node() e2d::Node::~Node() { ActionManager::__clearAllBindedWith(this); - ShapeManager::__delShape(m_pShape); + ShapeManager::__remove(m_pShape); for (auto child : m_vChildren) { SafeRelease(&child); @@ -548,9 +548,9 @@ void e2d::Node::setSize(Size size) void e2d::Node::setShape(Shape * pShape) { // 删除旧的形状 - ShapeManager::__delShape(m_pShape); + ShapeManager::__remove(m_pShape); // 添加新的形状 - ShapeManager::__addShape(pShape); + ShapeManager::__add(pShape); if (pShape) { @@ -755,7 +755,7 @@ void e2d::Node::runAction(Action * action) void e2d::Node::resumeAction(const String & strActionName) { - auto actions = ActionManager::getActions(strActionName); + auto actions = ActionManager::get(strActionName); for (auto action : actions) { if (action->getTarget() == this) @@ -767,7 +767,7 @@ void e2d::Node::resumeAction(const String & strActionName) void e2d::Node::pauseAction(const String & strActionName) { - auto actions = ActionManager::getActions(strActionName); + auto actions = ActionManager::get(strActionName); for (auto action : actions) { if (action->getTarget() == this) @@ -779,7 +779,7 @@ void e2d::Node::pauseAction(const String & strActionName) void e2d::Node::stopAction(const String & strActionName) { - auto actions = ActionManager::getActions(strActionName); + auto actions = ActionManager::get(strActionName); for (auto action : actions) { if (action->getTarget() == this) @@ -791,7 +791,7 @@ void e2d::Node::stopAction(const String & strActionName) e2d::Action * e2d::Node::getAction(const String & strActionName) { - auto actions = ActionManager::getActions(strActionName); + auto actions = ActionManager::get(strActionName); for (auto action : actions) { if (action->getTarget() == this) @@ -805,7 +805,7 @@ e2d::Action * e2d::Node::getAction(const String & strActionName) std::vector e2d::Node::getActions(const String & strActionName) { std::vector::iterator iter; - auto actions = ActionManager::getActions(strActionName); + auto actions = ActionManager::get(strActionName); for (iter = actions.begin(); iter != actions.end();) { if ((*iter)->getTarget() != this) diff --git a/core/Tool/Data.cpp b/core/Tool/Path.cpp similarity index 100% rename from core/Tool/Data.cpp rename to core/Tool/Path.cpp diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index 2429bd70..e76996bc 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -2,39 +2,6 @@ #include "..\enodes.h" #include "..\emanagers.h" -e2d::Timer::Timer() - : m_bRunning(false) - , m_nRunTimes(0) - , m_Callback(nullptr) - , m_fInterval(0) - , m_fLast(0) - , m_nUpdateTimes(-1) - , m_bAtOnce(false) - , m_bAutoRelease(false) - , m_bClear(true) -{ - TimerManager::__add(this); -} - -e2d::Timer::Timer(TimerCallback callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) - : m_bRunning(false) - , m_nRunTimes(0) - , m_Callback(nullptr) - , m_fInterval(0) - , m_fLast(0) - , m_nUpdateTimes(-1) - , m_bAtOnce(false) - , m_bAutoRelease(false) - , m_bClear(true) -{ - this->setCallback(callback); - this->setUpdateTimes(updateTimes); - this->setInterval(interval); - m_bAutoRelease = autoRelease; - m_bAtOnce = atOnce; - TimerManager::__add(this); -} - e2d::Timer::Timer(const String & name, TimerCallback callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) : m_bRunning(false) , m_nRunTimes(0) diff --git a/core/ebase.h b/core/ebase.h index 9e3fdaf7..ce1ba7e3 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -153,7 +153,7 @@ class Input public: // 添加输入监听 static void add( - VoidFunction callback, /* 回调函数 */ + ListenerCallback callback, /* 回调函数 */ const String & name = L"" /* 监听器名称 */ ); diff --git a/core/ecommon.h b/core/ecommon.h index b2cb3609..9dde1c76 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -12,6 +12,9 @@ namespace e2d // 返回值和参数列表都为空的函数 typedef std::function VoidFunction; +// 监听器回调函数 +typedef VoidFunction ListenerCallback; + // 定时器回调函数 typedef VoidFunction TimerCallback; @@ -582,9 +585,9 @@ protected: }; -class SceneManager; class Node; class Action; +class SceneManager; // 场景 class Scene : @@ -667,7 +670,7 @@ public: Listener(); Listener( - VoidFunction callback, /* 回调函数 */ + ListenerCallback callback, /* 回调函数 */ const String & name = L"" /* 监听器名称 */ ); @@ -693,7 +696,7 @@ public: // 修改回调函数 void setCallback( - VoidFunction callback + ListenerCallback callback ); // 更新 @@ -703,7 +706,7 @@ protected: String m_sName; bool m_bRunning; bool m_bClear; - VoidFunction m_callback; + ListenerCallback m_callback; }; // String 类模板函数定义 diff --git a/core/emanagers.h b/core/emanagers.h index d7d1c5f0..48e8ef11 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -181,12 +181,12 @@ public: static void stopAll(); // 获取所有名称相同的动作 - static std::vector getActions( + static std::vector get( const String & strActionName ); // 获取所有动作 - static std::vector getAllActions(); + static std::vector getAll(); private: // 更新动画状态 @@ -300,6 +300,12 @@ class ShapeManager friend Node; friend Shape; +public: + // 开启或禁用碰撞触发 onCollide 函数 + static void setCollisionEnable( + bool bEnable + ); + private: // 更新形状 static void __updateShape( @@ -307,12 +313,12 @@ private: ); // 添加形状 - static void __addShape( + static void __add( Shape * pShape ); // 删除已绑定的形状 - static void __delShape( + static void __remove( Shape * pShape ); }; diff --git a/core/etools.h b/core/etools.h index 54e2ed4e..e310942a 100644 --- a/core/etools.h +++ b/core/etools.h @@ -47,18 +47,8 @@ class Timer : friend TimerManager; public: - Timer(); - Timer( - TimerCallback callback, /* 定时器回调函数 */ - double interval = 0, /* 时间间隔(秒) */ - int times = -1, /* 执行次数(设 -1 为永久执行) */ - bool atOnce = false, /* 是否立即执行 */ - bool autoRelease = false /* 自动清除 */ - ); - - Timer( - const String &name, /* 定时器名称 */ + const String &name = L"", /* 定时器名称 */ TimerCallback callback = nullptr, /* 定时器回调函数 */ double interval = 0, /* 时间间隔(秒) */ int times = -1, /* 执行次数(设 -1 为永久执行) */ diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index 218faad9..46d1bbc7 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -22,6 +22,7 @@ + @@ -75,9 +76,9 @@ - + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 4a916b8c..b9bb655d 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -36,6 +36,7 @@ + @@ -128,9 +129,6 @@ Manager - - Tool - Tool @@ -200,5 +198,8 @@ Common + + Tool + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 0c57d807..d3329b0c 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -214,9 +214,9 @@ - + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index dd2611c1..91ca1c0b 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -129,9 +129,6 @@ Manager - - Tool - Tool @@ -201,5 +198,8 @@ Common + + Tool + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 15de7b6a..97e6fea6 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -235,7 +235,7 @@ - + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 6116fceb..853d15b8 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -156,9 +156,6 @@ Tool - - Tool - Tool @@ -189,6 +186,9 @@ Common + + Tool +