Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7142 was 7131, checked in by bensch, 19 years ago

orxonox/trunk: framerate calculation is much more 'accurate' meaning, if it is falling from one value into another one, the rate will be averanged to the middle.

File size: 15.2 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 "elements/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::setMenuID(this->getNextStoryID());
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  }
173
174  imageEntityList = ClassList::getList(CL_TEXT_ELEMENT);
175  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
176  {
177    if( !strcmp( "StartGame_Menu", (*entity)->getName()))
178    {
179      this->menuStartGame = dynamic_cast<TextElement*>(*entity);
180      this->menuStartGame->setBindNode((const PNode*)NULL);
181      this->menuStartGame->setRelCoor2D(State::getResX() / 2.0f,
182                                        State::getResY() / 2.0f - 60.0f,
183                                        0.0f);
184      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
185
186    }
187    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
188    {
189      this->menuStartMultiplayerGame = dynamic_cast<TextElement*>(*entity);
190      this->menuStartMultiplayerGame->setBindNode((const PNode*)NULL);
191      this->menuStartMultiplayerGame->setRelCoor2D(State::getResX() / 2.0f,
192                                                   State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 ) * 60.0f),
193                                                   0.0f);
194      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
195    }
196    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
197    {
198      this->menuQuitGame = dynamic_cast<TextElement*>(*entity);
199      this->menuQuitGame->setBindNode((const PNode*)NULL);
200      this->menuQuitGame->setRelCoor2D(State::getResX() / 2.0f,
201                                       State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 )* 60.0f),
202                                       0.0f);
203      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
204    }
205  }
206  this->menuSelected->getNullElement()->update2D(0.1f);
207  this->menuSelectedIndex = 0;
208  this->menuSelected = this->menuLayers[0].menuList[this->menuSelectedIndex];
209  this->sliderTo(this->menuSelected, 0.0f);
210
211
212  // loading the storyentities submenu (singleplayer)
213  const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
214  std::list<BaseObject*>::const_iterator it;
215  for( it = storyEntities->begin(); it != storyEntities->end(); it++)
216  {
217    StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
218    if( se->isContainedInMenu())
219    {
220      this->menuLayers[1].storyList.push_back(se);
221
222      // generating menu item
223      TextElement* te = new TextElement();
224      te->setVisibility(false);
225      te->setText(se->getName());
226      te->setRelCoor2D(State::getResX() / 2.0f - 200.0f, State::getResY() / 2.0f + ((this->menuLayers[1].menuList.size() - 2.0f) * 60.0f), 0.0f);
227      this->menuLayers[1].menuList.push_back(te);
228
229      // generating screenshoot item
230      ImageEntity* ie = new ImageEntity();
231      ie->setVisibility(false);
232      ie->setBindNode((const PNode*)NULL);
233      ie->setTexture(se->getMenuScreenshoot());
234      ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f, 0.0f);
235      ie->setSize2D(140.0f, 105.0f);
236      this->menuLayers[1].screenshootList.push_back(ie);
237    }
238  }
239}
240
241
242ErrorMessage SimpleGameMenu::unloadData()
243{
244
245  EventHandler::getInstance()->unsubscribe(this, ES_MENU);
246
247  std::vector<MenuLayer>::iterator mit;
248  for(mit = this->menuLayers.begin(); mit != this->menuLayers.end(); mit++)
249  {
250    while(!(*mit).menuList.empty())
251    {
252      delete (*mit).menuList.back();
253      (*mit).menuList.pop_back();
254    }
255
256    (*mit).menuList.clear();
257    (*mit).storyList.clear();
258    (*mit).screenshootList.clear();
259  }
260
261
262  GameWorld::unloadData();
263}
264
265
266/**
267 * start the menu
268 */
269bool SimpleGameMenu::start()
270{
271  EventHandler::getInstance()->pushState(ES_MENU);
272
273  /* now call the underlying*/
274  GameWorld::start();
275}
276
277
278
279/**
280 * stop the menu
281 */
282bool SimpleGameMenu::stop()
283{
284  EventHandler::getInstance()->popState();
285
286  /* now call the underlying*/
287  GameWorld::stop();
288}
289
290
291/**
292 *  override the standard tick for more functionality
293 */
294void SimpleGameMenu::tick()
295{
296  GameWorld::tick();
297
298  this->animateScene(this->dtS);
299}
300
301
302/**
303 *  no collision detection in the menu
304 */
305void SimpleGameMenu::collide()
306{
307//   this->dataTank->localCamera->
308}
309
310
311/**
312 *  animate the scene
313 */
314void SimpleGameMenu::animateScene(float dt)
315{
316  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
317  this->cameraVector = q.apply(this->cameraVector);
318  this->dataTank->localCamera->setRelCoor(this->cameraVector);
319  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
320}
321
322
323/**
324 * event dispatcher funciton
325 * @param event the incoming event
326 */
327void SimpleGameMenu::process(const Event &event)
328{
329  /* ----------------- LAYER 1 ---------------*/
330  if( this->layerIndex == 0)
331  {
332    if( event.type == SDLK_RETURN && event.bPressed == true)
333    {
334      if( this->menuSelected == this->menuQuitGame)
335      {
336        this->setNextStoryID(WORLD_ID_GAMEEND);
337        this->stop();
338      }
339      if( this->menuSelected == this->menuStartGame)
340      {
341        // switch to first submenu
342        if( this->menuLayers[1].menuList.size() == 0)
343        {
344          PRINTF(1)("Haven't got any StoryEntities to play!\n");
345          return;
346        }
347
348        this->switchMenuLayer(this->layerIndex, 1);
349      }
350    }
351    if( event.type == SDLK_ESCAPE && event.bPressed == true)
352    {
353      this->setNextStoryID(WORLD_ID_GAMEEND);
354      this->stop();
355    }
356  }  /* ----------------- LAYER 2 ---------------*/
357  else if( this->layerIndex == 1)
358  {
359    if( event.type == SDLK_RETURN && event.bPressed == true)
360    {
361      this->setNextStoryID( this->menuLayers[1].storyList[this->menuSelectedIndex]->getStoryID());
362      this->stop();
363    }
364    if( event.type == SDLK_ESCAPE && event.bPressed == true)
365    {
366      this->switchMenuLayer(this->layerIndex, 0);
367    }
368  }
369
370
371
372  // The menu selction cursor
373  if( event.type == SDLK_DOWN && event.bPressed == true)
374  {
375    if(this->menuSelectedIndex < (this->menuLayers[this->layerIndex].menuList.size() - 1))
376    {
377      this->menuSelected = this->menuLayers[this->layerIndex].menuList[++this->menuSelectedIndex];
378      this->sliderTo(this->menuSelected, 5.0f);
379
380      if( this->layerIndex == 1)
381      {
382        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
383        this->menuLayers[1].screenshootList[this->menuSelectedIndex-1]->setVisibility(false);
384      }
385    }
386  }
387  else if( event.type == SDLK_UP && event.bPressed == true)
388  {
389    if(this->menuSelectedIndex > 0)
390    {
391      this->menuSelected = this->menuLayers[this->layerIndex].menuList[--this->menuSelectedIndex];
392      this->sliderTo(this->menuSelected, 5.0f);
393
394      if( this->layerIndex == 1)
395      {
396        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
397        this->menuLayers[1].screenshootList[this->menuSelectedIndex+1]->setVisibility(false);
398      }
399    }
400  }
401}
402
403
404/**
405 *  switches to from one meny layer to an other
406 * @param layer1 from layer
407 * @param layer2 to layer
408 */
409void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
410{
411  // wrong sizes
412  if(layer1 >= this->menuLayers.size() || layer2 >= this->menuLayers.size())
413    return;
414
415
416  PRINTF(0)("Removing layer %i\n", layer1);
417  std::vector<TextElement*>::iterator te;
418  // fade old menu
419  for( te = this->menuLayers[layer1].menuList.begin(); te != this->menuLayers[layer1].menuList.end(); te++)
420  {
421    (*te)->setVisibility(false);
422  }
423
424  std::vector<ImageEntity*>::iterator it;
425
426  //also fade the screenshots if in level choosement mode
427  for( it = this->menuLayers[layer1].screenshootList.begin(); it != this->menuLayers[layer1].screenshootList.end(); it++)
428  {
429    (*it)->setVisibility(false);
430  }
431
432
433  PRINTF(0)("Showing layer %i\n", layer1);
434  // beam here the new menu
435  for( te = this->menuLayers[layer2].menuList.begin(); te != this->menuLayers[layer2].menuList.end(); te++ )
436  {
437    (*te)->setVisibility(true);
438  }
439
440
441  this->layerIndex = layer2;
442  this->menuSelected = this->menuLayers[layer2].menuList[0];
443  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
444  this->menuSelectedIndex = 0;
445
446  if( layer2 == 1)
447    this->menuLayers[layer2].screenshootList[0]->setVisibility(true);
448}
449
450void SimpleGameMenu::sliderTo(const Element2D* element, float bias)
451{
452  if (bias > 0.0)
453  {
454    this->menuSelector->setAbsCoorSoft2D(element->getAbsCoor2D() + Vector(0, element->getSizeY2D() *.5,0), bias);
455    this->menuSelector->setSizeSoft2D(element->getSizeX2D(), element->getSizeY2D(), bias);
456  }
457  else
458  {
459    this->menuSelector->setAbsCoor2D(element->getAbsCoor2D() + Vector(0, element->getSizeY2D() *.5,0));
460    this->menuSelector->setSize2D(element->getSizeX2D(), element->getSizeY2D());
461  }
462}
463
464
465
466/**********************************************************************************************
467    SimpleGameMenuData
468 **********************************************************************************************/
469
470
471/**
472 * SimpleGameMenuData constructor
473 */
474SimpleGameMenuData::SimpleGameMenuData()
475{}
476
477/**
478 * SimpleGameMenuData decontructor
479 */
480SimpleGameMenuData::~SimpleGameMenuData()
481{}
482
483
484/**
485 *  initialize the GameWorldDataData
486 */
487ErrorMessage SimpleGameMenuData::init()
488{
489  /* call underlying function */
490  GameWorldData::init();
491}
492
493
494/**
495 *  loads the GUI data
496 * @param root reference to the xml root element
497 */
498ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
499{
500  /* call underlying function */
501  GameWorldData::loadGUI(root);
502}
503
504
505/**
506 *  unloads the GUI data
507 */
508ErrorMessage SimpleGameMenuData::unloadGUI()
509{
510  /* call underlying function */
511  GameWorldData::unloadGUI();
512}
513
514
515/**
516 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
517 * @param root reference to the xml root parameter
518 */
519ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
520{
521  TiXmlElement* element = root->FirstChildElement("WorldEntities");
522
523  if( element != NULL)
524  {
525    element = element->FirstChildElement();
526    PRINTF(4)("Loading WorldEntities\n");
527    while( element != NULL)
528    {
529      BaseObject* created = Factory::fabricate(element);
530      if( created != NULL )
531        printf("Created a %s: %s\n", created->getClassName(), created->getName());
532
533      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
534        this->sky = dynamic_cast<WorldEntity*>(created);
535      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
536        this->terrain = dynamic_cast<Terrain*>(created);
537      element = element->NextSiblingElement();
538    }
539    PRINTF(4)("Done loading WorldEntities\n");
540  }
541
542  /* init the pnode tree */
543  PNode::getNullParent()->init();
544}
545
546
547/**
548 *  unloads the world entities from the xml file
549 */
550ErrorMessage SimpleGameMenuData::unloadWorldEntities()
551{
552  /* call underlying function */
553  GameWorldData::unloadWorldEntities();
554}
555
556
557/**
558 *  loads the scene data
559 * @param root reference to the xml root element
560 */
561ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
562{
563  /* call underlying function */
564  GameWorldData::loadScene(root);
565}
566
567
568/**
569 *  unloads the scene data
570 */
571ErrorMessage SimpleGameMenuData::unloadScene()
572{
573  /* call underlying function */
574  GameWorldData::unloadScene();
575}
576
577
578
Note: See TracBrowser for help on using the repository browser.