Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7054 was 7032, checked in by bensch, 19 years ago

testing

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->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
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->dt);
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*/ 0.0001* dt, 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->menuSelector->setAbsCoorSoft2D(this->menuSelected->getAbsCoor2D() + Vector(0, this->menuSelected->getSizeY2D() *.5,0), 3.0);
379      this->menuSelector->setSizeSoft2D(this->menuSelected->getSizeX2D(), this->menuSelected->getSizeY2D(), 3.0);
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->menuSelector->setAbsCoorSoft2D(this->menuSelected->getAbsCoor2D() + Vector(0, this->menuSelected->getSizeY2D() *.5,0), 3.0);
394      this->menuSelector->setSizeSoft2D(this->menuSelected->getSizeX2D(), this->menuSelected->getSizeY2D(), 3.0);
395
396      if( this->layerIndex == 1)
397      {
398        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
399        this->menuLayers[1].screenshootList[this->menuSelectedIndex+1]->setVisibility(false);
400      }
401    }
402  }
403}
404
405
406/**
407 *  switches to from one meny layer to an other
408 * @param layer1 from layer
409 * @param layer2 to layer
410 */
411void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
412{
413  // wrong sizes
414  if(layer1 >= this->menuLayers.size() || layer2 >= this->menuLayers.size())
415    return;
416
417
418  PRINTF(0)("Removing layer %i\n", layer1);
419  std::vector<TextElement*>::iterator te;
420  // fade old menu
421  for( te = this->menuLayers[layer1].menuList.begin(); te != this->menuLayers[layer1].menuList.end(); te++)
422  {
423    (*te)->setVisibility(false);
424  }
425
426  std::vector<ImageEntity*>::iterator it;
427
428  //also fade the screenshots if in level choosement mode
429  for( it = this->menuLayers[layer1].screenshootList.begin(); it != this->menuLayers[layer1].screenshootList.end(); it++)
430  {
431    (*it)->setVisibility(false);
432  }
433
434
435  PRINTF(0)("Showing layer %i\n", layer1);
436  // beam here the new menu
437  for( te = this->menuLayers[layer2].menuList.begin(); te != this->menuLayers[layer2].menuList.end(); te++ )
438  {
439    (*te)->setVisibility(true);
440  }
441
442
443  this->layerIndex = layer2;
444  this->menuSelected = this->menuLayers[layer2].menuList[0];
445  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
446  this->menuSelectedIndex = 0;
447
448  if( layer2 == 1)
449    this->menuLayers[layer2].screenshootList[0]->setVisibility(true);
450}
451
452
453
454
455/**********************************************************************************************
456    SimpleGameMenuData
457 **********************************************************************************************/
458
459
460/**
461 * SimpleGameMenuData constructor
462 */
463SimpleGameMenuData::SimpleGameMenuData()
464{}
465
466/**
467 * SimpleGameMenuData decontructor
468 */
469SimpleGameMenuData::~SimpleGameMenuData()
470{}
471
472
473/**
474 *  initialize the GameWorldDataData
475 */
476ErrorMessage SimpleGameMenuData::init()
477{
478  /* call underlying function */
479  GameWorldData::init();
480}
481
482
483/**
484 *  loads the GUI data
485 * @param root reference to the xml root element
486 */
487ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
488{
489  /* call underlying function */
490  GameWorldData::loadGUI(root);
491}
492
493
494/**
495 *  unloads the GUI data
496 */
497ErrorMessage SimpleGameMenuData::unloadGUI()
498{
499  /* call underlying function */
500  GameWorldData::unloadGUI();
501}
502
503
504/**
505 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
506 * @param root reference to the xml root parameter
507 */
508ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
509{
510  TiXmlElement* element = root->FirstChildElement("WorldEntities");
511
512  if( element != NULL)
513  {
514    element = element->FirstChildElement();
515    PRINTF(4)("Loading WorldEntities\n");
516    while( element != NULL)
517    {
518      BaseObject* created = Factory::fabricate(element);
519      if( created != NULL )
520        printf("Created a %s: %s\n", created->getClassName(), created->getName());
521
522      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
523        this->sky = dynamic_cast<WorldEntity*>(created);
524      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
525        this->terrain = dynamic_cast<Terrain*>(created);
526      element = element->NextSiblingElement();
527    }
528    PRINTF(4)("Done loading WorldEntities\n");
529  }
530
531  /* init the pnode tree */
532  PNode::getNullParent()->init();
533}
534
535
536/**
537 *  unloads the world entities from the xml file
538 */
539ErrorMessage SimpleGameMenuData::unloadWorldEntities()
540{
541  /* call underlying function */
542  GameWorldData::unloadWorldEntities();
543}
544
545
546/**
547 *  loads the scene data
548 * @param root reference to the xml root element
549 */
550ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
551{
552  /* call underlying function */
553  GameWorldData::loadScene(root);
554}
555
556
557/**
558 *  unloads the scene data
559 */
560ErrorMessage SimpleGameMenuData::unloadScene()
561{
562  /* call underlying function */
563  GameWorldData::unloadScene();
564}
565
566
567
Note: See TracBrowser for help on using the repository browser.