Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6878 was 6878, checked in by patrick, 18 years ago

trunk: now working with element2d only, game always returns to menu in singleplayer

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