ActionManager单例模式重做

This commit is contained in:
Nomango 2018-07-05 01:09:54 +08:00
parent 90d5196c4a
commit 58ae8dee20
7 changed files with 144 additions and 108 deletions

View File

@ -8,12 +8,12 @@ e2d::Action::Action()
, _target(nullptr) , _target(nullptr)
, _last(0) , _last(0)
{ {
ActionManager::__add(this); ActionManager::getInstance()->__add(this);
} }
e2d::Action::~Action() e2d::Action::~Action()
{ {
ActionManager::__remove(this); ActionManager::getInstance()->__remove(this);
} }
bool e2d::Action::isRunning() bool e2d::Action::isRunning()

View File

@ -1,5 +1,6 @@
#include "..\e2dbase.h" #include "..\e2dbase.h"
#include "..\e2dtool.h" #include "..\e2dtool.h"
#include "..\e2dmanager.h"
// GC 机制,用于自动销毁单例 // GC 机制,用于自动销毁单例
e2d::GC e2d::GC::_instance; e2d::GC e2d::GC::_instance;
@ -12,6 +13,7 @@ e2d::GC::~GC()
Input::destroyInstance(); Input::destroyInstance();
Window::destroyInstance(); Window::destroyInstance();
Player::destroyInstance(); Player::destroyInstance();
ActionManager::destroyInstance();
} }

View File

@ -43,6 +43,7 @@ void e2d::Game::start(bool cleanup)
auto input = Input::getInstance(); auto input = Input::getInstance();
auto window = Window::getInstance(); auto window = Window::getInstance();
auto renderer = Renderer::getInstance(); auto renderer = Renderer::getInstance();
auto actionManager = ActionManager::getInstance();
// 初始化场景管理器 // 初始化场景管理器
SceneManager::__init(); SceneManager::__init();
@ -69,7 +70,7 @@ void e2d::Game::start(bool cleanup)
{ {
input->__update(); // 获取用户输入 input->__update(); // 获取用户输入
Timer::__update(); // 更新定时器 Timer::__update(); // 更新定时器
ActionManager::__update(); // 更新动作管理器 actionManager->__update(); // 更新动作管理器
SceneManager::__update(); // 更新场景内容 SceneManager::__update(); // 更新场景内容
renderer->__render(); // 渲染游戏画面 renderer->__render(); // 渲染游戏画面
@ -109,7 +110,7 @@ void e2d::Game::reset()
if (!_ended) if (!_ended)
{ {
Time::__reset(); Time::__reset();
ActionManager::__resetAll(); ActionManager::getInstance()->__resetAll();
Timer::__resetAll(); Timer::__resetAll();
} }
} }
@ -149,8 +150,6 @@ void e2d::Game::cleanup()
Input::__clearListeners(); Input::__clearListeners();
// 删除碰撞监听器 // 删除碰撞监听器
Collision::__clearListeners(); Collision::__clearListeners();
// 删除动作
ActionManager::__uninit();
// 清空图片缓存 // 清空图片缓存
Image::clearCache(); Image::clearCache();
// 清空定时器 // 清空定时器

View File

