-
Notifications
You must be signed in to change notification settings - Fork 0
/
rythm.cpp
227 lines (197 loc) · 5.68 KB
/
rythm.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "config.hpp"
#include "game.hpp"
#include "rythm.hpp"
Rythm::Rythm() : _deltaBeat(DELTA_GENERATE_TIME_WAVES), _deltaTimeWaves(DELTA_TIME_WAVES), _isTheEnd(false), _totalWaves(0)
{
}
Rythm::~Rythm()
{
std::for_each(_waves.begin(), _waves.end(), WaveDeallocator());
}
bool Rythm::init(bool retry)
{
_deltaBeat = DELTA_GENERATE_TIME_WAVES;
_deltaTimeWaves = DELTA_TIME_WAVES;
_isTheEnd = false;
_totalWaves = 0;
_security = false;
_firstHeart = true;
if(!retry)
{
if(!_backgroundO.loadFromFile(IMAGES_PATH"rythm/background.png") ||
!_buffer1.loadFromFile(SOUNDS_PATH"beat1.wav") ||
!_buffer2.loadFromFile(SOUNDS_PATH"beat2.wav"))
return false;
_background.setTexture(_backgroundO);
_background.setPosition(WINDOW_WIDTH - _background.getGlobalBounds().width, 0);
_heart1.setBuffer(_buffer1);
_heart2.setBuffer(_buffer2);
if(!_lineO.loadFromFile(IMAGES_PATH"rythm/line.png"))
return false;
_lineSprite.setTexture(_lineO);
_lineSprite.setPosition(WINDOW_WIDTH - _lineSprite.getGlobalBounds().width, 0);
if(!_heart.init())
return false;
if(!_waveO.loadFromFile(IMAGES_PATH"rythm/wave.png"))
return false;
}
else
{
_heart.init(true);
std::for_each(_waves.begin(), _waves.end(), WaveDeallocator());
for(std::list<Wave*>::iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
it = _waves.erase(it);
}
_waves.clear();
}
return true;
}
void Rythm::update(sf::Time elapsedTime)
{
generateWaves(elapsedTime);
for(std::list<Wave*>::iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
if(!(*it)->isAlive() || (*it)->isMissed())
{
if((*it)->isMissed())
{
hurted();
_heart.breakCombo();
}
delete *it;
it = _waves.erase(it);
}
}
_elapsedTime += elapsedTime;
for(std::list<Wave*>::const_iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
(*it)->update(elapsedTime, _deltaTimeWaves);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && _elapsedTime > sf::milliseconds(100) && !_security)
{
beatWave();
_elapsedTime = _elapsedTime.Zero;
_security = true;
}
else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
_security = false;
_heart.update();
if(!_heart.isAlive())
{
_isTheEnd = true;
}
}
void Rythm::draw(sf::RenderWindow &app)
{
app.draw(_background);
for(int i = 0 ; i < WINDOW_HEIGHT ; i++)
{
bool draw = true;
for(std::list<Wave*>::const_iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
if(AABBvSAABB((*it)->getRect(),
sf::Rect<float>(WINDOW_WIDTH - _lineSprite.getGlobalBounds().width,
i,
_lineSprite.getGlobalBounds().width,
_lineSprite.getGlobalBounds().height)))
{
draw = false;
}
}
if(draw)
{
_lineSprite.setPosition(WINDOW_WIDTH - _lineSprite.getGlobalBounds().width, i);
app.draw(_lineSprite);
}
}
for(std::list<Wave*>::const_iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
(*it)->draw(app);
}
_heart.draw(app);
}
void Rythm::hurted()
{
_heart.losePieceOfHeart();
}
void Rythm::accelerate()
{
sf::Time deltaTimeWaves = DELTA_TIME_WAVES;
if(_deltaTimeWaves.asMilliseconds() > deltaTimeWaves.asMilliseconds() / 4)
_deltaTimeWaves = sf::milliseconds(_deltaTimeWaves.asMilliseconds() / 2);
}
void Rythm::decelerate(sf::Time elapsedTime)
{
_elapsedTimeWaves += elapsedTime;
if(_deltaTimeWaves < DELTA_TIME_WAVES && _elapsedTimeWaves > sf::seconds(10))
{
_deltaTimeWaves = sf::milliseconds(_deltaTimeWaves.asMilliseconds() * 2);
if(_deltaTimeWaves > DELTA_TIME_WAVES)
_deltaTimeWaves = DELTA_TIME_WAVES;
_elapsedTimeWaves = _elapsedTimeWaves.Zero;
}
}
void Rythm::generateWaves(sf::Time elapsedTime)
{
_elapsedTimeBeat += elapsedTime;
if(!_waves.empty())
{
if(_waves.back()->getPosition().y > _waves.back()->getRect().height && _totalWaves % 2 == 0)
{
createWave();
_elapsedTimeBeat = _elapsedTimeBeat.Zero;
}
else if(_waves.back()->getPosition().y > _waves.back()->getRect().height * 2 && _totalWaves % 2 == 1)
{
createWave();
_elapsedTimeBeat = _elapsedTimeBeat.Zero;
}
}
else
{
createWave();
_elapsedTimeBeat = _elapsedTimeBeat.Zero;
}
}
void Rythm::createWave()
{
Wave *wave = new Wave();
wave->init(_waveO);
_waves.push_back(wave);
_totalWaves++;
}
void Rythm::beatWave()
{
bool beat = false;
for(std::list<Wave*>::const_iterator it = _waves.begin() ; it != _waves.end() ; ++it)
{
if(AABBvSAABB((*it)->getRect(), _heart.getRect()))
{
beat = true;
_heart.beatWave();
(*it)->die();
if(_firstHeart)
{
_heart1.play();
_firstHeart = false;
}
else
{
_heart2.play();
_firstHeart = true;
}
break;
}
else
{
_heart.breakCombo();
}
}
if(!beat)
_heart.losePieceOfHeart();
}
bool Rythm::isTheEnd()
{
return _isTheEnd;
}