Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6874 was 6874, checked in by patrick, 19 years ago

trunk: the menu control better implemented

File size: 12.6 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);
[6874]200
201      // generating menu item
[6841]202      ImageEntity* ie = new ImageEntity();
[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);
[6874]207
208      // generating screenshoot item
209//       ie = new ImageEntity();
210//       ie->setTexture(se->getMenuScreenshoot);
211//       ie->setRelCoor(.0f, 10.0f, 0.0f);
212//       this->menuLayer[1]->screenshootList.push_back(ie);
213
[6839]214    }
215  }
[6521]216}
[6520]217
[6521]218
[6862]219ErrorMessage SimpleGameMenu::unloadData()
220{
221  GameWorld::unloadData();
[6521]222
[6862]223  EventHandler::getInstance()->unsubscribe(this, ES_MENU);
224
225  std::vector<ImageEntity*>::iterator it;
226  std::vector<MenuLayer*>::iterator mit;
227  for(mit = this->menuLayer.begin(); mit != this->menuLayer.end(); mit++)
228  {
229    (*mit)->menuList.erase((*mit)->menuList.begin(), (*mit)->menuList.end());
230    (*mit)->storyList.erase((*mit)->storyList.begin(), (*mit)->storyList.end());
231  }
232}
233
234
[6524]235/**
236 * start the menu
237 */
[6521]238bool SimpleGameMenu::start()
239{
240  EventHandler::getInstance()->pushState(ES_MENU);
241
[6504]242  /* now call the underlying*/
243  GameWorld::start();
244}
245
246
247
[6524]248/**
249 * stop the menu
250 */
[6504]251bool SimpleGameMenu::stop()
252{
253  EventHandler::getInstance()->popState();
254
255  /* now call the underlying*/
256  GameWorld::stop();
257}
258
259
[6521]260/**
261 *  override the standard tick for more functionality
262 */
263void SimpleGameMenu::tick()
264{
265  GameWorld::tick();
[6504]266
[6521]267  this->animateScene(this->dt);
268}
269
270
[6504]271/**
[6521]272 *  no collision detection in the menu
[6501]273 */
[6502]274void SimpleGameMenu::collide()
[6521]275{
276//   this->dataTank->localCamera->
277}
[6501]278
279
[6504]280/**
[6521]281 *  animate the scene
282 */
283void SimpleGameMenu::animateScene(float dt)
284{
[6524]285  Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0));
[6521]286  this->cameraVector = q.apply(this->cameraVector);
287  this->dataTank->localCamera->setRelCoor(this->cameraVector);
288  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
289}
290
291
292/**
[6504]293 * event dispatcher funciton
294 * @param event the incoming event
295 */
296void SimpleGameMenu::process(const Event &event)
297{
[6839]298  /* ----------------- LAYER 1 ---------------*/
[6837]299  if( this->layerIndex == 0)
[6504]300  {
[6837]301    if( event.type == SDLK_RETURN && event.bPressed == true)
[6520]302    {
[6837]303      if( this->menuSelected == this->menuQuitGame)
304      {
305        this->setNextStoryID(WORLD_ID_GAMEEND);
306        this->stop();
307      }
308      if( this->menuSelected == this->menuStartGame)
309      {
310        // switch to first submenu
[6839]311        if( this->menuLayer[1]->menuList.size() == 0)
[6837]312        {
[6839]313          PRINTF(1)("Haven't got any StoryEntities to play!\n");
[6837]314          return;
315        }
316
317        this->switchMenuLayer(this->layerIndex, 1);
318      }
[6520]319    }
[6854]320    if( event.type == SDLK_ESCAPE && event.bPressed == true)
321    {
322      this->setNextStoryID(WORLD_ID_GAMEEND);
323      this->stop();
324    }
[6839]325  }  /* ----------------- LAYER 2 ---------------*/
[6837]326  else if( this->layerIndex == 1)
[6520]327  {
[6848]328    if( event.type == SDLK_RETURN && event.bPressed == true)
329    {
330      this->setNextStoryID( this->menuLayer[1]->storyList[this->menuSelectedIndex]->getStoryID());
331      this->stop();
332    }
[6854]333    if( event.type == SDLK_ESCAPE && event.bPressed == true)
334    {
335      this->switchMenuLayer(this->layerIndex, 0);
336    }
[6520]337  }
[6853]338
339
340
341  // The menu selction cursor
342  if( event.type == SDLK_DOWN && event.bPressed == true)
343  {
344    if(this->menuSelectedIndex < (this->menuLayer[this->layerIndex]->menuList.size() - 1))
345    {
346      this->menuSelected = this->menuLayer[this->layerIndex]->menuList[++this->menuSelectedIndex];
347      this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
348    }
349  }
350  else if( event.type == SDLK_UP && event.bPressed == true)
351  {
352    if(this->menuSelectedIndex > 0)
353    {
354      this->menuSelected = this->menuLayer[this->layerIndex]->menuList[--this->menuSelectedIndex];
355      this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
356    }
357  }
[6504]358}
[6502]359
360
[6837]361/**
362 *  switches to from one meny layer to an other
363 * @param layer1 from layer
364 * @param layer2 to layer
365 */
366void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
367{
368  // wrong sizes
369  if(layer1 >= this->menuLayer.size() || layer2 >= this->menuLayer.size())
370    return;
[6502]371
372
[6837]373  PRINTF(0)("Removing layer %i\n", layer1);
374  std::vector<ImageEntity*>::iterator it;
375  // fade old menu
376  for( it = this->menuLayer[layer1]->menuList.begin(); it != this->menuLayer[layer1]->menuList.end(); it++ )
377  {
[6848]378    (*it)->setVisibility(false);
[6837]379  }
[6502]380
381
[6837]382  PRINTF(0)("Showing layer %i\n", layer1);
383  // beam here the new menu
384  for( it = this->menuLayer[layer2]->menuList.begin(); it != this->menuLayer[layer2]->menuList.end(); it++ )
[6848]385  {
386    (*it)->setVisibility(true);
387  }
[6837]388
389  this->layerIndex = layer2;
[6854]390  this->menuSelected = this->menuLayer[layer2]->menuList[0];
391  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
[6855]392  this->menuSelectedIndex = 0;
[6837]393}
394
395
396
397
[6502]398/**********************************************************************************************
399    SimpleGameMenuData
400 **********************************************************************************************/
401
402
[6501]403/**
[6502]404 * SimpleGameMenuData constructor
[6501]405 */
[6502]406SimpleGameMenuData::SimpleGameMenuData()
[6501]407{}
408
409/**
[6502]410 * SimpleGameMenuData decontructor
[6501]411 */
[6502]412SimpleGameMenuData::~SimpleGameMenuData()
[6501]413{}
414
415
416/**
[6502]417 *  initialize the GameWorldDataData
[6501]418 */
[6502]419ErrorMessage SimpleGameMenuData::init()
420{
421  /* call underlying function */
422  GameWorldData::init();
423}
[6501]424
425
426/**
[6502]427 *  loads the GUI data
428 * @param root reference to the xml root element
[6501]429 */
[6502]430ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
431{
432  /* call underlying function */
433  GameWorldData::loadGUI(root);
434}
[6501]435
436
437/**
[6502]438 *  unloads the GUI data
[6501]439 */
[6502]440ErrorMessage SimpleGameMenuData::unloadGUI()
441{
442  /* call underlying function */
443  GameWorldData::unloadGUI();
444}
445
446
447/**
448 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
449 * @param root reference to the xml root parameter
450 */
451ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
452{
453  TiXmlElement* element = root->FirstChildElement("WorldEntities");
454
455  if( element != NULL)
456  {
457    element = element->FirstChildElement();
458    PRINTF(4)("Loading WorldEntities\n");
459    while( element != NULL)
460    {
461      BaseObject* created = Factory::fabricate(element);
462      if( created != NULL )
463        printf("Created a %s: %s\n", created->getClassName(), created->getName());
464
465      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
466        this->sky = dynamic_cast<WorldEntity*>(created);
467      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
468        this->terrain = dynamic_cast<Terrain*>(created);
469      element = element->NextSiblingElement();
470    }
471    PRINTF(4)("Done loading WorldEntities\n");
472  }
473
474  /* init the pnode tree */
475  PNode::getNullParent()->init();
476}
477
478
479/**
480 *  unloads the world entities from the xml file
481 */
482ErrorMessage SimpleGameMenuData::unloadWorldEntities()
483{
484  /* call underlying function */
485  GameWorldData::unloadWorldEntities();
486}
487
488
489/**
490 *  loads the scene data
491 * @param root reference to the xml root element
492 */
493ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
494{
495  /* call underlying function */
496  GameWorldData::loadScene(root);
497}
498
499
500/**
501 *  unloads the scene data
502 */
503ErrorMessage SimpleGameMenuData::unloadScene()
504{
505  /* call underlying function */
506  GameWorldData::unloadScene();
507}
508
509
510
Note: See TracBrowser for help on using the repository browser.