new attribute of Action: name

This commit is contained in:
Nomango 2018-03-06 09:56:17 +08:00
parent 97a7944087
commit 4ae567eab5
15 changed files with 349 additions and 159 deletions

View File

@ -1,4 +1,5 @@
#include "..\eactions.h"
#include "..\emanagers.h"
e2d::Action::Action()
: m_bRunning(false)
@ -8,10 +9,12 @@ e2d::Action::Action()
, m_pParentScene(nullptr)
, m_fLast(0)
{
ActionManager::__add(this);
}
e2d::Action::~Action()
{
ActionManager::__remove(this);
}
bool e2d::Action::isRunning()
@ -24,11 +27,14 @@ bool e2d::Action::_isEnding()
return m_bEnding;
}
void e2d::Action::startWith(Node* pTarget)
void e2d::Action::setTarget(Node* pTarget)
{
m_bRunning = true;
m_pTarget = pTarget;
this->reset();
if (pTarget)
{
m_bRunning = true;
m_pTarget = pTarget;
this->reset();
}
}
void e2d::Action::resume()
@ -47,6 +53,16 @@ void e2d::Action::stop()
m_bEnding = true;
}
e2d::String e2d::Action::getName() const
{
return m_sName;
}
void e2d::Action::setName(const String & name)
{
m_sName = name;
}
e2d::Action * e2d::Action::reverse() const
{
ASSERT(false, "Action cannot be reversed!");

View File

@ -1,4 +1,5 @@
#include "..\eactions.h"
#include "..\emanagers.h"
e2d::ActionLoop::ActionLoop(Action * action, int times /* = -1 */)
: m_pAction(action)

View File

@ -13,7 +13,7 @@ e2d::ActionSequence::ActionSequence(int number, Action * action1, ...) :
while (number > 0)
{
ASSERT((*ppAction) != nullptr, "ActionSequence NULL pointer exception!");
this->_add(*ppAction);
this->add(*ppAction);
ppAction++;
number--;
}
@ -81,7 +81,7 @@ void e2d::ActionSequence::_resetTime()
}
}
void e2d::ActionSequence::_add(Action * action)
void e2d::ActionSequence::add(Action * action)
{
if (action)
{
@ -95,7 +95,7 @@ e2d::ActionSequence * e2d::ActionSequence::clone() const
auto a = new ActionSequence();
for (auto action : m_vActions)
{
a->_add(action->clone());
a->add(action->clone());
}
return a;
}
@ -107,11 +107,11 @@ e2d::ActionSequence * e2d::ActionSequence::reverse(bool actionReverse) const
{
if (actionReverse)
{
a->_add(action->reverse());
a->add(action->reverse());
}
else
{
a->_add(action->clone());
a->add(action->clone());
}
}
// 将动作顺序逆序排列

View File

@ -165,7 +165,7 @@ void e2d::Game::uninit()
// 清空图片缓存
Image::clearCache();
// 刷新内存池
ObjectManager::__clearAllObjects();
ObjectManager::__uninit();
// 删除渲染相关资源
Renderer::__discardResources();
// 销毁窗口

View File

@ -66,9 +66,7 @@ bool Input::__init()
}
else
{
MessageBox(nullptr, L"Keyboard not found. The game will now exit.",
L"Error",
MB_ICONERROR | MB_OK);
MessageBox(nullptr, L"Keyboard not found!", L"Error", MB_ICONERROR | MB_OK);
Game::quit();
return false;
}
@ -88,10 +86,7 @@ bool Input::__init()
}
else
{
MessageBox(nullptr, L"Mouse not found. The game will now exit.",
L"Error",
MB_ICONERROR | MB_OK);
Game::quit();
MessageBox(nullptr, L"Mouse not found!", L"Error", MB_ICONERROR | MB_OK);
return false;
}
}
@ -133,7 +128,6 @@ void Input::__updateDeviceState()
s_MouseRecordState = s_MouseState;
s_MouseDevice->GetDeviceState(sizeof(s_MouseState), (void**)&s_MouseState);
}
DIK_0;
}
GetCursorPos(&s_MousePosition);

