-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationComponent.h
More file actions
133 lines (121 loc) · 3.8 KB
/
Copy pathAnimationComponent.h
File metadata and controls
133 lines (121 loc) · 3.8 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
#pragma once
#include<iostream>
#include<string>
#include<map>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
class AnimationComponent
{
private:
class Animation
{
public:
//variable
sf::Sprite& sprite;
sf::Texture& textureSheet;
float animationTimer;
float timer;
bool done;
int imgy;
int imgx;
int width;
int height;
sf::IntRect startRect;
sf::IntRect currentRect;
sf::IntRect endRect;
Animation(sf::Sprite& sprite, sf::Texture& texture_Sheet, sf::Vector2f scale,
float animationTimer,
int stFrameX, int stFrameY, int frameX, int frameY, int imgx, int imgy)
:sprite(sprite), textureSheet(texture_Sheet),
animationTimer(animationTimer), imgx(imgx), imgy(imgy), timer(0.f), done(false)
{
this->width = this->textureSheet.getSize().x / imgx;
this->height = this->textureSheet.getSize().y / imgy;
this->startRect = sf::IntRect(stFrameX * width, stFrameY * height, width, height);
this->currentRect = this->startRect;
this->endRect = sf::IntRect(frameX * width, frameY * height, width, height);
this->sprite.setOrigin(float(width)/2.f, float(height)/2.f);
this->sprite.setScale(scale);
this->sprite.setTexture(this->textureSheet, true);
this->sprite.setTextureRect(this->startRect);
}
//Accessor
const bool& isDone()const { return this->done; }
//function
const bool &play(const float& deltaTime) {
//Update timer
this->done = false;
this->timer += 100.f * deltaTime;
if (this->timer >= this->animationTimer) {
//reset timer
this->timer = 0.f;
if (this->currentRect != this->endRect) {
this->currentRect.left += this->width;
}
else {
this->currentRect = this->startRect;
this->done = true;
}
this->sprite.setTextureRect(this->currentRect);
//change row
if (this->currentRect.left == (imgx - 1) * width && this->currentRect != this->endRect) {
this->currentRect.left = 0;
this->currentRect.top += height;
}
//std::cout << currentRect.left << " " << currentRect.top << std::endl;
}
return this->done;
}
const bool &play(const float& deltaTime, float percentage) {
//Update timer
this->done = false;
percentage = abs(percentage);
if (percentage < 0.5f) percentage = 0.5f;
this->timer += percentage * 100.f * deltaTime;
if (this->timer >= this->animationTimer) {
//reset timer
this->timer = 0.f;
if (this->currentRect != this->endRect) {
this->currentRect.left += this->width;
}
else {
this->currentRect = this->startRect;
this->done = true;
}
this->sprite.setTextureRect(this->currentRect);
//change row
if (this->currentRect.left == (imgx - 1) * width && this->currentRect != this->endRect) {
this->currentRect.left = 0;
this->currentRect.top += height;
}
//std::cout << currentRect.left << " " << currentRect.top << std::endl;
}
return this->done;
}
void reset() {
this->timer = this->animationTimer;
this->currentRect = this->startRect;
}
};
sf::Sprite& sprite;
sf::Texture& textrueSheet;
sf::Vector2f scale;
int imgx, imgy;
std::map<std::string, Animation*> animations;
Animation* lastAnimation;
Animation* priorityAnimation;
public:
AnimationComponent(sf::Sprite& sprite,sf::Texture& texture_Sheet, sf::Vector2f scale, int imgx, int imgy);
virtual ~AnimationComponent();
//Accessor
const bool& isDone(const std::string key);
//function
void addAnimation(const std::string key,
float animationTimer,
int stFrameX,int stFrameY, int frameX, int frameY);
const bool& play(const std::string key,const float& deltaTime, const bool priority = false);
const bool& play(const std::string key,const float& deltaTime,const float& modifier,const float& modifier_max, const bool priority = false);
};