Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7305 was 7287, checked in by bensch, 19 years ago

merged back with:
svn merge . . -rHEAD:PREV

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