Magic_Game/core/Action/Animate.cpp

133 lines
2.1 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()
{
}
e2d::Animation * e2d::Animate::getAnimation() const
{
return _animation;
}
void e2d::Animate::setAnimation(Animation * animation)
{
if (animation && animation != _animation)
{
2018-05-19 01:10:37 +08:00
GC::release(_animation);
2018-05-10 00:58:43 +08:00
_animation = animation;
_animation->retain();
}
}
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;
}
while ((Time::getTotalTime() - _last) >= _animation->getInterval())
{
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();
_last = Time::getTotalTime();
}
2018-05-10 00:58:43 +08:00
void e2d::Animate::reset()
{
Action::reset();
_frameIndex = 0;
}
void e2d::Animate::onDestroy()
{
Action::onDestroy();
2018-05-19 01:10:37 +08:00
GC::release(_animation);
2018-05-10 00:58:43 +08:00
}
e2d::Animate * e2d::Animate::clone() const
{
2018-05-10 14:03:54 +08:00
if (_animation)
{
2018-05-19 01:10:37 +08:00
return GC::create<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-10 14:03:54 +08:00
auto& oldFrames = _animation->getFrames();
std::vector<Image*> frames(oldFrames.size());
if (!oldFrames.empty())
2018-05-10 00:58:43 +08:00
{
2018-05-10 14:03:54 +08:00
for (auto iter = oldFrames.crbegin(), iterCrend = oldFrames.crend(); iter != iterCrend; ++iter)
2018-05-10 00:58:43 +08:00
{
2018-05-10 14:03:54 +08:00
Image* frame = *iter;
if (frame)
{
frames.push_back(frame);
}
2018-05-10 00:58:43 +08:00
}
}
2018-05-19 01:10:37 +08:00
auto animation = GC::create<Animation>(_animation->getInterval(), frames);
2018-05-15 23:59:58 +08:00
if (animation)
{
2018-05-19 01:10:37 +08:00
return GC::create<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
}