-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.cpp
161 lines (128 loc) · 4.06 KB
/
animation.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
namespace animation
{
AnimationClip::AnimationClip(std::string path, sf::Sprite* sprite, sf::IntRect& rect, int countOfSprites) : m_lostTime(0), m_current(0), m_animSpeed(1), m_pause(false), m_looped(true)
{
m_numberOfSprites = countOfSprites;
// Загрузка текстур
for (int i = 0; i < countOfSprites; i++)
{
// Создание новой текстуры
m_textures.push_back(new sf::Texture);
// Характеристка текстуры
sf::IntRect spriteRect(0 + i * rect.width, 0, rect.width, rect.height);
if (!m_textures[i]->loadFromFile(path, spriteRect))
{
std::cout << "Error on loading sprite\n";
std::exit(-3);
}
}
// Анимируемый спрайт в m_currentSprite
m_currentSprite = sprite;
// Установка текстуры
m_currentSprite->setTexture(*m_textures[m_current]);
std::cout << "Animation clip is created\n";
}
// Конструктор по умолчанию
// Устанавливает все члены класса в дефолтные значения
AnimationClip::AnimationClip(sf::Sprite* sprite)
{
m_lostTime = 0;
m_current = 0;
m_numberOfSprites = 0;
m_animSpeed = 1;
m_currentSprite = sprite;
m_pause = false;
m_looped = true;
}
AnimationClip::AnimationClip(const AnimationClip& animClip)
{
// Копирование обычных членов класса
m_numberOfSprites = animClip.m_numberOfSprites;
m_current = animClip.m_current;
m_lostTime = animClip.m_lostTime;
m_looped = animClip.m_looped;
// Копирование загруженных текстур
for (int i = 0; i < m_numberOfSprites; i++)
m_textures.push_back(new sf::Texture(*animClip.m_textures[i]));
// Копирование
for (int i = 0; i < m_numberOfSprites; i++)
m_animTimes.push_back(animClip.m_animTimes[i]);
}
AnimationClip& AnimationClip::operator=(const AnimationClip& animClip)
{
// Проверяем на присваивание самому себе
if (this == & animClip)
return *this;
// Копируем обычные члены класса
m_numberOfSprites = animClip.m_numberOfSprites;
m_current = animClip.m_current;
m_lostTime = animClip.m_lostTime;
m_looped = animClip.m_looped;
// Чистим уже загруженные текстуры
for (int i = 0; i < m_textures.size(); i++)
delete m_textures[i];
// Копирование загруженных текстур
for (int i = 0; i < m_numberOfSprites; i++)
m_textures.push_back(new sf::Texture(*animClip.m_textures[i]));
// Сбрасываем вектор m_animTimes
// и копируем в него новые значения
m_animTimes.clear();
for (int i = 0; i < m_numberOfSprites; i++)
m_animTimes.push_back(animClip.m_animTimes[i]);
return *this;
}
bool AnimationClip::SetAnimTime(long int time, int index)
{
// Проверка на корректность индекса
if (index < 0 || index >= m_numberOfSprites)
return false;
m_animTimes.insert(m_animTimes.begin() + index, time);
return true;
}
bool AnimationClip::SetTexture(sf::Texture& texture, int index)
{
if (index < 0 || index >= m_numberOfSprites)
return false;
m_textures.insert(m_textures.begin() + index, new sf::Texture(texture));
return true;
}
void AnimationClip::UpdateAnimTimes()
{
for (int i = 0; i < m_animTimes.size(); i++)
{
m_animTimes[i] /= m_animSpeed;
}
}
void AnimationClip::Update(int delta)
{
if (m_animTimes.size() != 0 && !m_pause)
{
if (m_current < m_numberOfSprites)
{
m_lostTime += delta;
if (m_lostTime >= m_animTimes[m_current] / m_animSpeed)
{
m_current++;
if (m_current >= m_numberOfSprites)
{
if (m_looped)
{
m_current = 0;
m_lostTime = 0;
} else
return;
}
m_currentSprite->setTexture(*m_textures[m_current]);
}
}
}
}
AnimationClip::~AnimationClip()
{
for (int i = 0; i < m_numberOfSprites; i++)
delete m_textures[i];
}
}