Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: fixed a segfault bug

File size: 12.0 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  this->layerIndex = 0;
62
63  this->loadParams(root);
64}
65
66
67/**
68 *  remove the SimpleGameMenu from memory
69 *
70 *  delete everything explicitly, that isn't contained in the parenting tree!
71 *  things contained in the tree are deleted automaticaly
72 */
73SimpleGameMenu::~SimpleGameMenu ()
74{
75  PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n");
76
77  if( this->dataTank)
78    delete this->dataTank;
79}
80
81
82/**
83 * loads the parameters of a SimpleGameMenu from an XML-element
84 * @param root the XML-element to load from
85 */
86void SimpleGameMenu::loadParams(const TiXmlElement* root)
87{
88  /* skip the GameWorld, since it does not define any useful loadParams for this class */
89  //static_cast<GameWorld*>(this)->loadParams(root);
90  GameWorld::loadParams(root);
91
92  PRINTF(4)("Loaded SimpleGameMenu specific stuff\n");
93}
94
95
96/**
97 * this is executed just before load
98 *
99 * since the load function sometimes needs data, that has been initialized
100 * before the load and after the proceeding storyentity has finished
101 */
102ErrorMessage SimpleGameMenu::init()
103{
104  /* call underlying init funciton */
105  GameWorld::init();
106
107  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_UP);
108  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_DOWN);
109  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_RETURN);
110  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_SPACE);
111  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_ESCAPE);
112
113  this->dataTank->localCamera->setRelCoor(this->cameraVector);
114
115  GraphicsEngine::getInstance()->displayFPS(false);
116}
117
118
119/**
120 * load the data
121 */
122ErrorMessage SimpleGameMenu::loadData()
123{
124  GameWorld::loadData();
125
126  if (this->dataXML != NULL)
127  {
128    TiXmlElement* element = this->dataXML->FirstChildElement("Elements");
129
130    if( element == NULL)
131    {
132      PRINTF(1)("SimpleGameMenu is missing 'Elements'\n");
133    }
134    else
135    {
136      element = element->FirstChildElement();
137    // load Players/Objects/Whatever
138      PRINTF(4)("Loading Elements\n");
139      while( element != NULL)
140      {
141        BaseObject* created = Factory::fabricate(element);
142        if( created != NULL )
143        {
144          PRINTF(4)("Created a %s::%s\n", created->getClassName(), created->getName());
145          if (!created->isA(CL_ELEMENT_2D))
146            PRINTF(2)("Error the Created Entity is not an Element2D but an %s::%s\n", created->getClassName(), created->getName());
147        }
148        element = element->NextSiblingElement();
149      }
150      PRINTF(4)("Done loading Elements\n");
151    }
152  }
153
154  /* get the menu list */
155  const std::list<BaseObject*>* imageEntityList = ClassList::getList(CL_IMAGE_ENTITY);
156  std::list<BaseObject*>::const_iterator entity;
157  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
158  {
159
160    if( !strcmp("Selector_Menu", (*entity)->getName()))
161    {
162      this->menuSelector = dynamic_cast<ImageEntity*>(*entity);
163    }
164    else if( !strcmp( "StartGame_Menu", (*entity)->getName()))
165    {
166      this->menuStartGame = dynamic_cast<ImageEntity*>(*entity);
167      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
168
169    }
170    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
171    {
172      this->menuStartMultiplayerGame = dynamic_cast<ImageEntity*>(*entity);
173      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
174    }
175    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
176    {
177      this->menuQuitGame = dynamic_cast<ImageEntity*>(*entity);
178      this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity));
179    }
180  }
181  this->menuSelectedIndex = 0;
182  this->menuSelected = this->menuLayer[0]->menuList[this->menuSelectedIndex];
183  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
184
185
186  // loading the storyentities submenu (singleplayer)
187  const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
188  std::list<BaseObject*>::const_iterator it;
189  for( it = storyEntities->begin(); it != storyEntities->end(); it++)
190  {
191    StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
192    if( se->isContainedInMenu())
193    {
194      this->menuLayer[1]->storyList.push_back(se);
195      PRINTF(0)("Got a new menu entry |%s|\n", se->getName());
196      ImageEntity* ie = new ImageEntity();
197      PRINTF(0)("setting texture to: |%s|\n", se->getMenuItemImage());
198      ie->setTexture(se->getMenuItemImage());
199      ie->setRelCoor(0.0f,20.0f - (this->menuLayer[1]->menuList.size() * 10.0f), 0.0f);
200      ie->setVisibility(false);
201      this->menuLayer[1]->menuList.push_back(ie);
202    }
203  }
204}
205
206
207
208/**
209 * start the menu
210 */
211bool SimpleGameMenu::start()
212{
213  EventHandler::getInstance()->pushState(ES_MENU);
214
215  /* now call the underlying*/
216  GameWorld::start();
217}
218
219
220
221/**
222 * stop the menu
223 */
224bool SimpleGameMenu::stop()
225{
226  EventHandler::getInstance()->popState();
227
228  /* now call the underlying*/
229  GameWorld::stop();
230}
231
232
233/**
234 *  override the standard tick for more functionality
235 */
236void SimpleGameMenu::tick()
237{
238  GameWorld::tick();
239
240  this->animateScene(this->dt);
241}
242
243
244/**
245 *  no collision detection in the menu
246 */
247void SimpleGameMenu::collide()
248{
249//   this->dataTank->localCamera->
250}
251
252
253/**
254 *  animate the scene
255 */
256void SimpleGameMenu::animateScene(float dt)
257{
258  Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0));
259  this->cameraVector = q.apply(this->cameraVector);
260  this->dataTank->localCamera->setRelCoor(this->cameraVector);
261  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
262}
263
264
265/**
266 * event dispatcher funciton
267 * @param event the incoming event
268 */
269void SimpleGameMenu::process(const Event &event)
270{
271  /* ----------------- LAYER 1 ---------------*/
272  if( this->layerIndex == 0)
273  {
274    if( event.type == SDLK_RETURN && event.bPressed == true)
275    {
276      if( this->menuSelected == this->menuQuitGame)
277      {
278        this->setNextStoryID(WORLD_ID_GAMEEND);
279        this->stop();
280      }
281      if( this->menuSelected == this->menuStartGame)
282      {
283        // switch to first submenu
284        if( this->menuLayer[1]->menuList.size() == 0)
285        {
286          PRINTF(1)("Haven't got any StoryEntities to play!\n");
287          return;
288        }
289
290        this->switchMenuLayer(this->layerIndex, 1);
291      }
292    }
293    if( event.type == SDLK_ESCAPE && event.bPressed == true)
294    {
295      this->setNextStoryID(WORLD_ID_GAMEEND);
296      this->stop();
297    }
298  }  /* ----------------- LAYER 2 ---------------*/
299  else if( this->layerIndex == 1)
300  {
301    if( event.type == SDLK_RETURN && event.bPressed == true)
302    {
303      this->setNextStoryID( this->menuLayer[1]->storyList[this->menuSelectedIndex]->getStoryID());
304      this->stop();
305    }
306    if( event.type == SDLK_ESCAPE && event.bPressed == true)
307    {
308      this->switchMenuLayer(this->layerIndex, 0);
309    }
310  }
311
312
313
314  // The menu selction cursor
315  if( event.type == SDLK_DOWN && event.bPressed == true)
316  {
317    if(this->menuSelectedIndex < (this->menuLayer[this->layerIndex]->menuList.size() - 1))
318    {
319      this->menuSelected = this->menuLayer[this->layerIndex]->menuList[++this->menuSelectedIndex];
320      this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
321    }
322  }
323  else if( event.type == SDLK_UP && event.bPressed == true)
324  {
325    if(this->menuSelectedIndex > 0)
326    {
327      this->menuSelected = this->menuLayer[this->layerIndex]->menuList[--this->menuSelectedIndex];
328      this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
329    }
330  }
331}
332
333
334/**
335 *  switches to from one meny layer to an other
336 * @param layer1 from layer
337 * @param layer2 to layer
338 */
339void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
340{
341  // wrong sizes
342  if(layer1 >= this->menuLayer.size() || layer2 >= this->menuLayer.size())
343    return;
344
345
346  PRINTF(0)("Removing layer %i\n", layer1);
347  std::vector<ImageEntity*>::iterator it;
348  // fade old menu
349  for( it = this->menuLayer[layer1]->menuList.begin(); it != this->menuLayer[layer1]->menuList.end(); it++ )
350  {
351    (*it)->setVisibility(false);
352  }
353
354
355  PRINTF(0)("Showing layer %i\n", layer1);
356  // beam here the new menu
357  for( it = this->menuLayer[layer2]->menuList.begin(); it != this->menuLayer[layer2]->menuList.end(); it++ )
358  {
359    (*it)->setVisibility(true);
360  }
361
362  this->layerIndex = layer2;
363  this->menuSelected = this->menuLayer[layer2]->menuList[0];
364  this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor());
365  this->menuSelectedIndex = 0;
366}
367
368
369
370
371/**********************************************************************************************
372    SimpleGameMenuData
373 **********************************************************************************************/
374
375
376/**
377 * SimpleGameMenuData constructor
378 */
379SimpleGameMenuData::SimpleGameMenuData()
380{}
381
382/**
383 * SimpleGameMenuData decontructor
384 */
385SimpleGameMenuData::~SimpleGameMenuData()
386{}
387
388
389/**
390 *  initialize the GameWorldDataData
391 */
392ErrorMessage SimpleGameMenuData::init()
393{
394  /* call underlying function */
395  GameWorldData::init();
396}
397
398
399/**
400 *  loads the GUI data
401 * @param root reference to the xml root element
402 */
403ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
404{
405  /* call underlying function */
406  GameWorldData::loadGUI(root);
407}
408
409
410/**
411 *  unloads the GUI data
412 */
413ErrorMessage SimpleGameMenuData::unloadGUI()
414{
415  /* call underlying function */
416  GameWorldData::unloadGUI();
417}
418
419
420/**
421 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
422 * @param root reference to the xml root parameter
423 */
424ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
425{
426  TiXmlElement* element = root->FirstChildElement("WorldEntities");
427
428  if( element != NULL)
429  {
430    element = element->FirstChildElement();
431    PRINTF(4)("Loading WorldEntities\n");
432    while( element != NULL)
433    {
434      BaseObject* created = Factory::fabricate(element);
435      if( created != NULL )
436        printf("Created a %s: %s\n", created->getClassName(), created->getName());
437
438      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
439        this->sky = dynamic_cast<WorldEntity*>(created);
440      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
441        this->terrain = dynamic_cast<Terrain*>(created);
442      element = element->NextSiblingElement();
443    }
444    PRINTF(4)("Done loading WorldEntities\n");
445  }
446
447  /* init the pnode tree */
448  PNode::getNullParent()->init();
449}
450
451
452/**
453 *  unloads the world entities from the xml file
454 */
455ErrorMessage SimpleGameMenuData::unloadWorldEntities()
456{
457  /* call underlying function */
458  GameWorldData::unloadWorldEntities();
459}
460
461
462/**
463 *  loads the scene data
464 * @param root reference to the xml root element
465 */
466ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
467{
468  /* call underlying function */
469  GameWorldData::loadScene(root);
470}
471
472
473/**
474 *  unloads the scene data
475 */
476ErrorMessage SimpleGameMenuData::unloadScene()
477{
478  /* call underlying function */
479  GameWorldData::unloadScene();
480}
481
482
483
Note: See TracBrowser for help on using the repository browser.