Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6991 was 6991, checked in by bensch, 18 years ago

orxonox/trunk: fixed the Bug with the Menu loosing its frames

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