Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6919 was 6884, checked in by patrick, 19 years ago

trunk: working on the lenseflare

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