-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
403 lines (358 loc) · 9.92 KB
/
Game.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "Game.h"
//takes in pointer to class that hold spritesheets so entities can be created with certain spritesheet
Game::Game(Textures *pSpriteSheet) :
mTextures(pSpriteSheet),
mPlayer(sf::Vector2f(WindowWidth/2,WindowHeight -40), sf::Vector2f(0,0), sf::Vector2i(96,16), (pSpriteSheet->getTexture(sPLAYER))),
mBall(sf::Vector2f(WindowWidth/2,WindowHeight -60), sf::Vector2f(0,0), sf::Vector2i(16,16), (pSpriteSheet->getTexture(sBALL))),
mNumofLives(3),
mTownHealth(100),
mCurrentLevel(0),
mMaxLevel(9)
{
mBackground.setTexture(*pSpriteSheet->getTexture(sGAMEBACKGROUND));
//text stuff
mFont.loadFromFile("Assets/galaxymonkey.ttf");
mTextLives.setFont(mFont);
mTextTown.setFont(mFont);
mTextLives.setColor(sf::Color::White);
mTextTown.setColor(sf::Color::White);
mTextTown.setScale(0.6,0.6);
mTextLives.setScale(0.6,0.6);
mTextTown.setPosition(30, WindowHeight - 25);
mTextLives.setPosition(WindowWidth - 100, WindowHeight - 25);
}
void Game::reset()
{
mCurrentLevel = 0;
mTownHealth = 100;
mNumofLives = 3;
quit();
mPlayer.setPosition(WindowWidth/2,WindowHeight -40);
mPlayer.setIsLeft(false);
mPlayer.setIsRight(false);
mBall.setVelocity(0,0);
mBall.setPosition(mPlayer.getPosition().x, WindowHeight -60);
}
Game::~Game(void)
{
}
void Game::addEnemy(float pX, float pY)
{
//if no delete before removed u get memory leaks
mEnemies.insert(mEnemies.begin(), new Enemy(sf::Vector2f(pX, pY), sf::Vector2f(0,.7), sf::Vector2i(32,32), (mTextures->getTexture(sGHOST))));
}
void Game::removeEnemy(int pIndex)
{
//so delete :D
delete(mEnemies.at(pIndex));
mEnemies.erase((mEnemies.begin() + pIndex));
}
//same for every vector/ add and deletes
void Game::addGrave(float pX, float pY, float pReleaseTimeMin, float pReleaseTimeMax)
{
mGraves.insert(mGraves.begin(), new Grave(sf::Vector2f(pX,pY), sf::Vector2f(0,0), sf::Vector2i(64,80),(mTextures->getTexture(sGRAVE)), pReleaseTimeMin, pReleaseTimeMax));
}
void Game::removeGrave(int pIndex)
{
delete(mGraves.at(pIndex));
mGraves.erase((mGraves.begin() + pIndex));
}
//update method used during game state
//TODO: think of a way to make Game update not so beastly and hacky, maybe some kind of event system
void Game::update()
{
//update text
std::stringstream ss;
ss << "Lives: " << mNumofLives;
mTextLives.setString(ss.str());
ss.str(std::string());
ss << "Town Health: " << mTownHealth;
mTextTown.setString(ss.str());
mPlayer.update();
mBall.update();
//while ball isnt moving make it follow paddle
if(mBall.getVelocity().y == 0)
{
mBall.setPosition(mPlayer.getPosition().x, WindowHeight -60);
}
//if ball hits paddle
if(mBall.getBounds().intersects(mPlayer.getBounds()))
{
//angle in radians
float mMaxBounceAngle = 3.0943951;
//get the relative intersect X value
//this means how far away the center of ball is from center of paddle
float relativeIntersectX = -(mPlayer.getPosition().x - mBall.getPosition().x);
//normalize it
float normalizedRelativeIntersectionX = (relativeIntersectX/mPlayer.getSize().x/2);
//calculate bounce off of the normalized intersection
float bounceAngle = normalizedRelativeIntersectionX * mMaxBounceAngle;
//calculate new speeds
float newSpeedX = (4 * sin(bounceAngle) + (mPlayer.getVelocity().x * 0.1));
float newSpeedY = 4 * -cos(bounceAngle);
mBall.setVelocity(newSpeedX, newSpeedY);
}
//for each enemy in enemies
for(int i = 0; i < mEnemies.size(); i++)
{
//update enemy
mEnemies[i]->update();
//collision detection with player
if(mEnemies[i]->getBounds().intersects(mPlayer.getBounds()))
{
removeEnemy(i);
continue;
}
//kill enemy if off screen X Direction
if(mEnemies[i]->getPosition().x > WindowWidth || mEnemies[i]->getPosition().x < 0)
{
removeEnemy(i);
continue;
}
if(mEnemies[i]->getPosition().y > WindowHeight || mEnemies[i]->getPosition().y < 0)
{
mTownHealth -= 10;
removeEnemy(i);
continue;
}
//enemy colision with ball
if(mBall.getBounds().intersects(mEnemies[i]->getBounds()))
{
//if ball position is on the sides of enemy bounce in X direction
if(mBall.getPosition().y >= mEnemies[i]->getPosition().y - mEnemies[i]->getSize().y/2 &&
mBall.getPosition().y <= mEnemies[i]->getPosition().y + mEnemies[i]->getSize().y/2)
{
mBall.setVelocity(- mBall.getVelocity().x, mBall.getVelocity().y);
}
//else if ball is top or bottom bounce in Y direction
else
{
mBall.setVelocity(mBall.getVelocity().x, -mBall.getVelocity().y);
}
removeEnemy(i);
continue;
}
}
//foreach grave in graves
for(int i = 0; i < mGraves.size(); i++)
{
mGraves[i]->update();
//check if its time to make a ghost
if(mGraves[i]->checkIfTime())
{
addEnemy(mGraves[i]->getPosition().x, mGraves[i]->getPosition().y);
}
//colision with ball
if(mBall.getBounds().intersects(mGraves[i]->getBounds()))
{
//if ball position is on the sides of enemy bounce in X direction
if(mBall.getPosition().y >= mGraves[i]->getPosition().y - mGraves[i]->getSize().y/2 &&
mBall.getPosition().y <= mGraves[i]->getPosition().y + mGraves[i]->getSize().y/2)
{
mBall.setVelocity(- mBall.getVelocity().x, mBall.getVelocity().y);
}
//else if ball is top or bottom bounce in Y direction
else
{
mBall.setVelocity(mBall.getVelocity().x, -mBall.getVelocity().y);
}
removeGrave(i);
}
}
//if ball falls out the bottom of screen
if(mBall.getPosition().y >= WindowHeight - mBall.getSize().y)
{
mNumofLives--;
if(mNumofLives > 0)
{
mBall.setVelocity(0,0);
mBall.setPosition(mPlayer.getPosition().x, WindowHeight -60);
}
else
{
mGameState = gGAMEOVER;
}
}
//if no more enemies
if(mGraves.size() <= 0 && mEnemies.size() <= 0)
{
nextlevel();
}
//check for gameover state
if(mTownHealth <= 0)
mGameState = gGAMEOVER;
}
//just call draw of all entities
//entities drawn first will be back layers
void Game::draw(sf::RenderWindow *window, float pInterpolation)
{
window->draw(mBackground);
window->draw(mTextLives);
window->draw(mTextTown);
mPlayer.draw(window, pInterpolation);
mBall.draw(window, pInterpolation);
for(int i = 0; i < mGraves.size(); i++)
{
mGraves[i]->draw(window, pInterpolation);
}
for(int i = 0; i < mEnemies.size(); i++)
{
mEnemies[i]->draw(window, pInterpolation);
}
}
//get passed the input events do stuff based on event type
void Game::input(sf::Event *pEvent)
{
switch(pEvent->type)
{
//change state of player based on what key is pressed
case(sf::Event::KeyPressed):
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
mPlayer.setIsLeft(true);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
mPlayer.setIsRight(true);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
}
//release ball
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if(mBall.getVelocity().y == 0)
mBall.setVelocity(mPlayer.getVelocity().x, -4);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P) || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
mGameState = gPAUSED;
}
break;
case(sf::Event::KeyReleased):
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
mPlayer.setIsLeft(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
mPlayer.setIsRight(false);
break;
default:
break;
}
}
//handle memory leaks before quitting
void Game::quit()
{
//we go down because when we remove from front, everything shifts down
for(int i = mEnemies.size() - 1; i >= 0; i--)
{
removeEnemy(i);
}
for(int i = mGraves.size() - 1; i >= 0; i--)
{
removeGrave(i);
}
}
//level handler basically
void Game::nextlevel()
{
mCurrentLevel++;
if(mCurrentLevel >= mMaxLevel)
{
//switch to game win state
mGameState = gCOMPLETE;
}
else
{
//make the level
switch(mCurrentLevel)
{
case(1):
addGrave(320,100,8,12);
addGrave(440,100,8,12);
addGrave(200,100,8,12);
break;
case(2):
addGrave(320,100,6,9);
addGrave(440,100,8,10);
addGrave(200,100,8,10);
addGrave(320,200,8,10);
break;
case(3):
addGrave(370,100,6,9);
addGrave(270,100,6,9);
addGrave(320,200,8,10);
addGrave(440,200,8,10);
addGrave(200,200,8,10);
break;
case(4):
addGrave(320,100,5,7);
addGrave(380,100,7,9);
addGrave(260,100,7,9);
addGrave(320,200,8,10);
addGrave(380,200,8,10);
addGrave(260,200,8,10);
break;
case(5):
addGrave(320,100,5,7);
addGrave(380,100,7,9);
addGrave(260,100,7,9);
addGrave(200,100,7,9);
addGrave(440,100,7,9);
addGrave(320,200,8,10);
addGrave(380,200,8,10);
addGrave(260,200,8,10);
addGrave(200,200,8,10);
addGrave(440,200,8,10);
break;
case(6):
addGrave(320,100,5,7);
addGrave(380,100,7,9);
addGrave(260,100,7,9);
addGrave(200,100,7,9);
addGrave(440,100,7,9);
addGrave(320,200,8,10);
addGrave(380,200,8,10);
addGrave(260,200,8,10);
addGrave(200,200,8,10);
addGrave(440,200,8,10);
addGrave(320,300,8,10);
addGrave(380,300,8,10);
addGrave(260,300,8,10);
addGrave(200,300,8,10);
addGrave(440,300,8,10);
break;
case(7):
addGrave(320,100,5,7);
addGrave(440,100,6,9);
addGrave(200,100,6,9);
addGrave(80,100,6,9);
addGrave(560,100,6,9);
addGrave(320,200,8,10);
addGrave(440,200,8,10);
addGrave(200,200,8,10);
addGrave(80,200,8,10);
addGrave(560,200,8,10);
break;
case(8):
addGrave(320,100,5,7);
addGrave(440,100,6,9);
addGrave(200,100,6,9);
addGrave(80,100,6,9);
addGrave(560,100,6,9);
addGrave(320,200,6,9);
addGrave(440,200,6,9);
addGrave(200,200,6,9);
addGrave(80,200,6,9);
addGrave(560,200,6,9);
addGrave(320,300,8,10);
addGrave(440,300,8,10);
addGrave(200,300,8,10);
addGrave(80,300,8,10);
addGrave(560,300,8,10);
break;
}
}
mPlayer.setPosition(WindowWidth/2,WindowHeight -40);
mBall.setVelocity(0,0);
mBall.setPosition(mPlayer.getPosition().x, WindowHeight -60);
}