Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SoundSource in the Menu

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