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
RevLine 
[6501]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
[6502]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
[6521]28#include "p_node.h"
[6502]29#include "world_entity.h"
[6520]30#include "image_entity.h"
[6502]31#include "terrain.h"
[6521]32#include "camera.h"
[6502]33
[6504]34#include "event_handler.h"
[6524]35#include "graphics_engine.h"
[6841]36#include "object_manager.h"
[6504]37
[6841]38
[6502]39#include "cd_engine.h"
40
41
[6501]42using namespace std;
43
44
[6502]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
[6521]58  this->cameraVector = Vector(50.0, 0.0, 0.0);
[6835]59  this->menuLayer.push_back(new MenuLayer());
[6839]60  this->menuLayer.push_back(new MenuLayer());
[6862]61
[6837]62  this->layerIndex = 0;
[6862]63  this->menuSelectedIndex = 0;
[6521]64
[6502]65  this->loadParams(root);
66}
67
68
[6501]69/**
[6502]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
[6501]74 */
[6502]75SimpleGameMenu::~SimpleGameMenu ()
76{
77  PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n");
[6501]78
[6502]79  if( this->dataTank)
80    delete this->dataTank;
81}
[6501]82
[6502]83
84/**
85 * loads the parameters of a SimpleGameMenu from an XML-element
86 * @param root the XML-element to load from
[6501]87 */
[6502]88void SimpleGameMenu::loadParams(const TiXmlElement* root)
89{
90  /* skip the GameWorld, since it does not define any useful loadParams for this class */
[6696]91  //static_cast<GameWorld*>(this)->loadParams(root);
92  GameWorld::loadParams(root);
[6501]93
[6502]94  PRINTF(4)("Loaded SimpleGameMenu specific stuff\n");
95}
[6501]96
[6502]97
[6501]98/**
[6504]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);
[6854]113  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_ESCAPE);
[6521]114
115  this->dataTank->localCamera->setRelCoor(this->cameraVector);
[6524]116
117  GraphicsEngine::getInstance()->displayFPS(false);
[6862]118
119  this->layerIndex = 0;
120  this->menuSelectedIndex = 0;
[6504]121}
122
123
[6524]124/**
125 * load the data
126 */
[6521]127ErrorMessage SimpleGameMenu::loadData()
[6504]128{
[6521]129  GameWorld::loadData();
[6504]130
[6845]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 )
[6852]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        }
[6851]153        element = element->NextSiblingElement();
[6845]154      }
155      PRINTF(4)("Done loading Elements\n");
156    }
157  }
158
[6520]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);
[6835]172      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
173
[6520]174    }
175    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
176    {
177      this->menuStartMultiplayerGame = dynamic_cast<ImageEntity*>(*entity);
[6835]178      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
[6520]179    }
180    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
181    {
182      this->menuQuitGame = dynamic_cast<ImageEntity*>(*entity);
[6835]183      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
[6520]184    }
185  }
186  this->menuSelectedIndex = 0;
[6835]187  this->menuSelected = this->menuLayer[0]->menuList[this->menuSelectedIndex];
[6520]188  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
[6839]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());
[6841]201      ImageEntity* ie = new ImageEntity();
[6853]202      PRINTF(0)("setting texture to: |%s|\n", se->getMenuItemImage());
[6848]203      ie->setTexture(se->getMenuItemImage());
[6854]204      ie->setRelCoor(0.0f,20.0f - (this->menuLayer[1]->menuList.size() * 10.0f), 0.0f);
[6848]205      ie->setVisibility(false);
206      this->menuLayer[1]->menuList.push_back(ie);
[6839]207    }
208  }
[6521]209}
[6520]210
[6521]211
[6862]212ErrorMessage SimpleGameMenu::unloadData()
213{
214  GameWorld::unloadData();
[6521]215
[6862]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
[6524]228/**
229 * start the menu
230 */
[6521]231bool SimpleGameMenu::start()
232{
233  EventHandler::getInstance()->pushState(ES_MENU);
234
[6504]235  /* now call the underlying*/
236  GameWorld::start();
237}
238
239
240
[6524]241/**
242 * stop the menu
243 */
[6504]244bool SimpleGameMenu::stop()
245{
246  EventHandler::getInstance()->popState();
247
248  /* now call the underlying*/
249  GameWorld::stop();
250}
251
252
[6521]253/**
254 *  override the standard tick for more functionality
255 */
256void SimpleGameMenu::tick()
257{
258  GameWorld::tick();
[6504]259
[6521]260  this->animateScene(this->dt);
261}
262
263
[6504]264/**
[6521]265 *  no collision detection in the menu
[6501]266 */
[6502]267void SimpleGameMenu::collide()
[6521]268{
269//   this->dataTank->localCamera->
270}
[6501]271
272
[6504]273/**
[6521]274 *  animate the scene
275 */
276void SimpleGameMenu::animateScene(float dt)
277{
[6524]278  Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0));
[6521]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/**
[6504]286 * event dispatcher funciton
287 * @param event the incoming event
288 */
289void SimpleGameMenu::process(const Event &event)
290{
[6839]291  /* ----------------- LAYER 1 ---------------*/
[6837]292  if( this->layerIndex == 0)
[6504]293  {
[6837]294    if( event.type == SDLK_RETURN && event.bPressed == true)
[6520]295    {
[6837]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
[6839]304        if( this->menuLayer[1]->menuList.size() == 0)
[6837]305        {
[6839]306          PRINTF(1)("Haven't got any StoryEntities to play!\n");
[6837]307          return;
308        }
309
310        this->switchMenuLayer(this->layerIndex, 1);
311      }
[6520]312    }
[6854]313    if( event.type == SDLK_ESCAPE && event.bPressed == true)
314    {
315      this->setNextStoryID(WORLD_ID_GAMEEND);
316      this->stop();
317    }
[6839]318  }  /* ----------------- LAYER 2 ---------------*/
[6837]319  else if( this->layerIndex == 1)
[6520]320  {
[6848]321    if( event.type == SDLK_RETURN && event.bPressed == true)
322    {
323      this->setNextStoryID( this->menuLayer[1]->storyList[this->menuSelectedIndex]->getStoryID());
324      this->stop();
325    }
[6854]326    if( event.type == SDLK_ESCAPE && event.bPressed == true)
327    {
328      this->switchMenuLayer(this->layerIndex, 0);
329    }
[6520]330  }
[6853]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  }
[6504]351}
[6502]352
353
[6837]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;
[6502]364
365
[6837]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  {
[6848]371    (*it)->setVisibility(false);
[6837]372  }
[6502]373
374
[6837]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++ )
[6848]378  {
379    (*it)->setVisibility(true);
380  }
[6837]381
382  this->layerIndex = layer2;
[6854]383  this->menuSelected = this->menuLayer[layer2]->menuList[0];
384  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
[6855]385  this->menuSelectedIndex = 0;
[6837]386}
387
388
389
390
[6502]391/**********************************************************************************************
392    SimpleGameMenuData
393 **********************************************************************************************/
394
395
[6501]396/**
[6502]397 * SimpleGameMenuData constructor
[6501]398 */
[6502]399SimpleGameMenuData::SimpleGameMenuData()
[6501]400{}
401
402/**
[6502]403 * SimpleGameMenuData decontructor
[6501]404 */
[6502]405SimpleGameMenuData::~SimpleGameMenuData()
[6501]406{}
407
408
409/**
[6502]410 *  initialize the GameWorldDataData
[6501]411 */
[6502]412ErrorMessage SimpleGameMenuData::init()
413{
414  /* call underlying function */
415  GameWorldData::init();
416}
[6501]417
418
419/**
[6502]420 *  loads the GUI data
421 * @param root reference to the xml root element
[6501]422 */
[6502]423ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
424{
425  /* call underlying function */
426  GameWorldData::loadGUI(root);
427}
[6501]428
429
430/**
[6502]431 *  unloads the GUI data
[6501]432 */
[6502]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.