@ -2,25 +2,52 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
static std::vector<e2d::Action*> s_vActions;
static std::vector<e2d::Action*> s_vRunningActions;
e2d::ActionManager * e2d::ActionManager::_instance = nullptr;
e2d::ActionManager * e2d::ActionManager::getInstance()
{
if (!_instance)
_instance = new (std::nothrow) ActionManager;
return _instance;
}
void e2d::ActionManager::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
}
}
e2d::ActionManager::ActionManager()
: _actions()
, _runningActions()
{
}
e2d::ActionManager::~ActionManager()
{
_actions.clear();
_runningActions.clear();
}
void e2d::ActionManager::__update() void e2d::ActionManager::__update()
{ {
if (s_vRunningActions.empty() || Game::getInstance()->isPaused()) if (_runningActions.empty() || Game::getInstance()->isPaused())
return; return;
// 循环遍历所有正在运行的动作 // 循环遍历所有正在运行的动作
for (size_t i = 0; i < s_vRunningActions.size(); ++i) for (size_t i = 0; i < _runningActions.size(); ++i)
{ {
auto action = s_vRunningActions[i]; auto action = _runningActions[i];
// 获取动作运行状态 // 获取动作运行状态
if (action->_isDone() || action->_target->getRefCount() == 0) if (action->_isDone() || action->_target->getRefCount() == 0)
{ {
action->release(); action->release();
action->_target = nullptr; action->_target = nullptr;
s_vRunningActions.erase(s_vRunningActions.begin() + i); _runningActions.erase(_runningActions.begin() + i);
} }
else else
{ {
@ -37,28 +64,32 @@ void e2d::ActionManager::__add(Action * action)
{ {
if (action) if (action)
{ {
s_vActions.push_back(action); auto iter = std::find(_actions.begin(), _actions.end(), action);
if (iter == _actions.end())
{
_actions.push_back(action);
}
} }
} }
void e2d::ActionManager::__remove(Action * action) void e2d::ActionManager::__remove(Action * action)
{ {
if (s_vActions.empty() || action == nullptr) if (_actions.empty() || action == nullptr)
return; return;
auto iter = std::find(s_vActions.begin(), s_vActions.end(), action); auto iter = std::find(_actions.begin(), _actions.end(), action);
if (iter != s_vActions.end()) if (iter != _actions.end())
{ {
s_vActions.erase(iter); _actions.erase(iter);
} }
} }
void e2d::ActionManager::__resumeAllBindedWith(Node * target) void e2d::ActionManager::resumeAllBindedWith(Node * target)
{ {
if (s_vRunningActions.empty() || target == nullptr) if (_runningActions.empty() || target == nullptr)
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getTarget() == target) if (action->getTarget() == target)
{ {
@ -67,12 +98,12 @@ void e2d::ActionManager::__resumeAllBindedWith(Node * target)
} }
} }
void e2d::ActionManager::__pauseAllBindedWith(Node * target) void e2d::ActionManager::pauseAllBindedWith(Node * target)
{ {
if (s_vRunningActions.empty() || target == nullptr) if (_runningActions.empty() || target == nullptr)
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getTarget() == target) if (action->getTarget() == target)
{ {
@ -81,12 +112,12 @@ void e2d::ActionManager::__pauseAllBindedWith(Node * target)
} }
} }
void e2d::ActionManager::__stopAllBindedWith(Node * target) void e2d::ActionManager::stopAllBindedWith(Node * target)
{ {
if (s_vRunningActions.empty() || target == nullptr) if (_runningActions.empty() || target == nullptr)
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getTarget() == target) if (action->getTarget() == target)
{ {
@ -104,13 +135,13 @@ void e2d::ActionManager::start(Action * action, Node * target, bool paused)
{ {
if (action->_target == nullptr) if (action->_target == nullptr)
{ {
auto iter = std::find(s_vRunningActions.begin(), s_vRunningActions.end(), action); auto iter = std::find(_runningActions.begin(), _runningActions.end(), action);
if (iter == s_vRunningActions.end()) if (iter == _runningActions.end())
{ {
action->_startWithTarget(target); action->_startWithTarget(target);
action->retain(); action->retain();
action->_running = !paused; action->_running = !paused;
s_vRunningActions.push_back(action); _runningActions.push_back(action);
} }
} }
else else
@ -122,10 +153,10 @@ void e2d::ActionManager::start(Action * action, Node * target, bool paused)
void e2d::ActionManager::resume(const String& name) void e2d::ActionManager::resume(const String& name)
{ {
if (s_vRunningActions.empty() || name.isEmpty()) if (_runningActions.empty() || name.isEmpty())
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getName() == name) if (action->getName() == name)
{ {
@ -136,10 +167,10 @@ void e2d::ActionManager::resume(const String& name)
void e2d::ActionManager::pause(const String& name) void e2d::ActionManager::pause(const String& name)
{ {
if (s_vRunningActions.empty() || name.isEmpty()) if (_runningActions.empty() || name.isEmpty())
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getName() == name) if (action->getName() == name)
{ {
@ -150,10 +181,10 @@ void e2d::ActionManager::pause(const String& name)
void e2d::ActionManager::stop(const String& name) void e2d::ActionManager::stop(const String& name)
{ {
if (s_vRunningActions.empty() || name.isEmpty()) if (_runningActions.empty() || name.isEmpty())
return; return;
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
if (action->getName() == name) if (action->getName() == name)
{ {
@ -162,17 +193,17 @@ void e2d::ActionManager::stop(const String& name)
} }
} }
void e2d::ActionManager::__clearAllBindedWith(Node * target) void e2d::ActionManager::clearAllBindedWith(Node * target)
{ {
if (target) if (target)
{ {
for (size_t i = 0; i < s_vRunningActions.size();) for (size_t i = 0; i < _runningActions.size();)
{ {
auto a = s_vRunningActions[i]; auto a = _runningActions[i];
if (a->getTarget() == target) if (a->getTarget() == target)
{ {
GC::release(a); GC::release(a);
s_vRunningActions.erase(s_vRunningActions.begin() + i); _runningActions.erase(_runningActions.begin() + i);
} }
else else
{ {
@ -182,21 +213,11 @@ void e2d::ActionManager::__clearAllBindedWith(Node * target)
} }
} }
void e2d::ActionManager::__uninit()
{
for (auto action : s_vRunningActions)
{
GC::release(action);
}
s_vActions.clear();
s_vRunningActions.clear();
}
void e2d::ActionManager::resumeAll() void e2d::ActionManager::resumeAll()
{ {
for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren()) for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren())
{ {
ActionManager::__resumeAllBindedWith(child); ActionManager::resumeAllBindedWith(child);
} }
} }
@ -204,7 +225,7 @@ void e2d::ActionManager::pauseAll()
{ {
for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren()) for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren())
{ {
ActionManager::__pauseAllBindedWith(child); ActionManager::pauseAllBindedWith(child);
} }
} }
@ -212,14 +233,14 @@ void e2d::ActionManager::stopAll()
{ {
for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren()) for (auto child : SceneManager::getCurrentScene()->getRoot()->getAllChildren())
{ {
ActionManager::__stopAllBindedWith(child); ActionManager::stopAllBindedWith(child);
} }
} }
std::vector<e2d::Action*> e2d::ActionManager::get(const String& name) std::vector<e2d::Action*> e2d::ActionManager::get(const String& name)
{ {
std::vector<Action*> actions; std::vector<Action*> actions;
for (auto action : s_vActions) for (auto action : _actions)
{ {
if (action->getName() == name) if (action->getName() == name)
{ {
@ -231,12 +252,12 @@ std::vector<e2d::Action*> e2d::ActionManager::get(const String& name)
const std::vector<e2d::Action*>& e2d::ActionManager::getAll() const std::vector<e2d::Action*>& e2d::ActionManager::getAll()
{ {
return s_vActions; return _actions;
} }
void e2d::ActionManager::__resetAll() void e2d::ActionManager::__resetAll()
{ {
for (auto action : s_vRunningActions) for (auto action : _runningActions)
{ {
action->_resetTime(); action->_resetTime();
} }

View File

@ -50,7 +50,7 @@ void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
} }
} }
void e2d::ColliderManager::__addCollider(Collider * pCollider) void e2d::ColliderManager::__add(Collider * pCollider)
{ {
if (pCollider) if (pCollider)
{ {
@ -59,7 +59,7 @@ void e2d::ColliderManager::__addCollider(Collider * pCollider)
} }
} }
void e2d::ColliderManager::__removeCollider(Collider * pCollider) void e2d::ColliderManager::__remove(Collider * pCollider)
{ {
if (pCollider) if (pCollider)
{ {

View File

@ -559,7 +559,7 @@ void e2d::Node::setColliderType(Collider::Type type)
default: default:
{ {
// 删除碰撞体 // 删除碰撞体
ColliderManager::__removeCollider(_collider); ColliderManager::__remove(_collider);
_collider = nullptr; _collider = nullptr;
} }
break; break;
@ -577,7 +577,7 @@ void e2d::Node::setColliderType(Collider::Type type)
this->_collider->_parentNode = this; this->_collider->_parentNode = this;
this->_collider->_recreate(type); this->_collider->_recreate(type);
// 添加新的碰撞体 // 添加新的碰撞体
ColliderManager::__addCollider(this->_collider); ColliderManager::__add(this->_collider);
} }
break; break;
@ -766,12 +766,12 @@ void e2d::Node::clearAllChildren()
void e2d::Node::runAction(Action * action) void e2d::Node::runAction(Action * action)
{ {
ActionManager::start(action, this, false); ActionManager::getInstance()->start(action, this, false);
} }
void e2d::Node::resumeAction(const String& name) void e2d::Node::resumeAction(const String& name)
{ {
auto& actions = ActionManager::get(name); auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions) for (auto action : actions)
{ {
if (action->getTarget() == this) if (action->getTarget() == this)
@ -783,7 +783,7 @@ void e2d::Node::resumeAction(const String& name)
void e2d::Node::pauseAction(const String& name) void e2d::Node::pauseAction(const String& name)
{ {
auto& actions = ActionManager::get(name); auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions) for (auto action : actions)
{ {
if (action->getTarget() == this) if (action->getTarget() == this)
@ -795,7 +795,7 @@ void e2d::Node::pauseAction(const String& name)
void e2d::Node::stopAction(const String& name) void e2d::Node::stopAction(const String& name)
{ {
auto& actions = ActionManager::get(name); auto& actions = ActionManager::getInstance()->get(name);
for (auto action : actions) for (auto action : actions)
{ {
if (action->getTarget() == this) if (action->getTarget() == this)
@ -910,8 +910,8 @@ void e2d::Node::setAutoUpdate(bool bAutoUpdate)
void e2d::Node::onDestroy() void e2d::Node::onDestroy()
{ {
ActionManager::__clearAllBindedWith(this); ActionManager::getInstance()->clearAllBindedWith(this);
ColliderManager::__removeCollider(_collider); ColliderManager::__remove(_collider);
for (auto child : _children) for (auto child : _children)
{ {
GC::release(child); GC::release(child);
@ -920,17 +920,17 @@ void e2d::Node::onDestroy()
void e2d::Node::resumeAllActions() void e2d::Node::resumeAllActions()
{ {
ActionManager::__resumeAllBindedWith(this); ActionManager::getInstance()->resumeAllBindedWith(this);
} }
void e2d::Node::pauseAllActions() void e2d::Node::pauseAllActions()
{ {
ActionManager::__pauseAllBindedWith(this); ActionManager::getInstance()->pauseAllBindedWith(this);
} }
void e2d::Node::stopAllActions() void e2d::Node::stopAllActions()
{ {
ActionManager::__stopAllBindedWith(this); ActionManager::getInstance()->stopAllBindedWith(this);
} }
void e2d::Node::setVisiable(bool value) void e2d::Node::setVisiable(bool value)

View File

@ -66,88 +66,102 @@ private:
class ActionManager class ActionManager
{ {
friend class Game; friend class Game;
friend class Node;
friend class Action; friend class Action;
public: public:
// 获取动作管理器实例
static ActionManager * getInstance();
// 销毁实例
static void destroyInstance();
// 获取所有名称相同的动作
std::vector<Action *> get(
const String& name
);
// 获取所有动作
const std::vector<Action*>& getAll();
// 执行动作 // 执行动作
static void start( void start(
Action * action, Action * action,
Node * target, Node * target,
bool paused bool paused
); );
// 继续名称相同的所有动作 // 继续名称相同的所有动作
static void resume( void resume(
const String& name const String& name
); );
// 暂停名称相同的所有动作 // 暂停名称相同的所有动作
static void pause( void pause(
const String& name const String& name
); );
// 停止名称相同的所有动作 // 停止名称相同的所有动作
static void stop( void stop(
const String& name const String& name
); );
// 继续所有动作 // 继续所有动作
static void resumeAll(); void resumeAll();
// 暂停所有动作 // 暂停所有动作
static void pauseAll(); void pauseAll();
// 停止所有动作 // 停止所有动作
static void stopAll(); void stopAll();
// 获取所有名称相同的动作
static std::vector<Action *> get(
const String& name
);
// 获取所有动作
static const std::vector<Action*>& getAll();
private:
// 更新动作状态
static void __update();
// 添加动作
static void __add(
Action * action
);
// 删除动作
static void __remove(
Action * action
);
// 继续绑定在节点上的所有动作 // 继续绑定在节点上的所有动作
static void __resumeAllBindedWith( void resumeAllBindedWith(
Node * target Node * target
); );
// 暂停绑定在节点上的所有动作 // 暂停绑定在节点上的所有动作
static void __pauseAllBindedWith( void pauseAllBindedWith(
Node * target Node * target
); );
// 停止绑定在节点上的所有动作 // 停止绑定在节点上的所有动作
static void __stopAllBindedWith( void stopAllBindedWith(
Node * target Node * target
); );
// 清空绑定在节点上的所有动作 // 清空绑定在节点上的所有动作
static void __clearAllBindedWith( void clearAllBindedWith(
Node * target Node * target
); );
// 重置所有动作状态 private:
static void __resetAll(); ActionManager();
// 回收资源 ~ActionManager();
static void __uninit();
E2D_DISABLE_COPY(ActionManager);
// 更新动作状态
void __update();
// 添加动作
void __add(
Action * action
);
// 删除动作
void __remove(
Action * action
);
// 重置所有动作状态
void __resetAll();
private:
std::vector<Action*> _actions;
std::vector<Action*> _runningActions;
static ActionManager * _instance;
}; };
@ -164,12 +178,12 @@ private:
); );
// 添加碰撞体 // 添加碰撞体
static void __addCollider( static void __add(
Collider * pCollider Collider * pCollider
); );
// 删除已绑定的碰撞体 // 删除已绑定的碰撞体
static void __removeCollider( static void __remove(
Collider * pCollider Collider * pCollider
); );
}; };