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