Collision detection will not be opened by default, and use ShapeManager::setCollisionEnable to enable it.

This commit is contained in:
Nomango 2018-03-13 13:13:26 +08:00
parent 6909067a60
commit 0a80483ba6
18 changed files with 68 additions and 86 deletions

View File

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

View File

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

View File

@ -191,7 +191,7 @@ void e2d::ActionManager::stopAll()
}
}
std::vector<e2d::Action*> e2d::ActionManager::getActions(const String & strActionName)
std::vector<e2d::Action*> e2d::ActionManager::get(const String & strActionName)
{
std::vector<Action*> vActions;
for (const auto action : s_vActions)
@ -204,7 +204,7 @@ std::vector<e2d::Action*> e2d::ActionManager::getActions(const String & strActio
return std::move(vActions);
}
std::vector<e2d::Action*> e2d::ActionManager::getAllActions()
std::vector<e2d::Action*> e2d::ActionManager::getAll()
{
return s_vActions;
}

View File

@ -3,19 +3,35 @@
#include "..\eshape.h"
// 形状集合
std::vector<e2d::Shape*> s_vShapes;
static std::vector<e2d::Shape*> 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)
{

View File

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

View File

@ -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::Action*> e2d::Node::getActions(const String & strActionName)
{
std::vector<Action*>::iterator iter;
auto actions = ActionManager::getActions(strActionName);
auto actions = ActionManager::get(strActionName);
for (iter = actions.begin(); iter != actions.end();)
{
if ((*iter)->getTarget() != this)

View File

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

View File

@ -153,7 +153,7 @@ class Input
public:
// 添加输入监听
static void add(
VoidFunction callback, /* 回调函数 */
ListenerCallback callback, /* 回调函数 */
const String & name = L"" /* 监听器名称 */
);

View File

@ -12,6 +12,9 @@ namespace e2d
// 返回值和参数列表都为空的函数
typedef std::function<void(void)> 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 类模板函数定义

View File

@ -181,12 +181,12 @@ public:
static void stopAll();
// 获取所有名称相同的动作
static std::vector<Action *> getActions(
static std::vector<Action *> get(
const String & strActionName
);
// 获取所有动作
static std::vector<Action*> getAllActions();
static std::vector<Action*> 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
);
};

View File

@ -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 为永久执行) */

View File

@ -22,6 +22,7 @@
<ClInclude Include="..\..\core\eactions.h" />
<ClInclude Include="..\..\core\easy2d.h" />
<ClInclude Include="..\..\core\ebase.h" />
<ClInclude Include="..\..\core\ecommon.h" />
<ClInclude Include="..\..\core\emacros.h" />
<ClInclude Include="..\..\core\emanagers.h" />
<ClInclude Include="..\..\core\enodes.h" />
@ -75,9 +76,9 @@
<ClCompile Include="..\..\core\Shape\Ellipse.cpp" />
<ClCompile Include="..\..\core\Shape\Rect.cpp" />
<ClCompile Include="..\..\core\Shape\Shape.cpp" />
<ClCompile Include="..\..\core\Tool\Data.cpp" />
<ClCompile Include="..\..\core\Tool\File.cpp" />
<ClCompile Include="..\..\core\Tool\Music.cpp" />
<ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" />
<ClCompile Include="..\..\core\Tool\Timer.cpp" />
<ClCompile Include="..\..\core\Transition\ETransition.cpp" />

View File

@ -36,6 +36,7 @@
<ClInclude Include="..\..\core\etools.h" />
<ClInclude Include="..\..\core\etransitions.h" />
<ClInclude Include="..\..\core\eshape.h" />
<ClInclude Include="..\..\core\ecommon.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\core\Action\Action.cpp">
@ -128,9 +129,6 @@
<ClCompile Include="..\..\core\Manager\TimerManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Data.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Music.cpp">
<Filter>Tool</Filter>
</ClCompile>
@ -200,5 +198,8 @@
<ClCompile Include="..\..\core\Common\Listener.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Path.cpp">
<Filter>Tool</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -214,9 +214,9 @@
<ClCompile Include="..\..\core\Shape\Ellipse.cpp" />
<ClCompile Include="..\..\core\Shape\Rect.cpp" />
<ClCompile Include="..\..\core\Shape\Shape.cpp" />
<ClCompile Include="..\..\core\Tool\Data.cpp" />
<ClCompile Include="..\..\core\Tool\File.cpp" />
<ClCompile Include="..\..\core\Tool\Music.cpp" />
<ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" />
<ClCompile Include="..\..\core\Tool\Timer.cpp" />
<ClCompile Include="..\..\core\Transition\ETransition.cpp" />

View File

@ -129,9 +129,6 @@
<ClCompile Include="..\..\core\Manager\TimerManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Data.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Music.cpp">
<Filter>Tool</Filter>
</ClCompile>
@ -201,5 +198,8 @@
<ClCompile Include="..\..\core\Common\Listener.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Path.cpp">
<Filter>Tool</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -235,7 +235,7 @@
<ClCompile Include="..\..\core\Shape\Ellipse.cpp" />
<ClCompile Include="..\..\core\Shape\Rect.cpp" />
<ClCompile Include="..\..\core\Shape\Shape.cpp" />
<ClCompile Include="..\..\core\Tool\Data.cpp" />
<ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\File.cpp" />
<ClCompile Include="..\..\core\Tool\Music.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" />

View File

@ -156,9 +156,6 @@
<ClCompile Include="..\..\core\Tool\Music.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Data.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\File.cpp">
<Filter>Tool</Filter>
</ClCompile>
@ -189,6 +186,9 @@
<ClCompile Include="..\..\core\Common\Listener.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Path.cpp">
<Filter>Tool</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\etools.h" />