-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
134 lines (114 loc) · 3.26 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
#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(200,200), sf::Vector2f(0,0), sf::Vector2i(16,16), (pSpriteSheet->getTexture(sPLAYER)))
{
//hardcoded enemies because time
for(int i = 0; i < 100; i++)
{
addEnemy();
}
}
Game::~Game(void)
{
}
//TODO: paramertertize this method to input the valuves to be used for the enemies
void Game::addEnemy()
{
//if no delete before removed u get memory leaks
mEnemies.insert(mEnemies.begin(), new Enemy(sf::Vector2f(450,400), sf::Vector2f(0,0), sf::Vector2i(32,32), (mTextures->getTexture(sBURGER))));
}
void Game::removeEnemy(int pIndex)
{
delete(mEnemies.at(pIndex));
mEnemies.erase((mEnemies.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()
{
//call each entity update
mPlayer.update();
for(int i = 0; i < mEnemies.size(); i++)
{
//bounds check each enemy with player
if(mPlayer.getPosition().x < (mEnemies[i]->getPosition().x - 50) && mPlayer.getPosition().x > (mEnemies[i]->getPosition().x - 80))
mEnemies[i]->setEnemyState(eRIGHT);
else if(mPlayer.getPosition().x > (mEnemies[i]->getPosition().x + 50) && mPlayer.getPosition().x < (mEnemies[i]->getPosition().x + 80))
mEnemies[i]->setEnemyState(eLEFT);
else mEnemies[i]->setEnemyState(eNONE);
//update enemy
mEnemies[i]->update();
//collision detection with player
if(mEnemies[i]->getBounds().intersects(mPlayer.getBounds()))
{
removeEnemy(i);
continue;
}
//kill enemy if off screen
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)
{
removeEnemy(i);
continue;
}
}
//check for gameover state
//if(mEnemies.size() == 0)
// mGameState = gGAMEOVER;
}
//just call draw of all entities
void Game::draw(sf::RenderWindow *window, float pInterpolation)
{
mPlayer.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))
mPlayer.setPlayerState(pLEFT);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
mPlayer.setPlayerState(pRIGHT);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
mPlayer.setPlayerState(pDOWN);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
mPlayer.setPlayerState(pUP);
//created to test for memory leaks
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
addEnemy();
}
break;
case(sf::Event::KeyReleased):
mPlayer.setPlayerState(pNONE);
break;
default:
break;
}
}
//TODO: impliment reset
void Game::reset()
{
}
//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);
}
}