-
Notifications
You must be signed in to change notification settings - Fork 5
/
statemanager.cpp
346 lines (302 loc) · 8.32 KB
/
statemanager.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
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tinyxml/tinyxml.h"
#include "statemanager.h"
#include "gamestate.h"
#include "mainmenustate.h"
#include "creditsstate.h"
#include "levelendstate.h"
#include "pausestate.h"
#include "loadingmapstate.h"
#include "loadmapstate.h"
#include "optionsstate.h"
#include "upgradesstate.h"
StateManager::StateManager()
{
_gameState = new GameState(this);
_mainMenuState = new MainMenuState(this);
_creditsState = new CreditsState(this);
_levelEndState = new LevelEndState(this);
_pauseState = new PauseState(this);
_loadingMapState = new LoadingMapState(this);
_loadMapState = new LoadMapState(this);
_optionsState = new OptionsState(this);
_upgradesState = new UpgradesState(this);
_activeState = _mainMenuState;
_winX = 800;
_winY = 600;
_settingsChanged = false;
_stateChanged = false;
_activeMapFilename = "";
_mapMusicFilename = "";
_activeStateName = MAINMENU_SCREEN;
Locator::getAudio()->playMenuMusic();
}
StateManager::~StateManager()
{
LOGF((stdout, "Running Statemanager destructor!\n"));
_activeState = NULL;
delete _gameState;
delete _mainMenuState;
delete _creditsState;
delete _levelEndState;
delete _pauseState;
delete _loadingMapState;
delete _loadMapState;
delete _optionsState;
delete _upgradesState;
}
BaseState* StateManager::getGameState()
{
return _gameState;
}
BaseState* StateManager::getActiveState()
{
return _activeState;
}
void StateManager::switchToState(eState state)
{
GameState* gs = static_cast<GameState*>(_gameState);
UpgradesState* us = static_cast<UpgradesState*>(_upgradesState);
LoadingMapState* lms = static_cast<LoadingMapState*>(_loadingMapState);
int jp, jt, timeToSniper, old_x, old_y;
unsigned int ammo, energy;
_activeState->getMousePosition(&old_x, &old_y);
switch(state)
{
case MAINMENU_SCREEN:
Locator::getAudio()->playMenuMusic();
_activeState = _mainMenuState;
break;
case GAME_SCREEN:
if (_activeState == _loadingMapState)
{
us->getModifiers(&jp, &jt, &ammo, &timeToSniper, &energy);
gs->getScene()->addPlayerJumpPower((float)jp / 50.0f);
gs->getScene()->subPlayerJumpChargeTime(jt * 90);
gs->getScene()->_numPlayerBullets = ammo;
gs->getScene()->_timeToSniper = (timeToSniper * 1000);
gs->getScene()->_playerEnergy = energy;
}
_activeState = _gameState;
break;
case CREDITS_SCREEN:
_activeState = _creditsState;
break;
case LEVELEND_SCREEN:
_activeState = _levelEndState;
break;
case PAUSE_SCREEN:
_activeState = _pauseState;
break;
case LOADINGMAP_SCREEN:
Locator::getAudio()->stopMusic();
getMusicFilename(_activeMapFilename);
lms->setMap(_activeMapFilename, _mapMusicFilename);
_activeState = _loadingMapState;
break;
case LOADMAP_SCREEN:
_activeState = _loadMapState;
break;
case OPTIONS_SCREEN:
_activeState = _optionsState;
static_cast<OptionsState*>(_optionsState)->setLabels();
break;
case UPGRADES_SCREEN:
us->setMap(_activeMapFilename);
_activeState = _upgradesState;
break;
}
_stateChanged = true;
_activeStateName = state;
//done to prevent the mouse cursor sprite from being initially drawn in the old position.
_activeState->setMousePosition(old_x, old_y);
}
void StateManager::initSceneAndMap(const char* filename)
{
GameState* gs = static_cast<GameState*>(_gameState);
_activeMapFilename = std::string(filename);
if (!gs->getScene())
{
gs->initSceneAndMap(filename);
gs->getScene()->setCameraDims(_winX, _winY);
}
else
{
LOGF((stderr, "ERROR: Game state currently has a scene!\n"));
}
}
void StateManager::setWindowDims(int w, int h)
{
_winX = w;
_winY = h;
static_cast<MenuState*>(_mainMenuState)->resetPositions(w, h);
static_cast<MenuState*>(_creditsState)->resetPositions(w, h);
static_cast<MenuState*>(_levelEndState)->resetPositions(w, h);
static_cast<MenuState*>(_pauseState)->resetPositions(w, h);
static_cast<MenuState*>(_loadMapState)->resetPositions(w, h);
static_cast<MenuState*>(_optionsState)->resetPositions(w, h);
static_cast<MenuState*>(_upgradesState)->resetPositions(w, h);
}
void StateManager::destroyScene()
{
static_cast<GameState*>(_gameState)->deleteScene();
_activeMapFilename = "";
}
void StateManager::update(unsigned int dT)
{
_activeState->update(dT);
}
void StateManager::setActiveMapFilename(std::string filename)
{
_activeMapFilename = filename;
_mapMusicFilename = "";
}
void StateManager::getMapVariables(std::string filename, int* money, int* upgrades, unsigned int* bullets, int* timeToSniper, unsigned int* energy)
{
*money = 0;
*upgrades = 0;
*energy = 0;
*bullets = 0;
*timeToSniper = 1;
TiXmlDocument doc(filename.c_str());
if (!doc.LoadFile())
{
LOGF((stderr, "Failed to load map file %s.\n", filename.c_str()));
return;
}
TiXmlHandle hDoc(&doc);
TiXmlElement *root, *lvl1, *lvl2;
root = doc.FirstChildElement("map");
if(!root)
{
LOGF((stderr, "Failed to parse map file %s.\n", filename.c_str()));
return;
}
lvl1 = root->FirstChildElement("properties");
while(lvl1)
{
lvl2 = lvl1->FirstChildElement("property");
while (lvl2)
{
if (!strcmp(lvl2->Attribute("name"), "startingmoney"))
{
*money = atoi(lvl2->Attribute("value"));
}
if (!strcmp(lvl2->Attribute("name"), "startingupgrades"))
{
*upgrades = atoi(lvl2->Attribute("value"));
}
if (!strcmp(lvl2->Attribute("name"), "startingenergy"))
{
*energy = atoi(lvl2->Attribute("value"));
}
if (!strcmp(lvl2->Attribute("name"), "startingammo"))
{
*bullets = atoi(lvl2->Attribute("value"));
}
if (!strcmp(lvl2->Attribute("name"), "timetosniper"))
{
*timeToSniper = atoi(lvl2->Attribute("value"));
}
lvl2 = lvl2->NextSiblingElement("property");
}
lvl1 = lvl1->NextSiblingElement("properties");
}
}
void StateManager::getMusicFilename(std::string mapFilename)
{
TiXmlDocument doc(mapFilename.c_str());
if (!doc.LoadFile())
{
LOGF((stderr, "Failed to load map file %s.\n", mapFilename.c_str()));
return;
}
TiXmlHandle hDoc(&doc);
TiXmlElement *root, *lvl1, *lvl2;
root = doc.FirstChildElement("map");
if(!root)
{
LOGF((stderr, "Failed to parse map file %s.\n", mapFilename.c_str()));
return;
}
lvl1 = root->FirstChildElement("properties");
while(lvl1)
{
lvl2 = lvl1->FirstChildElement("property");
while (lvl2)
{
if (!strcmp(lvl2->Attribute("name"), "music"))
{
_mapMusicFilename = std::string(lvl2->Attribute("value"));
}
lvl2 = lvl2->NextSiblingElement("property");
}
lvl1 = lvl1->NextSiblingElement("properties");
}
}
int StateManager::getWindowWidth()
{
return _winX;
}
int StateManager::getWindowHeight()
{
return _winY;
}
void StateManager::makeStartSave()
{
GameState* gs = static_cast<GameState*>(_gameState);
gs->getScene()->saveGame("start.sav");
}
bool StateManager::settingsChanged()
{
OptionsState* os = static_cast<OptionsState*>(_optionsState);
return os->_settingsChanged;
}
void StateManager::resetSettingsFlag()
{
OptionsState* os = static_cast<OptionsState*>(_optionsState);
os->_settingsChanged = false;
}
bool StateManager::screenshotTaken()
{
return _gameState->tookScreenshot ||
_mainMenuState->tookScreenshot ||
_creditsState->tookScreenshot ||
_levelEndState->tookScreenshot ||
_pauseState->tookScreenshot ||
_loadMapState->tookScreenshot ||
_optionsState->tookScreenshot ||
_upgradesState->tookScreenshot;
}
void StateManager::resetScreenShotFlag()
{
_gameState->tookScreenshot = false;
_mainMenuState->tookScreenshot = false;
_creditsState->tookScreenshot = false;
_levelEndState->tookScreenshot = false;
_pauseState->tookScreenshot = false;
_loadMapState->tookScreenshot = false;
_optionsState->tookScreenshot = false;
_upgradesState->tookScreenshot = false;
}
bool StateManager::stateChanged()
{
return _stateChanged;
}
void StateManager::resetStateChangedFlag()
{
_stateChanged = false;
}