ActionManager单例模式重做
This commit is contained in:
parent
90d5196c4a
commit
58ae8dee20
|
|
@ -8,12 +8,12 @@ e2d::Action::Action()
|
|||
, _target(nullptr)
|
||||
, _last(0)
|
||||
{
|
||||
ActionManager::__add(this);
|
||||
ActionManager::getInstance()->__add(this);
|
||||
}
|
||||
|
||||
e2d::Action::~Action()
|
||||
{
|
||||
ActionManager::__remove(this);
|
||||
ActionManager::getInstance()->__remove(this);
|
||||
}
|
||||
|
||||
bool e2d::Action::isRunning()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "..\e2dbase.h"
|
||||
#include "..\e2dtool.h"
|
||||
#include "..\e2dmanager.h"
|
||||
|
||||
// GC 机制,用于自动销毁单例
|
||||
e2d::GC e2d::GC::_instance;
|
||||
|
|
@ -12,6 +13,7 @@ e2d::GC::~GC()
|
|||
Input::destroyInstance();
|
||||
Window::destroyInstance();
|
||||
Player::destroyInstance();
|
||||
ActionManager::destroyInstance();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ void e2d::Game::start(bool cleanup)
|
|||
auto input = Input::getInstance();
|
||||
auto window = Window::getInstance();
|
||||
auto renderer = Renderer::getInstance();
|
||||
auto actionManager = ActionManager::getInstance();
|
||||
|
||||
// 初始化场景管理器
|
||||
SceneManager::__init();
|
||||
|
|
@ -69,7 +70,7 @@ void e2d::Game::start(bool cleanup)
|
|||
{
|
||||
input->__update(); // 获取用户输入
|
||||
Timer::__update(); // 更新定时器
|
||||
ActionManager::__update(); // 更新动作管理器
|
||||
actionManager->__update(); // 更新动作管理器
|
||||
SceneManager::__update(); // 更新场景内容
|
||||
renderer->__render(); // 渲染游戏画面
|
||||
|
||||
|
|
@ -109,7 +110,7 @@ void e2d::Game::reset()
|
|||
if (!_ended)
|
||||
{
|
||||
Time::__reset();
|
||||
ActionManager::__resetAll();
|
||||
ActionManager::getInstance()->__resetAll();
|
||||
Timer::__resetAll();
|
||||
}
|
||||
}
|
||||
|
|
@ -149,8 +150,6 @@ void e2d::Game::cleanup()
|
|||
Input::__clearListeners();
|
||||
// 删除碰撞监听器
|
||||
Collision::__clearListeners();
|
||||
// 删除动作
|
||||
ActionManager::__uninit();
|
||||
// 清空图片缓存
|
||||
Image::clearCache();
|
||||
// 清空定时器
|
||||
|
|
|
|||
|
|
@ -2,25 +2,52 @@
|
|||
#include "..\e2daction.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()
|
||||
{
|
||||
if (s_vRunningActions.empty() || Game::getInstance()->isPaused())
|
||||
if (_runningActions.empty() || Game::getInstance()->isPaused())
|
||||
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)
|
||||
{
|
||||
action->release();
|
||||
action->_target = nullptr;
|
||||
s_vRunningActions.erase(s_vRunningActions.begin() + i);
|
||||
_runningActions.erase(_runningActions.begin() + i);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -37,28 +64,32 @@ void e2d::ActionManager::__add(Action * 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)
|
||||
{
|
||||
if (s_vActions.empty() || action == nullptr)
|
||||
if (_actions.empty() || action == nullptr)
|
||||
return;
|
||||
|
||||
auto iter = std::find(s_vActions.begin(), s_vActions.end(), action);
|
||||
if (iter != s_vActions.end())
|
||||
auto iter = std::find(_actions.begin(), _actions.end(), action);
|
||||
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;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
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;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
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;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
if (action->getTarget() == target)
|
||||
{
|
||||
|
|
@ -104,13 +135,13 @@ void e2d::ActionManager::start(Action * action, Node * target, bool paused)
|
|||
{
|
||||
if (action->_target == nullptr)
|
||||
{
|
||||
auto iter = std::find(s_vRunningActions.begin(), s_vRunningActions.end(), action);
|
||||
if (iter == s_vRunningActions.end())
|
||||
auto iter = std::find(_runningActions.begin(), _runningActions.end(), action);
|
||||
if (iter == _runningActions.end())
|
||||
{
|
||||
action->_startWithTarget(target);
|
||||
action->retain();
|
||||
action->_running = !paused;
|
||||
s_vRunningActions.push_back(action);
|
||||
_runningActions.push_back(action);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -122,10 +153,10 @@ void e2d::ActionManager::start(Action * action, Node * target, bool paused)
|
|||
|
||||
void e2d::ActionManager::resume(const String& name)
|
||||
{
|
||||
if (s_vRunningActions.empty() || name.isEmpty())
|
||||
if (_runningActions.empty() || name.isEmpty())
|
||||
return;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
if (action->getName() == name)
|
||||
{
|
||||
|
|
@ -136,10 +167,10 @@ void e2d::ActionManager::resume(const String& name)
|
|||
|
||||
void e2d::ActionManager::pause(const String& name)
|
||||
{
|
||||
if (s_vRunningActions.empty() || name.isEmpty())
|
||||
if (_runningActions.empty() || name.isEmpty())
|
||||
return;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
if (action->getName() == name)
|
||||
{
|
||||
|
|
@ -150,10 +181,10 @@ void e2d::ActionManager::pause(const String& name)
|
|||
|
||||
void e2d::ActionManager::stop(const String& name)
|
||||
{
|
||||
if (s_vRunningActions.empty() || name.isEmpty())
|
||||
if (_runningActions.empty() || name.isEmpty())
|
||||
return;
|
||||
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
GC::release(a);
|
||||
s_vRunningActions.erase(s_vRunningActions.begin() + i);
|
||||
_runningActions.erase(_runningActions.begin() + i);
|
||||
}
|
||||
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()
|
||||
{
|
||||
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())
|
||||
{
|
||||
ActionManager::__pauseAllBindedWith(child);
|
||||
ActionManager::pauseAllBindedWith(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -212,14 +233,14 @@ void e2d::ActionManager::stopAll()
|
|||
{
|
||||
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<Action*> actions;
|
||||
for (auto action : s_vActions)
|
||||
for (auto action : _actions)
|
||||
{
|
||||
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()
|
||||
{
|
||||
return s_vActions;
|
||||
return _actions;
|
||||
}
|
||||
|
||||
void e2d::ActionManager::__resetAll()
|
||||
{
|
||||
for (auto action : s_vRunningActions)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
action->_resetTime();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -59,7 +59,7 @@ void e2d::ColliderManager::__addCollider(Collider * pCollider)
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::ColliderManager::__removeCollider(Collider * pCollider)
|
||||
void e2d::ColliderManager::__remove(Collider * pCollider)
|
||||
{
|
||||
if (pCollider)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -559,7 +559,7 @@ void e2d::Node::setColliderType(Collider::Type type)
|
|||
default:
|
||||
{
|
||||
// 删除碰撞体
|
||||
ColliderManager::__removeCollider(_collider);
|
||||
ColliderManager::__remove(_collider);
|
||||
_collider = nullptr;
|
||||
}
|
||||
break;
|
||||
|
|
@ -577,7 +577,7 @@ void e2d::Node::setColliderType(Collider::Type type)
|
|||
this->_collider->_parentNode = this;
|
||||
this->_collider->_recreate(type);
|
||||
// 添加新的碰撞体
|
||||
ColliderManager::__addCollider(this->_collider);
|
||||
ColliderManager::__add(this->_collider);
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -766,12 +766,12 @@ void e2d::Node::clearAllChildren()
|
|||
|
||||
void e2d::Node::runAction(Action * action)
|
||||
{
|
||||
ActionManager::start(action, this, false);
|
||||
ActionManager::getInstance()->start(action, this, false);
|
||||
}
|
||||
|
||||
void e2d::Node::resumeAction(const String& name)
|
||||
{
|
||||
auto& actions = ActionManager::get(name);
|
||||
auto& actions = ActionManager::getInstance()->get(name);
|
||||
for (auto action : actions)
|
||||
{
|
||||
if (action->getTarget() == this)
|
||||
|
|
@ -783,7 +783,7 @@ void e2d::Node::resumeAction(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)
|
||||
{
|
||||
if (action->getTarget() == this)
|
||||
|
|
@ -795,7 +795,7 @@ void e2d::Node::pauseAction(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)
|
||||
{
|
||||
if (action->getTarget() == this)
|
||||
|
|
@ -910,8 +910,8 @@ void e2d::Node::setAutoUpdate(bool bAutoUpdate)
|
|||
|
||||
void e2d::Node::onDestroy()
|
||||
{
|
||||
ActionManager::__clearAllBindedWith(this);
|
||||
ColliderManager::__removeCollider(_collider);
|
||||
ActionManager::getInstance()->clearAllBindedWith(this);
|
||||
ColliderManager::__remove(_collider);
|
||||
for (auto child : _children)
|
||||
{
|
||||
GC::release(child);
|
||||
|
|
@ -920,17 +920,17 @@ void e2d::Node::onDestroy()
|
|||
|
||||
void e2d::Node::resumeAllActions()
|
||||
{
|
||||
ActionManager::__resumeAllBindedWith(this);
|
||||
ActionManager::getInstance()->resumeAllBindedWith(this);
|
||||
}
|
||||
|
||||
void e2d::Node::pauseAllActions()
|
||||
{
|
||||
ActionManager::__pauseAllBindedWith(this);
|
||||
ActionManager::getInstance()->pauseAllBindedWith(this);
|
||||
}
|
||||
|
||||
void e2d::Node::stopAllActions()
|
||||
{
|
||||
ActionManager::__stopAllBindedWith(this);
|
||||
ActionManager::getInstance()->stopAllBindedWith(this);
|
||||
}
|
||||
|
||||
void e2d::Node::setVisiable(bool value)
|
||||
|
|
|
|||
|
|
@ -66,88 +66,102 @@ private:
|
|||
class ActionManager
|
||||
{
|
||||
friend class Game;
|
||||
friend class Node;
|
||||
friend class Action;
|
||||
|
||||
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,
|
||||
Node * target,
|
||||
bool paused
|
||||
);
|
||||
|
||||
// 继续名称相同的所有动作
|
||||
static void resume(
|
||||
void resume(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 暂停名称相同的所有动作
|
||||
static void pause(
|
||||
void pause(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 停止名称相同的所有动作
|
||||
static void stop(
|
||||
void stop(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 继续所有动作
|
||||
static void resumeAll();
|
||||
void resumeAll();
|
||||
|
||||
// 暂停所有动作
|
||||
static void pauseAll();
|
||||
void pauseAll();
|
||||
|
||||
// 停止所有动作
|
||||
static 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
|
||||
);
|
||||
void stopAll();
|
||||
|
||||
// 继续绑定在节点上的所有动作
|
||||
static void __resumeAllBindedWith(
|
||||
void resumeAllBindedWith(
|
||||
Node * target
|
||||
);
|
||||
|
||||
// 暂停绑定在节点上的所有动作
|
||||
static void __pauseAllBindedWith(
|
||||
void pauseAllBindedWith(
|
||||
Node * target
|
||||
);
|
||||
|
||||
// 停止绑定在节点上的所有动作
|
||||
static void __stopAllBindedWith(
|
||||
void stopAllBindedWith(
|
||||
Node * target
|
||||
);
|
||||
|
||||
// 清空绑定在节点上的所有动作
|
||||
static void __clearAllBindedWith(
|
||||
void clearAllBindedWith(
|
||||
Node * target
|
||||
);
|
||||
|
||||
// 重置所有动作状态
|
||||
static void __resetAll();
|
||||
private:
|
||||
ActionManager();
|
||||
|
||||
// 回收资源
|
||||
static void __uninit();
|
||||
~ActionManager();
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
// 删除已绑定的碰撞体
|
||||
static void __removeCollider(
|
||||
static void __remove(
|
||||
Collider * pCollider
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue