shooter/engine/animation/Animation.cpp

58 lines
1.7 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 27.01.2021.
//
#include "Animation.h"
2021-10-31 11:39:08 +03:00
Animation::Animation(double duration, Animation::LoopOut looped, Animation::InterpolationType intType, bool waitFor)
: _duration(duration), _looped(looped), _intType(intType), _waitFor(waitFor) {
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
bool Animation::updateState() {
2021-10-31 11:39:08 +03:00
if (_finished || std::abs(_duration) < Consts::EPS) {
_finished = true;
2021-10-28 16:58:02 +03:00
return false;
2021-09-13 15:53:43 +03:00
}
// linear normalized time:
2021-10-31 11:39:08 +03:00
_dtime = Time::deltaTime() / _duration;
2021-11-01 17:48:26 +03:00
if(_time + _dtime < 1.0) {
_time += _dtime;
} else {
_dtime = 1.0 - _time;
_time = 1.0 - _dtime;
_finished = true;
}
2021-09-13 15:53:43 +03:00
2021-10-31 11:39:08 +03:00
if (_looped == LoopOut::Continue && _time > 0.5) {
_time = 0.5;
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
switch (_intType) {
2021-10-28 16:58:02 +03:00
case InterpolationType::Bezier:
_progress = Interpolation::Bezier(Consts::BEZIER[0], Consts::BEZIER[1], _time);
_dprogress = Interpolation::dBezier(Consts::BEZIER[0], Consts::BEZIER[1], _time, _dtime);
2021-09-13 15:53:43 +03:00
break;
2021-10-28 16:58:02 +03:00
case InterpolationType::Bouncing:
_progress = Interpolation::Bouncing(_time);
_dprogress = Interpolation::dBouncing(_time, _dtime);
2021-09-13 15:53:43 +03:00
break;
2021-10-28 16:58:02 +03:00
case InterpolationType::Linear:
_progress = Interpolation::Linear(_time);
_dprogress = Interpolation::dLinear(_time, _dtime);
2021-09-13 15:53:43 +03:00
break;
2021-10-28 16:58:02 +03:00
case InterpolationType::Cos:
_progress = Interpolation::Cos(_time);
_dprogress = Interpolation::dCos(_time, _dtime);
2021-09-13 15:53:43 +03:00
break;
2021-10-28 16:58:02 +03:00
default:
2021-10-31 11:39:08 +03:00
throw std::logic_error{
"Animation::updateState: unknown interpolation type " + std::to_string(static_cast<int>(_intType))};
2021-09-13 15:53:43 +03:00
}
2021-10-28 16:58:02 +03:00
update();
2021-11-01 17:48:26 +03:00
return !_finished;
2021-10-28 16:58:02 +03:00
}