Magic_Game/core/Action/Animate.cpp

114 lines
1.8 KiB
C++
Raw Normal View History

2018-05-10 00:58:43 +08:00
#include "..\e2daction.h"
2018-05-24 00:58:16 +08:00
#include "..\e2dnode.h"
2018-05-10 00:58:43 +08:00
e2d::Animate::Animate()
: _frameIndex(0)
, _animation(nullptr)
{
}
e2d::Animate::Animate(Animation * animation)
: _frameIndex(0)
, _animation(nullptr)
{
this->setAnimation(animation);
}
e2d::Animate::~Animate()
{
2018-07-07 01:43:41 +08:00
GC::safeRelease(_animation);
2018-05-10 00:58:43 +08:00
}
e2d::Animation * e2d::Animate::getAnimation() const
{
return _animation;
}
void e2d::Animate::setAnimation(Animation * animation)
{
if (animation && animation != _animation)
{
2018-07-06 00:47:50 +08:00
GC::safeRelease(_animation);
2018-05-10 00:58:43 +08:00
_animation = animation;
2018-07-07 01:43:41 +08:00
GC::retain(_animation);
2018-05-10 00:58:43 +08:00
}
}
void e2d::Animate::_init()
{
Action::_init();
auto target = dynamic_cast<Sprite*>(_target);
if (target && _animation)
{
target->open(_animation->getFrames()[_frameIndex]);
++_frameIndex;
}
}
void e2d::Animate::_update()
{
Action::_update();
if (!_animation)
{
this->stop();
return;
}
2018-07-22 00:41:24 +08:00
auto game = Game::getInstance();
while ((game->getTotalDuration().seconds() - _last) >= _animation->getInterval())
2018-05-10 00:58:43 +08:00
{
auto& frames = _animation->getFrames();
auto target = dynamic_cast<Sprite*>(_target);
if (target)
{
target->open(frames[_frameIndex]);
}
_last += _animation->getInterval();
2018-05-14 22:51:40 +08:00
++_frameIndex;
2018-05-14 00:36:01 +08:00
2018-05-10 00:58:43 +08:00
if (_frameIndex == frames.size())
{
this->stop();
break;
}
}
}
void e2d::Animate::_resetTime()
{
Action::_resetTime();
2018-07-22 00:41:24 +08:00
_last = Game::getInstance()->getTotalDuration().seconds();
}
2018-05-10 00:58:43 +08:00
void e2d::Animate::reset()
{
Action::reset();
_frameIndex = 0;
}
e2d::Animate * e2d::Animate::clone() const
{
2018-05-10 14:03:54 +08:00
if (_animation)
{
return new (e2d::autorelease) Animate(_animation);
2018-05-10 14:03:54 +08:00
}
2018-05-14 00:36:01 +08:00
return nullptr;
2018-05-10 00:58:43 +08:00
}
e2d::Animate * e2d::Animate::reverse() const
{
2018-05-10 14:03:54 +08:00
if (_animation)
2018-05-10 00:58:43 +08:00
{
2018-05-24 12:24:39 +08:00
auto animation = _animation->reverse();
2018-05-15 23:59:58 +08:00
if (animation)
{
return new (e2d::autorelease) Animate(animation);
2018-05-15 23:59:58 +08:00
}
2018-05-10 14:03:54 +08:00
}
2018-05-15 23:59:58 +08:00
return nullptr;
2018-05-10 00:58:43 +08:00
}