View File

@ -2,17 +2,73 @@
#include "..\eactions.h"
static std::vector<e2d::Action*> s_vActions;
static std::vector<e2d::Action*> s_vRunningActions;
void e2d::ActionManager::_add(Action * pAction, Node * pTargetNode)
void e2d::ActionManager::__update()
{
if (s_vRunningActions.empty() || Game::isPaused())
return;
// 循环遍历所有正在运行的动作
for (size_t i = 0; i < s_vRunningActions.size(); i++)
{
auto action = s_vRunningActions[i];
// 获取动作运行状态
if (action->isRunning())
{
if (!action->_isEnding())
{
// 执行动作
action->_update();
}
else
{
// 动作已经结束
action->release();
action->m_pTarget = nullptr;
s_vRunningActions.erase(s_vRunningActions.begin() + i);
}
}
}
}
void e2d::ActionManager::__add(Action * pAction)
{
if (pAction)
{
for (const auto action : s_vActions)
{
if (action == pAction)
{
WARN_IF(true, "ActionManager::add Failed!The action is already added.");
return;
}
}
s_vActions.push_back(pAction);
}
}
void e2d::ActionManager::__remove(Action * pAction)
{
for (size_t i = 0; i < s_vActions.size(); i++)
{
if (s_vActions[i] == pAction)
{
s_vActions.erase(s_vActions.begin() + i);
}
}
}
void e2d::ActionManager::__startAction(Action * pAction, Node * pTargetNode)
{
WARN_IF(pAction == nullptr, "Action NULL pointer exception!");
if (pAction)
{
pAction->startWith(pTargetNode);
pAction->setTarget(pTargetNode);
pAction->retain();
s_vActions.push_back(pAction);
s_vRunningActions.push_back(pAction);
}
}
@ -20,17 +76,13 @@ void e2d::ActionManager::resumeAllActionsBindedWith(Node * pTargetNode)
{
if (pTargetNode)
{
for (auto action : s_vActions)
for (auto action : s_vRunningActions)
{
if (action->getTarget() == pTargetNode)
{
action->resume();
}
}
for (auto child : pTargetNode->getChildren())
{
ActionManager::resumeAllActionsBindedWith(child);
}
}
}
@ -38,17 +90,13 @@ void e2d::ActionManager::pauseAllActionsBindedWith(Node * pTargetNode)
{
if (pTargetNode)
{
for (auto action : s_vActions)
for (auto action : s_vRunningActions)
{
if (action->getTarget() == pTargetNode)
{
action->pause();
}
}
for (auto child : pTargetNode->getChildren())
{
ActionManager::pauseAllActionsBindedWith(child);
}
}
}
@ -56,16 +104,45 @@ void e2d::ActionManager::stopAllActionsBindedWith(Node * pTargetNode)
{
if (pTargetNode)
{
for (auto action : s_vActions)
for (auto action : s_vRunningActions)
{
if (action->getTarget() == pTargetNode)
{
action->stop();
}
}
for (auto child : pTargetNode->getChildren())
}
}
void e2d::ActionManager::resumeAllActions(const String & strActionName)
{
for (auto action : s_vRunningActions)
{
if (action->getName() == strActionName)
{
ActionManager::stopAllActionsBindedWith(child);
action->resume();
}
}
}
void e2d::ActionManager::pauseAllActions(const String & strActionName)
{
for (auto action : s_vRunningActions)
{
if (action->getName() == strActionName)
{
action->pause();
}
}
}
void e2d::ActionManager::stopAllActions(const String & strActionName)
{
for (auto action : s_vRunningActions)
{
if (action->getName() == strActionName)
{
action->stop();
}
}
}
@ -74,13 +151,13 @@ void e2d::ActionManager::__clearAllActionsBindedWith(Node * pTargetNode)
{
if (pTargetNode)
{
for (size_t i = 0; i < s_vActions.size();)
for (size_t i = 0; i < s_vRunningActions.size();)
{
auto a = s_vActions[i];
auto a = s_vRunningActions[i];
if (a->getTarget() == pTargetNode)
{
SafeRelease(&a);
s_vActions.erase(s_vActions.begin() + i);
s_vRunningActions.erase(s_vRunningActions.begin() + i);
}
else
{
@ -114,40 +191,28 @@ void e2d::ActionManager::stopAllActions()
}
}
std::vector<e2d::Action*> e2d::ActionManager::getActions(const String & strActionName)
{
std::vector<Action*> vActions;
for (const auto action : s_vActions)
{
if (action->getName() == strActionName)
{
vActions.push_back(action);
}
}
return std::move(vActions);
}
std::vector<e2d::Action*> e2d::ActionManager::getAllActions()
{
return s_vActions;
}
void e2d::ActionManager::__resetAllActions()
{
for (auto action : s_vActions)
for (auto action : s_vRunningActions)
{
action->_resetTime();
}
}
void e2d::ActionManager::__update()
{
if (s_vActions.empty() || Game::isPaused())
return;
// 循环遍历所有正在运行的动作
for (size_t i = 0; i < s_vActions.size(); i++)
{
auto &action = s_vActions[i];
// 获取动作运行状态
if (action->isRunning() &&
action->getTarget() &&
action->getTarget()->getParentScene() == SceneManager::getCurrentScene())
{
if (!action->_isEnding())
{
// 执行动作
action->_update();
}
else
{
// 动作已经结束
action->release();
action->m_pTarget = nullptr;
s_vActions.erase(s_vActions.begin() + i);
}
}
}
}

View File

@ -37,13 +37,14 @@ void e2d::ObjectManager::__update()
}
}
void e2d::ObjectManager::__clearAllObjects()
void e2d::ObjectManager::__uninit()
{
for (const auto &obj : s_vObjectPool)
{
delete obj;
}
s_vObjectPool.clear();
// ÊÍ·ÅÁ½±éÄÚ´æ
s_bNotifyed = true;
ObjectManager::__update();
s_bNotifyed = true;
ObjectManager::__update();
}
void e2d::ObjectManager::add(e2d::Object * nptr)

