Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D uses Vector2D instead of Vector whenever possible

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