Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/simple_game_menu.cc @ 6862

Last change on this file since 6862 was 6862, checked in by patrick, 18 years ago

trunk: the game now always jumps back to the menu

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