-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.cpp
More file actions
126 lines (111 loc) · 4.47 KB
/
animation.cpp
File metadata and controls
126 lines (111 loc) · 4.47 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
#include "animation.h"
#include <iostream>
Animation::Animation(sf::Texture &texture, int t)
{
this->setTexture(texture);
this->setTextureRect(sf::IntRect(0, 0, spritedim, spritedim));
this->type = t;
if(this->type == 1) setShipParameters();
else if(this->type == 2) setDestroyerParameters();
}
void Animation::playAnimation(sf::Time &elapsed)
{
if(this->type == 1) this->playShipAnimation(elapsed);
else if(this->type == 2) this->playDestroyerAnimation(elapsed);
}
//// For old explosion sprite
//void Animation::setShipParameters()
//{
// this->setScale((float)1/3, (float)1/3); // 192 / 3 = 64
// spritedim = 192; // Dimension of one sprite in sprite sheet. 192x192
// switch_time = 0.05; // 20 fps. Will play all 10 frames of sprite sheet in 0.5 second.
// totalrow = 2;
// totalcolumn = 5;
// currentrow = 1;
// currentcolumn = 1;
//}
//void Animation::playShipAnimation(sf::Time &elapsed)
//{
// // Set animation position
// this->setPosition(this->enemyposition.x, this->enemyposition.y); // Center animation wrt to enemy sprite
// this->total_time += elapsed.asSeconds();
// if(this->total_time >= this->switch_time)
// {
// this->total_time -= this->switch_time; // Almost same as total_time = 0;. In the long run this will prevent errors caused by approximations.
// this->setTextureRect(sf::IntRect((this->currentcolumn-1) * this->spritedim, (this->currentrow-1) * this->spritedim, this->spritedim, this->spritedim));
// this->currentcolumn++; // Go to next column
// if(this->currentcolumn == this->totalcolumn && this->currentrow != this->totalrow) // After animating first row of sprite sheet
// {
// this->currentrow++;
// this->currentcolumn = 1;
// }
// if(this->currentrow >= this->totalrow && this->currentcolumn >= this->totalcolumn) // If one animation is done
// {
// this->explosion = false;
// this->currentcolumn = 1;
// this->currentrow = 1;
// }
// }
//}
// For new animation spritesheet
void Animation::setShipParameters()
{
this->setScale((float)1/2, (float)1/2); // 256 / 2 = 128
spritedim = 256; // Dimension of one sprite in sprite sheet. 256x256
switch_time = 0.02; // 0.02 -> 50 fps
totalrow = 1;
totalcolumn = 64;
currentrow = 1;
currentcolumn = 1;
}
void Animation::playShipAnimation(sf::Time &elapsed)
{
// Set animation position
// Center animation wrt to enemy sprite
// ______
// | __ | __
// | |__| | b |__| a [ x - (b-a)/2, y - (b-a)/2 ]
// |______|
this->setPosition(this->enemyposition.x - (this->getGlobalBounds().width - 64)/2,
this->enemyposition.y - (this->getGlobalBounds().height - 64)/2);
this->total_time += elapsed.asSeconds();
if(this->total_time >= this->switch_time)
{
this->total_time -= this->switch_time; // Almost same as total_time = 0;. In the long run this will prevent errors caused by approximations.
this->setTextureRect(sf::IntRect((this->currentcolumn-1) * this->spritedim, 0, this->spritedim, this->spritedim));
this->currentcolumn++; // Go to next column
if(this->currentcolumn >= this->totalcolumn) // If one animation is done
{
this->explosion = false;
this->currentcolumn = 1;
}
}
}
void Animation::setDestroyerParameters()
{
// this->setScale((float)1/1.6, (float)1/1.6); // 192 / 1.6 = 120
this->setScale((float)1/1.2, (float)1/1.2); // 192 / 1.2 = 160
spritedim = 192; // 192x192
switch_time = 0.02; // 0.02 -> 50 fps
totalrow = 1;
totalcolumn = 64;
currentrow = 1;
currentcolumn = 1;
}
void Animation::playDestroyerAnimation(sf::Time &elapsed)
{
// Set animation position
this->setPosition(this->enemyposition.x, this->enemyposition.y); // Center animation wrt to enemy sprite
this->total_time += elapsed.asSeconds();
if(this->total_time >= this->switch_time)
{
this->total_time -= this->switch_time; // Almost same as total_time = 0;. In the long run this will prevent errors caused by approximations.
this->setTextureRect(sf::IntRect((this->currentcolumn-1) * this->spritedim, 0, this->spritedim, this->spritedim));
this->currentcolumn++; // Go to next column
if(this->currentcolumn >= this->totalcolumn) // If one animation is done
{
this->explosion = false;
this->currentcolumn = 1;
}
}
}