1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
17 | |
---|
18 | |
---|
19 | #include "simple_game_menu.h" |
---|
20 | |
---|
21 | #include "state.h" |
---|
22 | #include "class_list.h" |
---|
23 | |
---|
24 | #include "load_param.h" |
---|
25 | #include "fast_factory.h" |
---|
26 | #include "factory.h" |
---|
27 | |
---|
28 | #include "p_node.h" |
---|
29 | #include "world_entity.h" |
---|
30 | #include "image_entity.h" |
---|
31 | #include "terrain.h" |
---|
32 | #include "camera.h" |
---|
33 | |
---|
34 | #include "event_handler.h" |
---|
35 | #include "graphics_engine.h" |
---|
36 | |
---|
37 | #include "cd_engine.h" |
---|
38 | |
---|
39 | |
---|
40 | using namespace std; |
---|
41 | |
---|
42 | |
---|
43 | //! This creates a Factory to fabricate a SimpleGameMenu |
---|
44 | CREATE_FACTORY(SimpleGameMenu, CL_SIMPLE_GAME_MENU); |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | SimpleGameMenu::SimpleGameMenu(const TiXmlElement* root) |
---|
49 | : GameWorld(root) |
---|
50 | { |
---|
51 | this->setClassID(CL_SIMPLE_GAME_MENU, "SimpleGameMenu"); |
---|
52 | this->setName("SimpleGameMenu uninitialized"); |
---|
53 | |
---|
54 | this->dataTank = new SimpleGameMenuData(); |
---|
55 | |
---|
56 | this->cameraVector = Vector(50.0, 0.0, 0.0); |
---|
57 | this->menuLayer.push_back(new MenuLayer()); |
---|
58 | this->menuLayer.push_back(new MenuLayer()); |
---|
59 | this->layerIndex = 0; |
---|
60 | |
---|
61 | this->loadParams(root); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | * remove the SimpleGameMenu from memory |
---|
67 | * |
---|
68 | * delete everything explicitly, that isn't contained in the parenting tree! |
---|
69 | * things contained in the tree are deleted automaticaly |
---|
70 | */ |
---|
71 | SimpleGameMenu::~SimpleGameMenu () |
---|
72 | { |
---|
73 | PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n"); |
---|
74 | |
---|
75 | if( this->dataTank) |
---|
76 | delete this->dataTank; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * loads the parameters of a SimpleGameMenu from an XML-element |
---|
82 | * @param root the XML-element to load from |
---|
83 | */ |
---|
84 | void SimpleGameMenu::loadParams(const TiXmlElement* root) |
---|
85 | { |
---|
86 | /* skip the GameWorld, since it does not define any useful loadParams for this class */ |
---|
87 | //static_cast<GameWorld*>(this)->loadParams(root); |
---|
88 | GameWorld::loadParams(root); |
---|
89 | |
---|
90 | PRINTF(4)("Loaded SimpleGameMenu specific stuff\n"); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * this is executed just before load |
---|
96 | * |
---|
97 | * since the load function sometimes needs data, that has been initialized |
---|
98 | * before the load and after the proceeding storyentity has finished |
---|
99 | */ |
---|
100 | ErrorMessage SimpleGameMenu::init() |
---|
101 | { |
---|
102 | /* call underlying init funciton */ |
---|
103 | GameWorld::init(); |
---|
104 | |
---|
105 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_UP); |
---|
106 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_DOWN); |
---|
107 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_RETURN); |
---|
108 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_SPACE); |
---|
109 | |
---|
110 | this->dataTank->localCamera->setRelCoor(this->cameraVector); |
---|
111 | |
---|
112 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | * load the data |
---|
118 | */ |
---|
119 | ErrorMessage SimpleGameMenu::loadData() |
---|
120 | { |
---|
121 | GameWorld::loadData(); |
---|
122 | |
---|
123 | /* get the menu list */ |
---|
124 | const std::list<BaseObject*>* imageEntityList = ClassList::getList(CL_IMAGE_ENTITY); |
---|
125 | std::list<BaseObject*>::const_iterator entity; |
---|
126 | for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++) |
---|
127 | { |
---|
128 | |
---|
129 | if( !strcmp("Selector_Menu", (*entity)->getName())) |
---|
130 | { |
---|
131 | this->menuSelector = dynamic_cast<ImageEntity*>(*entity); |
---|
132 | } |
---|
133 | else if( !strcmp( "StartGame_Menu", (*entity)->getName())) |
---|
134 | { |
---|
135 | this->menuStartGame = dynamic_cast<ImageEntity*>(*entity); |
---|
136 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
137 | |
---|
138 | } |
---|
139 | else if( !strcmp( "Multiplayer_Menu", (*entity)->getName())) |
---|
140 | { |
---|
141 | this->menuStartMultiplayerGame = dynamic_cast<ImageEntity*>(*entity); |
---|
142 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
143 | } |
---|
144 | else if( !strcmp( "Quit_Menu", (*entity)->getName())) |
---|
145 | { |
---|
146 | this->menuQuitGame = dynamic_cast<ImageEntity*>(*entity); |
---|
147 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
148 | } |
---|
149 | } |
---|
150 | this->menuSelectedIndex = 0; |
---|
151 | this->menuSelected = this->menuLayer[0]->menuList[this->menuSelectedIndex]; |
---|
152 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
153 | |
---|
154 | |
---|
155 | // loading the storyentities submenu (singleplayer) |
---|
156 | const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY); |
---|
157 | std::list<BaseObject*>::const_iterator it; |
---|
158 | for( it = storyEntities->begin(); it != storyEntities->end(); it++) |
---|
159 | { |
---|
160 | StoryEntity* se = dynamic_cast<StoryEntity*>(*it); |
---|
161 | if( se->isContainedInMenu()) |
---|
162 | { |
---|
163 | this->menuLayer[1]->storyList.push_back(se); |
---|
164 | PRINTF(0)("Got a new menu entry |%s|\n", se->getName()); |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | /** |
---|
172 | * start the menu |
---|
173 | */ |
---|
174 | bool SimpleGameMenu::start() |
---|
175 | { |
---|
176 | EventHandler::getInstance()->pushState(ES_MENU); |
---|
177 | |
---|
178 | /* now call the underlying*/ |
---|
179 | GameWorld::start(); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | * stop the menu |
---|
186 | */ |
---|
187 | bool SimpleGameMenu::stop() |
---|
188 | { |
---|
189 | EventHandler::getInstance()->popState(); |
---|
190 | |
---|
191 | /* now call the underlying*/ |
---|
192 | GameWorld::stop(); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /** |
---|
197 | * override the standard tick for more functionality |
---|
198 | */ |
---|
199 | void SimpleGameMenu::tick() |
---|
200 | { |
---|
201 | GameWorld::tick(); |
---|
202 | |
---|
203 | this->animateScene(this->dt); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /** |
---|
208 | * no collision detection in the menu |
---|
209 | */ |
---|
210 | void SimpleGameMenu::collide() |
---|
211 | { |
---|
212 | // this->dataTank->localCamera-> |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | * animate the scene |
---|
218 | */ |
---|
219 | void SimpleGameMenu::animateScene(float dt) |
---|
220 | { |
---|
221 | Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0)); |
---|
222 | this->cameraVector = q.apply(this->cameraVector); |
---|
223 | this->dataTank->localCamera->setRelCoor(this->cameraVector); |
---|
224 | this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /** |
---|
229 | * event dispatcher funciton |
---|
230 | * @param event the incoming event |
---|
231 | */ |
---|
232 | void SimpleGameMenu::process(const Event &event) |
---|
233 | { |
---|
234 | /* ----------------- LAYER 1 ---------------*/ |
---|
235 | if( this->layerIndex == 0) |
---|
236 | { |
---|
237 | if( event.type == SDLK_RETURN && event.bPressed == true) |
---|
238 | { |
---|
239 | if( this->menuSelected == this->menuQuitGame) |
---|
240 | { |
---|
241 | this->setNextStoryID(WORLD_ID_GAMEEND); |
---|
242 | this->stop(); |
---|
243 | } |
---|
244 | if( this->menuSelected == this->menuStartGame) |
---|
245 | { |
---|
246 | //this->stop(); |
---|
247 | // switch to first submenu |
---|
248 | if( this->menuLayer[1]->menuList.size() == 0) |
---|
249 | { |
---|
250 | PRINTF(1)("Haven't got any StoryEntities to play!\n"); |
---|
251 | return; |
---|
252 | } |
---|
253 | |
---|
254 | this->switchMenuLayer(this->layerIndex, 1); |
---|
255 | |
---|
256 | } |
---|
257 | } |
---|
258 | else if( event.type == SDLK_DOWN && event.bPressed == true) |
---|
259 | { |
---|
260 | // ImageEntity* |
---|
261 | if(this->menuSelectedIndex < (this->menuLayer[this->layerIndex]->menuList.size() - 1)) |
---|
262 | { |
---|
263 | this->menuSelected = this->menuLayer[this->layerIndex]->menuList[++this->menuSelectedIndex]; |
---|
264 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
265 | } |
---|
266 | } |
---|
267 | else if( event.type == SDLK_UP && event.bPressed == true) |
---|
268 | { |
---|
269 | if(this->menuSelectedIndex > 0) |
---|
270 | { |
---|
271 | this->menuSelected = this->menuLayer[this->layerIndex]->menuList[--this->menuSelectedIndex]; |
---|
272 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
273 | } |
---|
274 | } |
---|
275 | } /* ----------------- LAYER 2 ---------------*/ |
---|
276 | else if( this->layerIndex == 1) |
---|
277 | { |
---|
278 | |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /** |
---|
284 | * switches to from one meny layer to an other |
---|
285 | * @param layer1 from layer |
---|
286 | * @param layer2 to layer |
---|
287 | */ |
---|
288 | void SimpleGameMenu::switchMenuLayer(int layer1, int layer2) |
---|
289 | { |
---|
290 | // wrong sizes |
---|
291 | if(layer1 >= this->menuLayer.size() || layer2 >= this->menuLayer.size()) |
---|
292 | return; |
---|
293 | |
---|
294 | |
---|
295 | PRINTF(0)("Removing layer %i\n", layer1); |
---|
296 | std::vector<ImageEntity*>::iterator it; |
---|
297 | // fade old menu |
---|
298 | for( it = this->menuLayer[layer1]->menuList.begin(); it != this->menuLayer[layer1]->menuList.end(); it++ ) |
---|
299 | { |
---|
300 | (*it)->setAbsCoor(Vector(-100, -100, -100)); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | PRINTF(0)("Showing layer %i\n", layer1); |
---|
305 | // beam here the new menu |
---|
306 | for( it = this->menuLayer[layer2]->menuList.begin(); it != this->menuLayer[layer2]->menuList.end(); it++ ) |
---|
307 | {} |
---|
308 | |
---|
309 | this->layerIndex = layer2; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | /********************************************************************************************** |
---|
316 | SimpleGameMenuData |
---|
317 | **********************************************************************************************/ |
---|
318 | |
---|
319 | |
---|
320 | /** |
---|
321 | * SimpleGameMenuData constructor |
---|
322 | */ |
---|
323 | SimpleGameMenuData::SimpleGameMenuData() |
---|
324 | {} |
---|
325 | |
---|
326 | /** |
---|
327 | * SimpleGameMenuData decontructor |
---|
328 | */ |
---|
329 | SimpleGameMenuData::~SimpleGameMenuData() |
---|
330 | {} |
---|
331 | |
---|
332 | |
---|
333 | /** |
---|
334 | * initialize the GameWorldDataData |
---|
335 | */ |
---|
336 | ErrorMessage SimpleGameMenuData::init() |
---|
337 | { |
---|
338 | /* call underlying function */ |
---|
339 | GameWorldData::init(); |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | /** |
---|
344 | * loads the GUI data |
---|
345 | * @param root reference to the xml root element |
---|
346 | */ |
---|
347 | ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root) |
---|
348 | { |
---|
349 | /* call underlying function */ |
---|
350 | GameWorldData::loadGUI(root); |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | /** |
---|
355 | * unloads the GUI data |
---|
356 | */ |
---|
357 | ErrorMessage SimpleGameMenuData::unloadGUI() |
---|
358 | { |
---|
359 | /* call underlying function */ |
---|
360 | GameWorldData::unloadGUI(); |
---|
361 | } |
---|
362 | |
---|
363 | |
---|
364 | /** |
---|
365 | * overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff) |
---|
366 | * @param root reference to the xml root parameter |
---|
367 | */ |
---|
368 | ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root) |
---|
369 | { |
---|
370 | TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
371 | |
---|
372 | if( element != NULL) |
---|
373 | { |
---|
374 | element = element->FirstChildElement(); |
---|
375 | PRINTF(4)("Loading WorldEntities\n"); |
---|
376 | while( element != NULL) |
---|
377 | { |
---|
378 | BaseObject* created = Factory::fabricate(element); |
---|
379 | if( created != NULL ) |
---|
380 | printf("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
381 | |
---|
382 | if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) |
---|
383 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
384 | if( element->Value() != NULL && !strcmp( element->Value(), "Terrain")) |
---|
385 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
386 | element = element->NextSiblingElement(); |
---|
387 | } |
---|
388 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
389 | } |
---|
390 | |
---|
391 | /* init the pnode tree */ |
---|
392 | PNode::getNullParent()->init(); |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | /** |
---|
397 | * unloads the world entities from the xml file |
---|
398 | */ |
---|
399 | ErrorMessage SimpleGameMenuData::unloadWorldEntities() |
---|
400 | { |
---|
401 | /* call underlying function */ |
---|
402 | GameWorldData::unloadWorldEntities(); |
---|
403 | } |
---|
404 | |
---|
405 | |
---|
406 | /** |
---|
407 | * loads the scene data |
---|
408 | * @param root reference to the xml root element |
---|
409 | */ |
---|
410 | ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root) |
---|
411 | { |
---|
412 | /* call underlying function */ |
---|
413 | GameWorldData::loadScene(root); |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | /** |
---|
418 | * unloads the scene data |
---|
419 | */ |
---|
420 | ErrorMessage SimpleGameMenuData::unloadScene() |
---|
421 | { |
---|
422 | /* call underlying function */ |
---|
423 | GameWorldData::unloadScene(); |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | |
---|