shooter/engine/animation/Animation.cpp

61 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-11-09 22:54:20 +03:00
#include "../Consts.h"
#include "../utils/Time.h"
2021-09-13 15:53:43 +03:00
2021-11-09 22:54:20 +03:00
Animation::Animation(double duration, Animation::LoopOut looped, Animation::InterpolationType intType, bool waitForFinish)
: _duration(duration), _looped(looped), _intType(intType), _waitForFinish(waitForFinish) {
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
2021-09-13 15:53:43 +03:00
switch (_intType) {
2021-10-28 16:58:02 +03:00
case InterpolationType::Bezier:
_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:
_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:
_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:
_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{
2022-02-23 07:51:53 +03:00
"Animation::updateState: unknown interpolation type " + std::to_string(static_cast<int>(_intType))
};
2021-09-13 15:53:43 +03:00
}
2021-11-01 21:38:57 +03:00
if (_time + _dtime > 1.0) {
_dtime = 1.0 - _time;
_time = 1.0;
_dprogress = 1.0 - _progress;
_progress = 1.0;
_finished = true;
2022-02-23 07:51:53 +03:00
2021-11-01 21:38:57 +03:00
} else {
_time += _dtime;
_progress += _dprogress;
}
2021-11-01 19:04:10 +03:00
if (_looped == LoopOut::Continue && _time > 0.5) {
_time = 0.5;
}
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
}