-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
334 lines (262 loc) · 10.7 KB
/
Copy pathGame.cpp
File metadata and controls
334 lines (262 loc) · 10.7 KB
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
// Copyright 2018 Corey Selover
#include "Game.h"
// External libs
#include <fstream>
// Project files
#include "Console.h"
#include "Manager.h"
#include "Entity.h"
#include "Splash.h"
#include "Map.h"
#include "Texture.h"
#include "HudObject.h"
#include "Tile.h"
Game::Game() {}
BootError Game::boot() {
// Set random seed.
std::srand(std::time(nullptr));
loadGameValues();
// Window
m_window.create(sf::VideoMode::getFullscreenModes()[0], "Genesis", sf::Style::Fullscreen);
//m_window.create(sf::VideoMode(800,600), "Genesis");
m_window.setVerticalSyncEnabled(true);
// Set up managers
m_managers[TEXTURE_MANAGER] = new Manager();
m_managers[SCREEN_MANAGER] = new Manager();
m_managers[HUD_MANAGER] = new Manager();
// Console
m_console = new Console(this);
// TODO - turn these back on for release.
//m_managers[SCREEN_MANAGER]->add("happy_rock", new Splash(this, "resources/textures/splash.png", sf::seconds(3.0f)));
//m_managers[SCREEN_MANAGER]->add("disclaimer", new Splash(this, "resources/textures/splash2.png", sf::seconds(3.0f)));
m_managers[SCREEN_MANAGER]->add("map", new Map(this));
//m_screenQueue.push("happy_rock");
//m_screenQueue.push("disclaimer");
m_screenQueue.push("map");
m_currentScreen = m_screenQueue.front();
// HUD
// TODO - Does this belong here?
m_managers[HUD_MANAGER]->add("mana_hud", new ManaHudObject(this,
"resources/textures/TEXTURE_MANA.png",
sf::IntRect(0, 0, 300, 50), 30, 60, 3, true));
m_managers[HUD_MANAGER]->add("grass_aura_hud", new AuraHudObject(this,
"resources/textures/TEXTURE_AURA_GRASS.png",
sf::IntRect(0, 0, 64, 64), 360, 70, 3, true));
m_managers[HUD_MANAGER]->add("rock_aura_hud", new AuraHudObject(this,
"resources/textures/TEXTURE_AURA_ROCK.png",
sf::IntRect(0, 0, 64, 64), 440, 70, 3, true));
m_managers[HUD_MANAGER]->add("water_aura_hud", new AuraHudObject(this,
"resources/textures/TEXTURE_AURA_WATER.png",
sf::IntRect(0, 0, 64, 64), 520, 70, 3, true));
m_running = true;
return BOOT_SUCCESS;
}
RunError Game::run() {
float clockRemainder = 0;
while(m_running) {
/* We do this now to ensure that events (particularly mouse clicks)
* are relative to the "world" and not, say the HUD.
*/
m_window.setView(static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->getView());
sf::Event event;
while (m_window.pollEvent(event)) {
processInput(event);
}
// No more screens to display. Time to quit.
if(m_screenQueue.empty()) {
shutdown();
}
if(!m_running) return RUN_SUCCESS;
if(m_gameClock.getElapsedTime().asSeconds() > 1/60.f - clockRemainder) {
// Update managers as appropriate.
m_currentScreen = m_screenQueue.front();
m_managers[SCREEN_MANAGER]->get(m_currentScreen)->update();
if (m_currentScreen.compare("map") == 0) m_managers[HUD_MANAGER]->update();
m_console->update();
clockRemainder = m_gameClock.restart().asSeconds() - 1/60.f;
// Draw game objects, screens, etc.
m_window.clear();
m_managers[SCREEN_MANAGER]->get(m_currentScreen)->draw();
// Draw console
// TODO - this line should be unnecessary if HUD_MANAGER is the last to draw.
// Might be good to keep it in place in case manager updating gets shuffled.
m_window.setView(m_window.getDefaultView());
m_managers[HUD_MANAGER]->draw();
m_console->draw();
// Display
m_window.display();
}
}
return RUN_SUCCESS;
}
void Game::processInput(sf::Event event) {
if (event.type == sf::Event::Closed) {
shutdown();
}
// Keyboard
if (event.type == sf::Event::TextEntered && m_console->active()) {
m_console->handle(event);
}
else if (event.type == sf::Event::KeyPressed) {
// Meta keys
if(event.key.code == sf::Keyboard::Tilde) {
m_console->toggle();
}
// Player control or GUI interaction
else if(!m_console->active()) {
static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->processInput(event);
}
}
else if (event.type == sf::Event::KeyReleased) {
// Player control or GUI interaction
if(!m_console->active()) {
static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->processInput(event);
}
}
// Mouse
else if (event.type == sf::Event::MouseButtonPressed) {
static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->processInput(event);
}
else if (event.type == sf::Event::MouseButtonReleased) {
static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->processInput(event);
}
}
ShutdownError Game::shutdown() {
m_running = false;
delete m_console;
// Shutdown managers
std::map<ManagerType, Manager*>::iterator itr;
for(itr = m_managers.begin(); itr != m_managers.end(); itr++) {
itr->second->shutdown();
}
// Delete managers
for(itr = m_managers.begin(); itr != m_managers.end(); itr++) {
delete itr->second;
}
m_managers.clear();
m_window.close();
return SHUTDOWN_SUCCESS;
}
///////////////////////////////////
// Drawing and Screen //
///////////////////////////////////
void Game::draw(sf::Drawable& drawable) {
m_window.draw(drawable);
}
void Game::nextScreen() {
if(!m_screenQueue.empty()) {
m_screenQueue.pop();
}
if(!m_screenQueue.empty()) {
static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_screenQueue.front()))->restartClock();
}
}
///////////////////////////////////
// Getters and Setters //
///////////////////////////////////
Entity* Game::entity(std::string name) {
// TODO - this should throw an error if the entity doesn't exist
return static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(m_currentScreen))->entity(name);
}
Texture* Game::texture(std::string name) {
return static_cast<Texture*>(m_managers[TEXTURE_MANAGER]->get(name));
}
Texture* Game::addTexture(std::string filePath, bool repeated) {
return static_cast<Texture*>(m_managers[TEXTURE_MANAGER]->add(filePath, new Texture(this, filePath, repeated)));
}
Screen* Game::screen(std::string name) {
return static_cast<Screen*>(m_managers[SCREEN_MANAGER]->get(name));
}
Tile* Game::tileByGrid(int x, int y) {
return static_cast<Map*>(screen("map"))->tileByGrid(x, y);
}
Tile* Game::tileByPixels(float x, float y) {
return static_cast<Map*>(screen("map"))->tileByPixels(x, y);
}
HudObject* Game::hudObject(std::string name) {
return static_cast<HudObject*>(m_managers[HUD_MANAGER]->get(name));
}
void Game::setView(sf::View view) { m_window.setView(view); }
sf::Vector2f Game::windowCenter() { return sf::Vector2f(m_window.getSize().x / 2, m_window.getSize().y / 2); }
sf::Vector2u Game::windowSize() { return m_window.getSize(); }
const sf::View& Game::defaultView() { return m_window.getDefaultView(); }
sf::Vector2i Game::mapCenterAsPixels() { return sf::Vector2i(mapWidth() * tileWidth() / 2, mapHeight() * tileHeight() / 2); }
sf::Vector2i Game::mapCenterAsCoordinates() { return sf::Vector2i(mapWidth() / 2, mapHeight() / 2); }
sf::Vector2f Game::worldCoords(int x, int y) { return m_window.mapPixelToCoords(sf::Vector2i(x,y)); }
int Game::mapWidth() { return std::atoi(m_gameValues["map_width"].c_str()); }
int Game::mapHeight() { return std::atoi(m_gameValues["map_height"].c_str()); }
int Game::tileWidth() { return std::atoi(m_gameValues["tile_width"].c_str()); }
int Game::tileHeight() { return std::atoi(m_gameValues["tile_height"].c_str()); }
///////////////////////////////////
// Console //
///////////////////////////////////
void Game::runCommand(std::string command) {
std::vector<std::string>::iterator itr;
for(itr = m_commands.begin(); itr != m_commands.end(); itr++) {
if((*itr) == command) {
// Do stuff accordingly
m_console->clean();
m_console->toggle();
}
}
m_console->displayError("Command not found or syntax invalid:", command);
}
void Game::assignGameValue(std::string variable, std::string value, bool saveToDisk) {
if(m_gameValues.count(variable)) {
m_gameValues[variable] = value;
if(saveToDisk) {
saveGameValues();
}
m_console->clean();
m_console->toggle();
}
else {
m_console->displayError("Variable unknown or value not valid:", value);
}
}
std::string Game::getGameValue(std::string variable) {
if(m_gameValues.count(variable)) {
return m_gameValues[variable];
}
else {
m_console->displayError("Variable unknown:", variable);
return "ERROR";
}
}
void Game::loadGameValues() {
std::ifstream input( "data.ini" );
for( std::string line; getline( input, line ); )
{
std::string first = line.substr(0, line.find("="));
std::string second = line.substr(line.find("=") + 1);
/* The above code will set first and second to the same value if
* the line contains no = sign. In other words, if the line is
* something like [Map], we don't want to parse it and we can
* ignore it.
*/
if(first.compare(second) != 0) {
m_gameValues[first] = second;
}
}
}
void Game::saveGameValues() {
std::cout << "Saving values" << std::endl;
std::ofstream output;
// TODO - throw an error if this doesn't work.
output.open("data.ini");
std::map<std::string, std::string>::iterator itr;
for (itr = m_gameValues.begin(); itr != m_gameValues.end(); itr++) {
std::cout << "Writing: " << itr->first << "=" << itr->second << std::endl;
output<< itr->first << "=" << itr->second << std::endl;
}
output.close();
}
///////////////////////////////////
// Util //
///////////////////////////////////
void Game::printGameValues() {
std::map<std::string, std::string>::iterator itr;
for (itr = m_gameValues.begin(); itr != m_gameValues.end(); itr++) {
std::cout << itr->first << "=" << itr->second << std::endl;
}
}