-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationComponent.cpp
More file actions
109 lines (102 loc) · 3.12 KB
/
Copy pathAnimationComponent.cpp
File metadata and controls
109 lines (102 loc) · 3.12 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
#include "AnimationComponent.h"
AnimationComponent::AnimationComponent(sf::Sprite& sprite, sf::Texture& texture_Sheet, sf::Vector2f scale, int imgx, int imgy)
: sprite(sprite), textrueSheet(texture_Sheet), scale(scale), imgx(imgx), imgy(imgy), lastAnimation(nullptr), priorityAnimation(nullptr)
{
}
AnimationComponent::~AnimationComponent()
{
for (auto& it : this->animations) {
delete it.second;
}
}
//Accessors
const bool& AnimationComponent::isDone(const std::string key)
{
return this->animations[key]->isDone();
}
//function
void AnimationComponent::addAnimation(const std::string key,
float animationTimer,
int stFrameX, int stFrameY,int frameX, int frameY)
{
this->animations[key] = new Animation(
this->sprite, this->textrueSheet, this->scale,
animationTimer,
stFrameX,stFrameY,frameX,frameY,imgx,imgy
);
}
const bool& AnimationComponent::play(const std::string key,const float& deltaTime,const bool priority)
{
if (priority) {
this->priorityAnimation = this->animations[key];
}
if (this->priorityAnimation) { //If there is priority animation
if (this->priorityAnimation == this->animations[key]) {
//check if need to reset animation form last animation
if (this->lastAnimation != this->animations[key]) {
if (this->lastAnimation == nullptr) {
this->lastAnimation = this->animations[key];
}
else {
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
// remove priorityAnimation
if (this->animations[key]->play(deltaTime)) {
this->priorityAnimation = nullptr;
}
}
}
else {
//check if need to reset animation form last animation
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(deltaTime);
}
return this->animations[key]->isDone();
}
const bool& AnimationComponent::play(const std::string key, const float& deltaTime, const float& modifier, const float& modifier_max, const bool priority)
{
if (priority) {
this->priorityAnimation = this->animations[key];
}
if (this->priorityAnimation) { //If there is priority animation
if (this->priorityAnimation == this->animations[key]) {
//check if need to reset animation form last animation
if (this->lastAnimation != this->animations[key]) {
if (this->lastAnimation == nullptr) {
this->lastAnimation = this->animations[key];
}
else {
this->lastAnimation->reset();
this->lastAnimation = this->animations[key];
}
}
// remove priorityAnimation
if (this->animations[key]->play(deltaTime, (modifier / modifier_max))) {
this->priorityAnimation = nullptr;
}
}
}
else {
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(deltaTime, (modifier / modifier_max));
}
return this->animations[key]->isDone();
}