-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
276 lines (242 loc) · 5.19 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
#include "Game.h"
void game::initstates()
{
this->states.push(new MainMenuState(this->window, &supportedkeys, &states));
}
void game::initkeybinds()
{
ifstream ifs("Config/supportedkeys.ini");
if (ifs.is_open())
{
string key = "";
int key_value = 0;
while (ifs >> key >> key_value)
{
this->supportedkeys[key] = key_value;
}
}
}
// void game::initenimies()
// {
// this->Enemy.setSize(Vector2f(50.f, 50.f));
// this->Enemy.setScale(Vector2f(0.5f, 0.5f));
// this->Enemy.setFillColor(Color::Green);
// this->Enemy.setPosition(50.f, 50.f);
// }
void game::initvars()
{
this->window = nullptr;
this->fullscreen = false;
this->dt = 0.f;
}
void game::initview()
{
}
void game::initwindow()
{
//Creates an sfml window by using options from window.ini file
ifstream ifs("Config/Window.ini");
this->videomodes = VideoMode::getFullscreenModes();
string title = "None";
VideoMode window_bounds= VideoMode::getDesktopMode();
unsigned framerate_limit = 120;
unsigned antialiasing_level = 0;
bool fullscreen = false;
bool veritcal_sync_enabled = false;
if (ifs.is_open())
{
getline(ifs, title);
ifs >> window_bounds.width >> window_bounds.height;
ifs >> fullscreen;
ifs >> framerate_limit;
ifs >> veritcal_sync_enabled;
ifs >> antialiasing_level;
}
else
{
cout << "ERROR::GAME::INITWINDOW::Failed to load window.ini file" << endl;
}
ifs.close();
this->fullscreen = fullscreen;
this->window_settings.antialiasingLevel = antialiasing_level;
if(this->fullscreen)
this->window = new RenderWindow(window_bounds, title, Style::Fullscreen, window_settings);
else
this->window = new RenderWindow(window_bounds, title, Style::Titlebar | Style::Close, window_settings);
this->window->setFramerateLimit(framerate_limit);
this->window->setVerticalSyncEnabled(veritcal_sync_enabled);
}
game::game()
{
this->initvars();
this->initwindow();
this->initkeybinds();
this->initstates();
// this->initenimies();
}
game::~game()
{
delete this->window;
while(this->states.empty() == false)
{
delete this->states.top();
this->states.pop();
}
}
void game::run()
{
while (this->window->isOpen())
{
this->updatedt();
this->update();
this->render();
}
}
/*
void game::updateenemy()
{
//spawn the enemy and set the timer to zero
if (this->enemies.size() < this->maxEnemies)
{
if (this->enemySpawnTimer >= this->enemySpawnTimerMax)
{
//spawn the enemy and reset the timer
this->spawnenemy();
this->enemySpawnTimer = 0.f;
}
else
this->enemySpawnTimer += 1.f;
}
//Move & Update the Enemy
for (unsigned int i = 0; i < this->enemies.size(); i++)
{
this->enemies[i].move(0.f, 5.f);
//if the enemy is out of the screen
if (this->enemies[i].getPosition().y > this->window->getSize().y)
{
this->enemies.erase(this->enemies.begin() + i);
this->health -= 1;
cout << "Health: " << this->health << endl;
if (health == 0)
{
this->endgame = true;
}
}
}
//if the enemy is clicked
if (Mouse::isButtonPressed(Mouse::Left))
{
if (mousehold == false)
{
this->mousehold = true;
bool deleted = false;
for (unsigned int i = 0; i < this->enemies.size() && deleted == false; i++)
{
if (this->enemies[i].getGlobalBounds().contains(this->mouseposview))
{
deleted = true;
this->enemies.erase(this->enemies.begin() + i);
this->points += 1;
cout << this->points << endl;
}
}
}
}
else
{
this->mousehold = false;
}
}
*/
bool game::getwindowisopen()
{
return window->isOpen();
}
void game::endapplication()
{
cout << "Ending Application" << endl;
}
// void game::updateCollision()
// {
// //Check Collision
// for(size_t i = 0; i < this->balls.size(); i++)
// {
// if(this->player.getShape().getGlobalBounds().intersects(this->balls[i].getShape().getGlobalBounds()))
// {
// //Check Ball Type
// switch(this->balls[i].gettype())
// {
// case balltypes::DEFAULT:
// this->points+=1;
// break;
// case balltypes::DAMAGING:
// this->player.takeDamage(1);
// this->points-=1;
// break;
// case balltypes::HEALING:
// this->player.gainHealth(1);
// break;
// default:
// break;
// }
// if(this->player.getHP()==0)
// {
// this->endgame = true;
// }
// //Add points
// //Erase Ball
// this->balls.erase(this->balls.begin() + i);
// }
// }
// }
void game::updateEvents()
{
while (window->pollEvent(ev))
{
switch (ev.type)
{
case Event::Closed:
window->close();
break;
// case Event::KeyPressed:
// if (ev.key.code == Keyboard::Escape)
// window->close();
// break;
default:
break;
}
}
}
void game::render()
{
window->clear();
if(!this->states.empty())
this->states.top()->render(this->window);
window->display();
}
void game::updatedt()
{
this->dt = this->clock.restart().asSeconds();
}
void game::update()
{
this->updateEvents();
if(!this->states.empty())
{
this->states.top()->update(this->dt,this->window);
if(this->states.top()->getquit())
{
this->states.top()->endState();
delete this->states.top();
this->states.pop();
}
}
else
{
this->endapplication();
this->window->close();
}
//this->updateText();
// this->player.update(this->window);
// this->updateCollision();
}