-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
167 lines (130 loc) · 4.16 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
#include "Game.hpp"
Game::Game(){
// Initialize variable
points = 0;
enemySpawnTimerMax = 10.f;
enemySpawnTimer = enemySpawnTimerMax;
maxEnemies = 20;
// Initialize Window
window.create(sf::VideoMode(720,960), "SFML Window");
window.setFramerateLimit(144);
window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width/2-window.getSize().x/2,0.f));
// Initialize Backgroud
if(!worldBackgroundTex.loadFromFile("./content/texture/background.jpg"))
std::cout << "Error loading from file" << std::endl;
worldBackground.setTexture(worldBackgroundTex);
worldBackground.setScale(0.3f,0.3f);
// Initialize Fonts
if(!font.loadFromFile("./content/fonts/BebasNeue-Regular.ttf")) {
std::cout << "Error loading from file" << std::endl;
}
// Initialize Text
uiText.setFont(font);
uiText.setCharacterSize(24);
uiText.setFillColor(sf::Color::White);
uiText.setString("NONE");
gameOverText.setFont(font);
gameOverText.setCharacterSize(80);
gameOverText.setFillColor(sf::Color::Red);
gameOverText.setString("NONE");
}
void Game::processInput(){
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
if (event.Event::KeyPressed && event.Event::key.code == sf::Keyboard::Escape)
window.close();
if (event.Event::KeyPressed && event.Event::key.code == sf::Keyboard::Num0)
player.setHp(1);
}
}
void Game::spawnEnemy(){
if(enemySpawnTimer < enemySpawnTimerMax)
enemySpawnTimer += 1.f;
else{
if(enemies.size() < maxEnemies){
enemies.push_back(new Enemy(window));
enemySpawnTimer = 0.f;
}
}
}
void Game::updateEnemyAndCombat(){
spawnEnemy();
for(int i=0; i<enemies.size(); i++){
enemies[i]->update();
auto playerBullets = player.getBullets();
// Combat
for(size_t j = 0; j<playerBullets.size() ; j++){
if(playerBullets[j]->getBounds().intersects(enemies[i]->getBounds())){
points += enemies[i]->getPoints();
playerBullets.erase(playerBullets.begin()+j);
player.removeBullet(j);
delete enemies.at(i);
enemies.erase(enemies.begin()+i);
}
}
// Culling
if(enemies[i]->getBounds().top > window.getSize().y){
delete enemies.at(i);
enemies.erase(enemies.begin() + i);
}
// Player
else if(enemies[i]->getBounds().intersects(player.getBounds())){
player.loseHp(enemies[i]->getDamage());
delete enemies.at(i);
enemies.erase(enemies.begin() + i);
}
}
}
void Game::updateText(){
std::stringstream ss;
ss << "Hp: "<< player.getHp() << "/" << player.getMaxHp() << "\n"
<< "Points: " << points << "\n"
<< "WASD: move \n"
<< "Spacebar: shoot\n";
uiText.setString(ss.str());
}
void Game::update(sf::Time deltaTime){
if(player.getHp()>0){
processInput();
updateEnemyAndCombat();
player.update(window);
updateText();
}
}
void Game::renderWorld(sf::RenderTarget& target){
target.draw(worldBackground);
}
void Game::renderText(sf::RenderTarget& target){
target.draw(uiText);
}
void Game::renderEnemies(sf::RenderTarget& target){
for(auto* e : enemies){
e->render(window);
}
}
void Game::render() {
window.clear();
renderWorld(window);
renderText(window);
renderEnemies(window);
player.render(window);
if(player.getHp() <= 0){
window.clear();
std::stringstream ss;
ss <<"Game Over!\n"
<< "You get " << points << " points\n";
gameOverText.setString(ss.str());
gameOverText.setPosition(window.getSize().x/2-gameOverText.getLocalBounds().width/2,window.getSize().y/2-gameOverText.getLocalBounds().height/2);
window.draw(gameOverText);
}
window.display();
}
void Game::run(){
sf::Clock clock;
while(window.isOpen()){
processInput();
update(clock.restart());
render();
}
}