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, 18 years ago

trunk: the menu control better implemented

File size: 12.6 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
201      // generating menu item
202      ImageEntity* ie = new ImageEntity();
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      // 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
214    }
215  }
216}
217
218
219ErrorMessage SimpleGameMenu::unloadData()
220{
221  GameWorld::unloadData();
222
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
235/**
236 * start the menu
237 */
238bool SimpleGameMenu::start()
239{
240  EventHandler::getInstance()->pushState(ES_MENU);
241
242  /* now call the underlying*/
243  GameWorld::start();
244}
245
246
247
248/**
249 * stop the menu
250 */
251bool SimpleGameMenu::stop()
252{
253  EventHandler::getInstance()->popState();
254
255  /* now call the underlying*/
256  GameWorld::stop();
257}
258
259
260/**
261 *  override the standard tick for more functionality
262 */
263void SimpleGameMenu::tick()
264{
265  GameWorld::tick();
266
267  this->animateScene(this->dt);
268}
269
270
271/**
272 *  no collision detection in the menu
273 */
274void SimpleGameMenu::collide()
275{
276//   this->dataTank->localCamera->
277}
278
279
280/**
281 *  animate the scene
282 */
283void SimpleGameMenu::animateScene(float dt)
284{
285  Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0));
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/**
293 * event dispatcher funciton
294 * @param event the incoming event
295 */
296void SimpleGameMenu::process(const Event &event)
297{
298  /* ----------------- LAYER 1 ---------------*/
299  if( this->layerIndex == 0)
300  {
301    if( event.type == SDLK_RETURN && event.bPressed == true)
302    {
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
311        if( this->menuLayer[1]->menuList.size() == 0)
312        {
313          PRINTF(1)("Haven't got any StoryEntities to play!\n");
314          return;
315        }
316
317        this->switchMenuLayer(this->layerIndex, 1);
318      }
319    }
320    if( event.type == SDLK_ESCAPE && event.bPressed == true)
321    {
322      this->setNextStoryID(WORLD_ID_GAMEEND);
323      this->stop();
324    }
325  }  /* ----------------- LAYER 2 ---------------*/
326  else if( this->layerIndex == 1)
327  {
328    if( event.type == SDLK_RETURN && event.bPressed == true)
329    {
330      this->setNextStoryID( this->menuLayer[1]->storyList[this->menuSelectedIndex]->getStoryID());
331      this->stop();
332    }
333    if( event.type == SDLK_ESCAPE && event.bPressed == true)
334    {
335      this->switchMenuLayer(this->layerIndex, 0);
336    }
337  }
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  }
358}
359
360
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;
371
372
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  {
378    (*it)->setVisibility(false);
379  }
380
381
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++ )
385  {
386    (*it)->setVisibility(true);
387  }
388
389  this->layerIndex = layer2;
390  this->menuSelected = this->menuLayer[layer2]->menuList[0];
391  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
392  this->menuSelectedIndex = 0;
393}
394
395
396
397
398/**********************************************************************************************
399    SimpleGameMenuData
400 **********************************************************************************************/
401
402
403/**
404 * SimpleGameMenuData constructor
405 */
406SimpleGameMenuData::SimpleGameMenuData()
407{}
408
409/**
410 * SimpleGameMenuData decontructor
411 */
412SimpleGameMenuData::~SimpleGameMenuData()
413{}
414
415
416/**
417 *  initialize the GameWorldDataData
418 */
419ErrorMessage SimpleGameMenuData::init()
420{
421  /* call underlying function */
422  GameWorldData::init();
423}
424
425
426/**
427 *  loads the GUI data
428 * @param root reference to the xml root element
429 */
430ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
431{
432  /* call underlying function */
433  GameWorldData::loadGUI(root);
434}
435
436
437/**
438 *  unloads the GUI data
439 */
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.