-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveAnimation.cpp
64 lines (57 loc) · 1.58 KB
/
MoveAnimation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "MoveAnimation.h"
#include <cmath>
#include "CharShape.h"
#include "Mask.h"
MoveAnimation::MoveAnimation(Vector2f relative_destination, Mode movement_mode)
:destination{ relative_destination }
, mode{ movement_mode }
{
}
template <class T>
void MoveAnimation::operator()(T& object, double progress)
{
if (firstCall) {
source = object.getPosition();
firstCall = false;
}
//prepare "firstCall" for next animation if the current one is complete:
//firstCall = progress == 1.0f; //irrelevant in thor2.0(progress is not guaranteed to be 1 on last call), pending thor2.1
this->progress = progress;
double modifier;
switch (mode)
{
case Mode::accelerate :
modifier = pow(progress, 2);
break;
case Mode::decelerate :
modifier = cbrt(progress);
break;
case Mode::ac_de :
modifier = (cbrt(2 * progress - 1) + 1) / 2;
break;
case Mode::de_ac :
modifier = (pow(2 * progress - 1, 3) + 1) / 2;
break;
case Mode::settle :
modifier = -1 * abs(1.25 * progress - 1.125) + 1.125;
break;
default: //uniform
modifier = progress;
break;
}
object.setPosition(source + static_cast<float>(modifier) * destination);
}
bool MoveAnimation::is_idle()
{
return firstCall;
}
void MoveAnimation::reset(Vector2f relative_destination, Mode movement_mode)
{
this->destination = relative_destination;
this->mode = movement_mode;
firstCall = true;
progress = 0.0;
}
template void MoveAnimation::operator() < CharShape >(CharShape&, double);
template void MoveAnimation::operator() < Transformable > (Transformable&, double);
template void MoveAnimation::operator() < Mask > (Mask&, double);