-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationComponent.cpp
More file actions
148 lines (130 loc) · 3.28 KB
/
AnimationComponent.cpp
File metadata and controls
148 lines (130 loc) · 3.28 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stdafx.h"
#include "AnimationComponent.h"
//Constructors and Destructor:
AnimationComponent::AnimationComponent(sf::Sprite& sprite, sf::Texture& texture_sheet)
: sprite(sprite), textureSheet(texture_sheet), lastAnimation(nullptr), priorityAnimation(nullptr)
{
}
AnimationComponent::~AnimationComponent()
{
for (auto& i : this->animations)
{
delete i.second;
}
}
//Accessors:
const bool& AnimationComponent::isDone(const std::string key)
{
return this->animations[key]->isDone();
}
//Functions:
void AnimationComponent::addAnimation
(
const std::string key,
float animation_timer,
int start_frame_x, int start_frame_y, int frames_x, int frames_y, int width, int height
)
{
this->animations[key] = new Animation
(
this->sprite, this->textureSheet,
animation_timer,
start_frame_x, start_frame_y, frames_x, frames_y, width, height
);
}
const bool& AnimationComponent::play(const std::string key, const float& dt, const bool priority)
{
if (this->priorityAnimation) //If there is a priority animation
{
if (this->priorityAnimation == this->animations[key])
{
if (this->lastAnimation != this->animations[key])
{
if (this->lastAnimation == nullptr)
{
this->lastAnimation = this->animations[key];
}
else
{
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
//If the priority animation is done - remove it
if (this->animations[key]->play(dt))
{
this->priorityAnimation = nullptr;
}
}
}
else //Play animation if no other priority animation is set
{
//If priprity is true - set this animation to priority
if (priority)
{
this->priorityAnimation = this->animations[key];
}
if (this->lastAnimation != this->animations[key])
{
if (this->lastAnimation == nullptr)
{
this->lastAnimation = this->animations[key];
}
else
{
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
this->animations[key]->play(dt);
}
return this->animations[key]->isDone();
}
const bool& AnimationComponent::play(const std::string key, const float& dt, const float& modifier, const float& modifier_max, const bool priority)
{
if (this->priorityAnimation) //If there is a priority animation
{
if (this->priorityAnimation == this->animations[key])
{
if (this->lastAnimation != this->animations[key])
{
if (this->lastAnimation == nullptr)
{
this->lastAnimation = this->animations[key];
}
else
{
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
//If the priority animation is done - remove it
if (this->animations[key]->play(abs(modifier / modifier_max), dt))
{
this->priorityAnimation = nullptr;
}
}
}
else //Play animation if no other priority animation is set
{
//If priprity is true - set this animation to priority
if (priority)
{
this->priorityAnimation = this->animations[key];
}
if (this->lastAnimation != this->animations[key])
{
if (this->lastAnimation == nullptr)
{
this->lastAnimation = this->animations[key];
}
else
{
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
this->animations[key]->play(abs(modifier / modifier_max), dt);
}
return this->animations[key]->isDone();
}