-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
398 lines (338 loc) · 12 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
#include "Game.h"
//takes in pointer to class that hold spritesheets so entities can be created with certain spritesheet
Game::Game(Textures *pTextureHolder, sf::View * pView) :
mTextureHolder(pTextureHolder),
mPlayer(pTextureHolder, sf::Vector2f(50,50), sf::Vector2f(0,0), sf::Vector2i(32,32), (pTextureHolder->getTexture(sPLAYER))),
mView(pView),
mMap(pTextureHolder),
particleEngine(sf::Vector2f(-300,300),500, sf::Color::Red)
{
mLaserSoundBuffer.loadFromFile("Assets/Laser_Shoot.wav");
mLaserSound.setBuffer(mLaserSoundBuffer);
particleEngine.setSize(10);
//text example
/*
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);
*/
mView->reset(sf::FloatRect(0,0,WindowWidth,WindowHeight));
mView->zoom(0.5f);
//mView->setCenter(mPlayer.getPosition());
mView->setCenter(static_cast<int>(mView->getSize().x/2), static_cast<int>(mView->getSize().y/2));
lighting = false;
}
void Game::reset()
{
quit();
mPlayer.setPosition(50,50);
mPlayer.reset();
}
Game::~Game(void)
{
}
//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
//Everything from update can be taken out / very specific to your game
//left in as example
void Game::update(ltbl::LightSystem * lightSystem, ltbl::Light_Point * light)
{
particleEngine.Update(0, true);
//move view to follow player unless view would expose past the room, then stop moving to player
mView->setCenter(static_cast<int>(mPlayer.getOldPosition().x),static_cast<int>(mPlayer.getOldPosition().y));
if(mView->getCenter().x < mView->getSize().x/2)
{
mView->setCenter(static_cast<int>(mView->getSize().x/2), static_cast<int>(mView->getCenter().y));
}
else if(mView->getCenter().x > mMap.getCurrentRoom()->getRoomWidth() * mMap.getCurrentRoom()->getTileSize().x - mView->getSize().x/2)
{
mView->setCenter(static_cast<int>(mMap.getCurrentRoom()->getRoomWidth() * mMap.getCurrentRoom()->getTileSize().x - mView->getSize().x/2), static_cast<int>(mView->getCenter().y));
}
if(mView->getCenter().y < mView->getSize().y/2)
{
mView->setCenter(static_cast<int>(mView->getCenter().x), static_cast<int>(mView->getSize().y/2));
}
else if(mView->getCenter().y > mMap.getCurrentRoom()->getRoomHeight() * mMap.getCurrentRoom()->getTileSize().y - mView->getSize().y/2)
{
mView->setCenter(static_cast<int>(mView->getCenter().x), static_cast<int>(mMap.getCurrentRoom()->getRoomHeight() * mMap.getCurrentRoom()->getTileSize().y - mView->getSize().y/2));
}
for(int i = 0; i < mLasers.size(); i++)
{
mLasers.at(i)->update();
if(mLasers.at(i)->getPosition().x < 0)
{
removeLaser(i);
continue;
}
else if(mLasers.at(i)->getPosition().y < 0)
{
removeLaser(i);
continue;
}
else if(mLasers.at(i)->getPosition().y > mMap.getCurrentRoom()->getRoomHeight() * mMap.getCurrentRoom()->getTileSize().y)
{
removeLaser(i);
continue;
}
else if(mLasers.at(i)->getPosition().y > mMap.getCurrentRoom()->getRoomWidth() * mMap.getCurrentRoom()->getTileSize().x)
{
removeLaser(i);
continue;
}
for(int j = 0; j < mMap.getCurrentRoom()->getWallAmount(); j++)
{
if(mLasers.at(i)->getBounds().intersects(mMap.getCurrentRoom()->getWall(j)->getGlobalBounds()))
{
particleEngine.setEmitterLocation(mLasers.at(i)->getPosition());
if(mLasers.at(i)->getVelocity().x > 0)
particleEngine.Refill(30,pdLEFT);
else if(mLasers.at(i)->getVelocity().x < 0)
particleEngine.Refill(30,pdRIGHT);
else if(mLasers.at(i)->getVelocity().y < 0)
particleEngine.Refill(30,pdDOWN);
else
particleEngine.Refill(30);
//TODO: particles
removeLaser(i);
break;
}
}
for(int j = 0; j < mMap.getCurrentRoom()->getEnemyAmount(); j++)
{
if(mLasers.at(i)->getBounds().intersects(mMap.getCurrentRoom()->getEnemy(j)->getBounds()))
{
particleEngine.setEmitterLocation(mLasers.at(i)->getPosition());
if(mLasers.at(i)->getVelocity().x > 0)
particleEngine.Refill(30,pdLEFT);
else if(mLasers.at(i)->getVelocity().x < 0)
particleEngine.Refill(30,pdRIGHT);
else if(mLasers.at(i)->getVelocity().y < 0)
particleEngine.Refill(30,pdDOWN);
else
particleEngine.Refill(30);
mMap.getCurrentRoom()->getEnemy(j)->setIsActive(false);
//TODO: particles
removeLaser(i);
break;
}
}
}
for(int i = 0; i < mMap.getCurrentRoom()->getEnemyAmount(); i++)
{
//update enemy
Enemy* tmpEnemyPtr = mMap.getCurrentRoom()->getEnemy(i);
if(tmpEnemyPtr->getIsActive())
{
//collision detection with player
if(tmpEnemyPtr->getBounds().intersects(mPlayer.getBounds()))
{
mPlayer.setPlayerHealth(mPlayer.getPlayerHealth() - 1);
tmpEnemyPtr->setIsActive(false);
}
//TODO ENEMIES COLLIDING
for(int j = 0; j < mMap.getCurrentRoom()->getEnemyAmount(); j++)
{
if(j==i) continue;
if(tmpEnemyPtr->getBounds().intersects(mMap.getCurrentRoom()->getEnemy(j)->getBounds()))
{
tmpEnemyPtr->revertPosition();
tmpEnemyPtr->setVelocity(-tmpEnemyPtr->getVelocity().x,-tmpEnemyPtr->getVelocity().y);
mMap.getCurrentRoom()->getEnemy(j)->revertPosition();
//mMap.getCurrentRoom()->getEnemy(j)->setVelocity(0,0);
mMap.getCurrentRoom()->getEnemy(j)->setVelocity(-mMap.getCurrentRoom()->getEnemy(j)->getVelocity().x,-mMap.getCurrentRoom()->getEnemy(j)->getVelocity().y);
}
}
for(int i = 0; i < mMap.getCurrentRoom()->getWallAmount(); i++)
{
sf::Sprite* tmpWallPtr = mMap.getCurrentRoom()->getWall(i);
if(tmpWallPtr->getGlobalBounds().intersects(tmpEnemyPtr->getBounds()))
{
tmpEnemyPtr->revertPosition();
}
}
}
tmpEnemyPtr->update(mPlayer.getPosition());
}
for(int i = 0; i < mMap.getCurrentRoom()->getWallAmount(); i++)
{
sf::Sprite* tmpWallPtr = mMap.getCurrentRoom()->getWall(i);
if(tmpWallPtr->getGlobalBounds().intersects(mPlayer.getBounds()))
{
mPlayer.revertPosition();
}
}
mPlayer.update();
//check for room switch
if(mPlayer.getPosition().x < 0)
{
mMap.setCurrentRoom(LEFT);
mPlayer.setPosition((mMap.getCurrentRoom()->getRoomWidth() * mMap.getCurrentRoom()->getTileSize().x) - mPlayer.getSize().x/2, mPlayer.getPosition().y);
}
else if (mPlayer.getPosition().y < 0)
{
mMap.setCurrentRoom(UP);
mPlayer.setPosition(mPlayer.getPosition().x, (mMap.getCurrentRoom()->getRoomHeight() * mMap.getCurrentRoom()->getTileSize().y) - mPlayer.getSize().y/2);
}
else if (mPlayer.getPosition().x > (mMap.getCurrentRoom()->getRoomWidth() * mMap.getCurrentRoom()->getTileSize().x))
{
mMap.setCurrentRoom(RIGHT);
mPlayer.setPosition(mPlayer.getSize().x/2, mPlayer.getPosition().y);
}
else if (mPlayer.getPosition().y > (mMap.getCurrentRoom()->getRoomHeight() * mMap.getCurrentRoom()->getTileSize().y))
{
mMap.setCurrentRoom(DOWN);
mPlayer.setPosition(mPlayer.getPosition().x, mPlayer.getSize().y/2);
}
if (lighting)
{
light->SetCenter(Vec2f(mPlayer.getPosition().x, WindowHeight - mPlayer.getPosition().y));
lightSystem->RenderLights();
}
if(mPlayer.getPlayerHealth() <= 0)
{
mGameState = gGAMEOVER;
}
for(int i = 0; i < mMap.getCurrentRoom()->getObjectAmount(); i++)
{
Object* tmpObjectPtr = mMap.getCurrentRoom()->getObject(i);
tmpObjectPtr->update();
if(tmpObjectPtr->getBounds().intersects(mPlayer.getBounds()))
{
switch (tmpObjectPtr->getObjectType())
{
case oBATTERY:
mPlayer.setBatteryLevel(mPlayer.getBatterLevel() + 2);
if(mPlayer.getBatterLevel() > 5)
mPlayer.setBatteryLevel(5);
mMap.getCurrentRoom()->removeObject(i);
break;
case oENGINE:
mGameState = gCOMPLETE;
break;
}
}
}
}
//just call draw of all entities
//entities drawn first will be back layers
void Game::draw(sf::RenderWindow *window, float pInterpolation, ltbl::LightSystem * lightSystem)
{
window->setView(*mView);
mMap.getCurrentRoom()->draw(window,pInterpolation);
for(int i = 0; i < mMap.getCurrentRoom()->getEnemyAmount(); i++)
{
mMap.getCurrentRoom()->getEnemy(i)->draw(window, pInterpolation);
}
for(int i = 0; i < mMap.getCurrentRoom()->getObjectAmount(); i++)
{
mMap.getCurrentRoom()->getObject(i)->draw(window, pInterpolation);
}
mPlayer.draw(window, pInterpolation);
if (lighting)
lightSystem->RenderLightTexture();
for(int i = 0; i < mLasers.size(); i++)
{
mLasers.at(i)->draw(window, pInterpolation);
}
particleEngine.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) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
mPlayer.setIsDown(true);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
mPlayer.setIsUp(true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P) || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
mGameState = gPAUSED;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
//TODO: take out is attacking
//TODO: player battery charge variable and deplete it
if(mPlayer.getBatterLevel() > 0)
{
if(mPlayer.getPlayerState() == pUP)
addLaser(mPlayer.getPosition().x,mPlayer.getPosition().y - mPlayer.getSize().y/2,0,-4);
else if(mPlayer.getPlayerState() == pDOWN)
addLaser(mPlayer.getPosition().x,mPlayer.getPosition().y + mPlayer.getSize().y/2,0,4);
else if(mPlayer.getPlayerState() == pLEFT)
addLaser(mPlayer.getPosition().x - mPlayer.getSize().x/2,mPlayer.getPosition().y,-4,0);
else
addLaser(mPlayer.getPosition().x + mPlayer.getSize().x/2,mPlayer.getPosition().y,4,0);
mPlayer.setBatteryLevel(mPlayer.getBatterLevel() -1);
mLaserSound.play();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSlash))
lighting = !lighting;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::E) || sf::Keyboard::isKeyPressed(sf::Keyboard::T))
{
mPlayer.setIsHealthDisplay(true);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q) || sf::Keyboard::isKeyPressed(sf::Keyboard::Y))
{
mPlayer.setIsBatteryDisplay(true);
}
break;
case(sf::Event::KeyReleased):
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::S))
mPlayer.setIsLeft(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !sf::Keyboard::isKeyPressed(sf::Keyboard::D))
mPlayer.setIsRight(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !sf::Keyboard::isKeyPressed(sf::Keyboard::S))
mPlayer.setIsDown(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::W))
mPlayer.setIsUp(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::E) || sf::Keyboard::isKeyPressed(sf::Keyboard::T))
mPlayer.setIsHealthDisplay(false);
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Q) || sf::Keyboard::isKeyPressed(sf::Keyboard::Y))
mPlayer.setIsBatteryDisplay(false);
break;
default:
break;
}
}
void Game::addLaser(float pX, float pY, float xVel, float yVel)
{
mLasers.insert(mLasers.begin(), new Laser(sf::Vector2f(pX, pY), sf::Vector2f(xVel,yVel), sf::Vector2i(16,16), (mTextureHolder->getTexture(sLASER))));
}
void Game::removeLaser(int pIndex)
{
delete(mLasers.at(pIndex));
mLasers.erase((mLasers.begin() + pIndex));
}
//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);
}
*/
}