View File

@ -10,10 +10,9 @@ void e2d::TimerManager::__update()
if (s_vTimers.empty() || Game::isPaused())
return;
std::vector<Timer*>::iterator mIter;
for (mIter = s_vTimers.begin(); mIter != s_vTimers.end();)
for (size_t i = 0; i < s_vTimers.size(); i++)
{
Timer * pTimer = (*mIter);
auto pTimer = s_vTimers[i];
// ¸üж¨Ê±Æ÷
if (pTimer->isReady())
{
@ -23,22 +22,19 @@ void e2d::TimerManager::__update()
if (pTimer->m_bClear)
{
pTimer->release();
mIter = s_vTimers.erase(mIter);
}
else
{
mIter++;
s_vTimers.erase(s_vTimers.begin() + i);
i--;
}
}
}
void e2d::TimerManager::add(double timeOut, TimerCallback callback)
void e2d::TimerManager::start(double timeOut, TimerCallback callback)
{
auto pTimer = new Timer(callback, timeOut, 1, false, true);
pTimer->start();
}
void e2d::TimerManager::add(Timer * pTimer)
void e2d::TimerManager::__add(Timer * pTimer)
{
WARN_IF(pTimer == nullptr, "Timer NULL pointer exception!");
@ -100,18 +96,6 @@ void e2d::TimerManager::stopAndClear(const String & name)
}
}
e2d::Timer * e2d::TimerManager::get(const String & name)
{
for (auto timer : s_vTimers)
{
if (timer->getName() == name)
{
return timer;
}
}
return nullptr;
}
std::vector<e2d::Timer*> e2d::TimerManager::getTimers(const String & name)
{
std::vector<Timer*> vTimers;

View File

@ -750,7 +750,7 @@ void e2d::Node::runAction(Action * action)
{
action = action->clone();
}
ActionManager::_add(action, this);
ActionManager::__startAction(action, this);
}
else
{
@ -758,22 +758,73 @@ void e2d::Node::runAction(Action * action)
}
}
void e2d::Node::resumeAction(Action * action)
void e2d::Node::resumeAction(const String & strActionName)
{
if (action->getTarget() == this)
auto actions = ActionManager::getActions(strActionName);
for (auto action : actions)
{
action->resume();
if (action->getTarget() == this)
{
action->resume();
}
}
}
void e2d::Node::pauseAction(Action * action)
void e2d::Node::pauseAction(const String & strActionName)
{
if (action->getTarget() == this)
auto actions = ActionManager::getActions(strActionName);
for (auto action : actions)
{
action->pause();
if (action->getTarget() == this)
{
action->pause();
}
}
}
void e2d::Node::stopAction(const String & strActionName)
{
auto actions = ActionManager::getActions(strActionName);
for (auto action : actions)
{
if (action->getTarget() == this)
{
action->stop();
}
}
}
e2d::Action * e2d::Node::getAction(const String & strActionName)
{
auto actions = ActionManager::getActions(strActionName);
for (auto action : actions)
{
if (action->getTarget() == this)
{
return action;
}
}
return nullptr;
}
std::vector<e2d::Action*> e2d::Node::getActions(const String & strActionName)
{
std::vector<Action*>::iterator iter;
auto actions = ActionManager::getActions(strActionName);
for (iter = actions.begin(); iter != actions.end();)
{
if ((*iter)->getTarget() != this)
{
iter = actions.erase(iter);
}
else
{
iter++;
}
}
return std::move(actions);
}
bool e2d::Node::isPointIn(Point point)
{
if (m_bTransformNeeded)
@ -808,6 +859,39 @@ bool e2d::Node::isPointIn(Point point)
return false;
}
bool e2d::Node::isIntersectWith(Node * pNode) const
{
ID2D1RectangleGeometry * pRect1;
ID2D1RectangleGeometry * pRect2;
ID2D1TransformedGeometry * pShape;
D2D1_GEOMETRY_RELATION relation;
// 根据自身大小位置创建矩形
Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, m_fWidth * m_fScaleX, m_fHeight * m_fScaleY),
&pRect1
);
// 根据二维矩阵进行转换
Renderer::getID2D1Factory()->CreateTransformedGeometry(
pRect1,
this->m_MatriFinal,
&pShape
);
// 根据相比较节点的大小位置创建矩形
Renderer::getID2D1Factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, pNode->m_fWidth * pNode->m_fScaleX, pNode->m_fHeight * pNode->m_fScaleY),
&pRect2
);
// 获取相交状态
pShape->CompareWithGeometry(
pRect2,
pNode->m_MatriFinal,
&relation
);
return (relation != D2D1_GEOMETRY_RELATION::D2D1_GEOMETRY_RELATION_UNKNOWN) &&
(relation != D2D1_GEOMETRY_RELATION::D2D1_GEOMETRY_RELATION_DISJOINT);
}
void e2d::Node::setAutoUpdate(bool bAutoUpdate)
{
m_bAutoUpdate = bAutoUpdate;
@ -819,14 +903,6 @@ void e2d::Node::setDefaultPiovt(double defaultPiovtX, double defaultPiovtY)
s_fDefaultPiovtY = min(max(static_cast<float>(defaultPiovtY), 0), 1);
}
void e2d::Node::stopAction(Action * action)
{
if (action->getTarget() == this)
{
action->stop();
}
}
void e2d::Node::resumeAllActions()
{
ActionManager::resumeAllActionsBindedWith(this);

View File

@ -13,7 +13,7 @@ e2d::Timer::Timer()
, m_bAutoRelease(false)
, m_bClear(false)
{
TimerManager::add(this);
TimerManager::__add(this);
}
e2d::Timer::Timer(const TimerCallback & callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */)
@ -32,7 +32,7 @@ e2d::Timer::Timer(const TimerCallback & callback, double interval /* = 0 */, int
this->setInterval(interval);
m_bAutoRelease = autoRelease;
m_bAtOnce = atOnce;
TimerManager::add(this);
TimerManager::__add(this);
}
e2d::Timer::Timer(const String & name, const TimerCallback & callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */)
@ -52,7 +52,7 @@ e2d::Timer::Timer(const String & name, const TimerCallback & callback, double in
this->setInterval(interval);
m_bAutoRelease = autoRelease;
m_bAtOnce = atOnce;
TimerManager::add(this);
TimerManager::__add(this);
}
bool e2d::Timer::isRunning() const

View File

@ -30,7 +30,7 @@ public:
virtual bool isRunning();
// 开始动作
virtual void startWith(
virtual void setTarget(
Node* pTarget /* 执行该动作的目标 */
);
@ -43,6 +43,14 @@ public:
// 停止动作
virtual void stop();
// 获取动作名称
virtual String getName() const;
// 设置动作名称
virtual void setName(
const String &name
);
// 获取一个新的拷贝动作
virtual Action * clone() const = 0;
@ -69,11 +77,12 @@ protected:
virtual void _resetTime();
protected:
String m_sName;
bool m_bRunning;
bool m_bEnding;
bool m_bInit;
Node * m_pTarget;
Scene *m_pParentScene;
Scene * m_pParentScene;
double m_fLast;
};
@ -106,7 +115,7 @@ class ActionMoveBy :
public:
// 创建相对位移动画
ActionMoveBy(
double duration, /* 动画持续时长 */
double duration, /* 动画持续时长 */
Vector vector /* 位移向量 */
);
@ -135,8 +144,8 @@ class ActionMoveTo :
public:
// 创建位移动画
ActionMoveTo(
double duration, /* 动画持续时长 */
Point pos /* 位移至目标点的坐标 */
double duration, /* 动画持续时长 */
Point pos /* 位移至目标点的坐标 */
);
// 获取该动画的拷贝对象
@ -157,15 +166,15 @@ class ActionScaleBy :
public:
// 创建相对缩放动画
ActionScaleBy(
double duration, /* 动画持续时长 */
double duration, /* 动画持续时长 */
double scale /* 缩放比例变化 */
);
// 创建相对缩放动画
ActionScaleBy(
double duration, /* 动画持续时长 */
double scaleX, /* 横向缩放比例变化 */
double scaleY /* 纵向缩放比例变化 */
double duration, /* 动画持续时长 */
double scaleX, /* 横向缩放比例变化 */
double scaleY /* 纵向缩放比例变化 */
);
// 获取该动画的拷贝对象
@ -195,15 +204,15 @@ class ActionScaleTo :
public:
// 创建缩放动画
ActionScaleTo(
double duration, /* 动画持续时长 */
double duration, /* 动画持续时长 */
double scale /* 缩放至目标比例 */
);
// 创建缩放动画
ActionScaleTo(
double duration, /* 动画持续时长 */
double scaleX, /* 横向缩放至目标比例 */
double scaleY /* 纵向缩放至目标比例 */
double duration, /* 动画持续时长 */
double scaleX, /* 横向缩放至目标比例 */
double scaleY /* 纵向缩放至目标比例 */
);
// 获取该动画的拷贝对象
@ -225,8 +234,8 @@ class ActionOpacityBy :
public:
// 创建透明度相对渐变动画
ActionOpacityBy(
double duration, /* 动画持续时长 */
double opacity /* 透明度相对变化值 */
double duration, /* 动画持续时长 */
double opacity /* 透明度相对变化值 */
);
// 获取该动画的拷贝对象
@ -255,7 +264,7 @@ public:
// 创建透明度渐变动画
ActionOpacityTo(
double duration, /* 动画持续时长 */
double opacity /* 透明度渐变至目标值 */
double opacity /* 透明度渐变至目标值 */
);
// 获取该动画的拷贝对象
@ -299,7 +308,7 @@ public:
// 创建相对旋转动画
ActionRotateBy(
double duration, /* 动画持续时长 */
double rotation /* 旋转角度变化值 */
double rotation /* 旋转角度变化值 */
);
// 获取该动画的拷贝对象
@ -328,7 +337,7 @@ public:
// 创建旋转动画
ActionRotateTo(
double duration, /* 动画持续时长 */
double rotation /* 旋转角度至目标值 */
double rotation /* 旋转角度至目标值 */
);
// 获取该动画的拷贝对象
@ -401,7 +410,7 @@ public:
virtual ~ActionSequence();
// 向顺序动作中添加动作
void _add(
void add(
Action * action /* 将动作添加至顺序动作尾部 */
);

View File

@ -35,7 +35,7 @@ private:
static void __update();
// 清空所有对象
static void __clearAllObjects();
static void __uninit();
};
@ -87,15 +87,11 @@ class TimerManager
{
friend Game;
friend Node;
friend Timer;
public:
// 添加一个定时器
static void add(
Timer * pTimer
);
// 等待一段时间后执行指定函数
static void add(
static void start(
double timeOut, /* 等待的时长(秒) */
TimerCallback callback /* 执行的函数 */
);
@ -115,11 +111,6 @@ public:
const String &name
);
// 获取名称相同的定时器(有多个时返回第一个)
static Timer * get(
const String & name
);
// 获取名称相同的定时器
static std::vector<Timer*> getTimers(
const String & name
@ -141,6 +132,11 @@ private:
// 更新定时器
static void __update();
// 添加一个定时器
static void __add(
Timer * pTimer
);
// 重置定时器状态
static void __resetAllTimers();
@ -157,6 +153,21 @@ class ActionManager
friend Action;
public:
// 继续名称相同的所有动作
static void resumeAllActions(
const String & strActionName
);
// 暂停名称相同的所有动作
static void pauseAllActions(
const String & strActionName
);
// 停止名称相同的所有动作
static void stopAllActions(
const String & strActionName
);
// 继续绑定在节点上的所有动作
static void resumeAllActionsBindedWith(
Node * pTargetNode
@ -181,16 +192,34 @@ public:
// 停止所有动作
static void stopAllActions();
// 获取所有名称相同的动作
static std::vector<Action *> getActions(
const String & strActionName
);
// 获取所有动作
static std::vector<Action*> getAllActions();
private:
// 更新动画状态
static void __update();
// 添加动作
static void _add(
static void __add(
Action * pAction
);
// 删除动作
static void __remove(
Action * pAction
);
// 执行动作
static void __startAction(
Action * pAction,
Node * pTargetNode
);
// 更新动画状态
static void __update();
// 清空绑定在节点上的所有动作
static void __clearAllActionsBindedWith(
Node * pTargetNode

View File

@ -53,6 +53,11 @@ public:
Point point
);
// 判断两节点是否相交
virtual bool isIntersectWith(
Node * pNode
) const;
// 获取节点名称
virtual String getName() const;
@ -328,17 +333,27 @@ public:
// 继续动画
virtual void resumeAction(
Action * action
const String & strActionName
);
// 暂停动画
virtual void pauseAction(
Action * action
const String & strActionName
);
// 停止动画
virtual void stopAction(
Action * action
const String & strActionName
);
// 获取名称相同的动画
virtual Action * getAction(
const String & strActionName
);
// 获取所有名称相同的动画
virtual std::vector<Action*> getActions(
const String & strActionName
);
// 继续所有暂停动画

View File

@ -58,12 +58,12 @@ public:
);
Timer(
const String &name, /* 定时器名称 */
const TimerCallback &callback, /* 定时器回调函数 */
double interval = 0, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */
bool atOnce = false, /* 是否立即执行 */
bool autoRelease = false /* 自动清除 */
const String &name, /* 定时器名称 */
const TimerCallback &callback = nullptr, /* 定时器回调函数 */
double interval = 0, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */
bool atOnce = false, /* 是否立即执行 */
bool autoRelease = false /* 自动清除 */
);
// 启动定时器

View File

@ -101,7 +101,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>None</DebugInformationFormat>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>