Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/story_entities/menu/game_menu.cc @ 9064

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

merged the network branche with the trunk

File size: 14.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 "game_menu.h"
20
21#include "event_handler.h"
22
23#include "state.h"
24#include "class_list.h"
25
26#include "util/loading/load_param.h"
27#include "util/loading/factory.h"
28#include "util/loading/resource_manager.h"
29
30#include "graphics_engine.h"
31#include "camera.h"
32#include "sound_engine.h"
33
34#include "sound_source.h"
35
36#include "glgui.h"
37#include "menu/glgui_imagebutton.h"
38
39#include "glgui_text.h"
40
41#include "network_manager.h"
42
43//! This creates a Factory to fabricate a GameMenu
44CREATE_FACTORY(GameMenu, CL_GAME_MENU);
45
46
47
48GameMenu::GameMenu(const TiXmlElement* root)
49    : GameWorld()
50{
51  this->setClassID(CL_GAME_MENU, "GameMenu");
52  this->setName("GameMenu uninitialized");
53
54  this->dataTank = new GameMenuData();
55
56  this->cameraVector = Vector(50.0, 0.0, 0.0);
57
58  if (root != NULL)
59    this->loadParams(root);
60
61  State::setMenuID(this->getNextStoryID());
62}
63
64/**
65*  @brief remove the GameMenu from memory
66*
67*  delete everything explicitly, that isn't contained in the parenting tree!
68*  things contained in the tree are deleted automaticaly
69*/
70GameMenu::~GameMenu ()
71{
72  PRINTF(3)("GameMenu::~GameMenu() - deleting current world\n");
73
74  if( this->dataTank)
75    delete this->dataTank;
76  delete OrxGui::GLGuiHandler::getInstance( );
77}
78
79
80/**
81* @brief loads the parameters of a GameMenu from an XML-element
82* @param root the XML-element to load from
83*/
84void GameMenu::loadParams(const TiXmlElement* root)
85{
86  /* skip the GameWorld, since it does not define any useful loadParams for this class */
87  //static_cast<GameWorld*>(this)->loadParams(root);
88  GameWorld::loadParams(root);
89
90  PRINTF(4)("Loaded GameMenu specific stuff\n");
91}
92
93
94/**
95* @brief this is executed just before load
96*
97* since the load function sometimes needs data, that has been initialized
98* before the load and after the proceeding storyentity has finished
99*/
100ErrorMessage GameMenu::init()
101{
102  /* call underlying init funciton */
103  GameWorld::init();
104
105  this->subscribeEvent(ES_MENU, SDLK_UP);
106  this->subscribeEvent(ES_MENU, SDLK_DOWN);
107  this->subscribeEvent(ES_MENU, SDLK_RETURN);
108  this->subscribeEvent(ES_MENU, SDLK_SPACE);
109  this->subscribeEvent(ES_MENU, SDLK_ESCAPE);
110
111  this->dataTank->localCamera->setRelCoor(this->cameraVector);
112
113  GraphicsEngine::getInstance()->displayFPS(false);
114
115  return ErrorMessage();
116}
117
118
119/**
120* @brief load the data
121*/
122ErrorMessage GameMenu::loadData()
123{
124  this->mainMenuBox = NULL;
125
126  this->levelsBox = NULL;
127  this->networkBox = NULL;
128 
129  this->clientNetworkBox = NULL;
130  this->serverNetworkBox = NULL;
131
132  this->optionsBox = NULL;
133  this->generalBox = NULL;
134  this->audioBox = NULL;
135  this->videoBox = NULL;
136  this->controlBox = NULL;
137  this->levelsBox = NULL;
138
139  this->currentlyOpened = NULL;
140
141  return GameWorld::loadData();
142}
143
144
145void GameMenu::showMainMenu()
146{
147  if (mainMenuBox == NULL)
148  {
149    this->mainMenuBox = new OrxGui::GLGuiBox();
150    {
151      OrxGui::GLGuiButton* startButton = new OrxGui::GLGuiPushButton("Play");
152      startButton->connect(SIGNAL(startButton, released), this, SLOT(GameMenu, showCampaigns));
153      this->mainMenuBox->pack(startButton);
154      startButton->select();
155
156      OrxGui::GLGuiButton* networkButton = new OrxGui::GLGuiPushButton("MultiPlayer");
157      networkButton->connect(SIGNAL(networkButton, released), this, SLOT(GameMenu, showMultiPlayer));
158      this->mainMenuBox->pack(networkButton);
159
160      OrxGui::GLGuiButton* optionsButton = new OrxGui::GLGuiPushButton("Options");
161      optionsButton->connect(SIGNAL(optionsButton, released), this, SLOT(GameMenu, showOptionsMenu));
162      this->mainMenuBox->pack(optionsButton);
163
164
165      OrxGui::GLGuiButton* quitButton = new OrxGui::GLGuiPushButton("Quit");
166      this->mainMenuBox->pack(quitButton);
167      quitButton->connect(SIGNAL(quitButton, released), this, SLOT(GameMenu, quitMenu));
168    }
169  }
170  this->mainMenuBox->showAll();
171
172
173  this->mainMenuBox->setRelCoor2D(200, 100);
174}
175
176
177void GameMenu::showCampaigns()
178{
179  if (this->levelsBox == NULL)
180  {
181    this->levelsBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
182    {
183      OrxGui::GLGuiBox* labelBox = new OrxGui::GLGuiBox();
184
185      OrxGui::GLGuiImage* image = new OrxGui::GLGuiImage();
186      image->show();
187      image->setWidgetSize( 250, 200);
188      image->setAbsCoor2D(400, 150);
189      image->setForegroundColor(Color( 1,1,1,.6));
190
191      const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
192      std::list<BaseObject*>::const_iterator it;
193      bool first = true;
194      for( it = storyEntities->begin(); it != storyEntities->end(); it++)
195      {
196        StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
197        if( se->isContainedInMenu())
198        {
199
200          printf("%s\n", se->getMenuScreenshoot().c_str());
201          OrxGui::GLGuiImageButton* button = new OrxGui::GLGuiImageButton(se->getName(), se->getStoryID(), se->getMenuScreenshoot(), image);
202          button->connect(SIGNAL(button, startLevel), this, SLOT(GameMenu, startLevel));
203          labelBox->pack(button);
204
205          if (first)
206          {
207            first = !first;
208            button->select();
209          }
210
211          // generating screenshoot item
212          /*
213          ImageEntity* ie = new ImageEntity();
214          ie->setVisibility(false);
215          ie->setBindNode((const PNode*)NULL);
216          ie->setTexture(se->getMenuScreenshoot());
217          ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f);
218          ie->setSize2D(140.0f, 105.0f);
219          this->menuLayers[1].screenshootList.push_back(ie);
220          */
221        }
222      }
223
224      this->levelsBox->pack(labelBox);
225      this->levelsBox->pack(image);
226    }
227
228  }
229
230  this->showSecondLevelElement(this->levelsBox);
231
232}
233
234void GameMenu::showMultiPlayer()
235{
236  if (this->networkBox == NULL)
237  {
238    this->networkBox = new OrxGui::GLGuiBox();
239    {
240      OrxGui::GLGuiButton* clientButton = new OrxGui::GLGuiPushButton("Client");
241      networkBox->pack(clientButton);
242      clientButton->connect(SIGNAL(clientButton, released), this, SLOT(GameMenu, showClientMenu));
243
244      OrxGui::GLGuiButton* serverButton = new OrxGui::GLGuiPushButton("Server");
245      networkBox->pack(serverButton);
246      serverButton->connect(SIGNAL(serverButton, released), this, SLOT(GameMenu, showServerMenu));
247
248
249    }
250  }
251
252  this->showSecondLevelElement(this->networkBox);
253
254}
255
256void GameMenu::showOptionsMenu()
257{
258  if (this->optionsBox == NULL)
259  {
260    this->optionsBox = new OrxGui::GLGuiBox();
261    {
262      OrxGui::GLGuiTextfield* WARNtext = new OrxGui::GLGuiTextfield();
263      WARNtext->setText("PLEASE USE THE EXTERNAL GUI\n FOR ORXONOX CONFIGURATION\n (start with './orxonox -g')");
264      optionsBox->pack(WARNtext);
265
266
267      OrxGui::GLGuiButton* generalButton = new OrxGui::GLGuiPushButton("General");
268      optionsBox->pack(generalButton);
269
270      OrxGui::GLGuiButton* audioButton = new OrxGui::GLGuiPushButton("Audio");
271      optionsBox->pack(audioButton);
272
273      OrxGui::GLGuiButton* videoButton = new OrxGui::GLGuiPushButton("Video");
274      optionsBox->pack(videoButton);
275
276      OrxGui::GLGuiButton* controlButton = new OrxGui::GLGuiPushButton("Control");
277      optionsBox->pack(controlButton);
278
279
280      //      for (unsigned int i = 0; i <
281      //OrxGui::GLGuiButton*
282
283    }
284  }
285
286  this->showSecondLevelElement(this->optionsBox);
287}
288
289
290void GameMenu::showSecondLevelElement(OrxGui::GLGuiBox* element)
291{
292  if (this->currentlyOpened != NULL && this->currentlyOpened != element)
293    this->currentlyOpened->hideAll();
294
295  element->showAll();
296  element->setRelCoor2D(200, 100);
297
298  this->currentlyOpened = element;
299
300  this->mainMenuBox->setRelCoorSoft2D(50, 100, 5);
301}
302
303
304
305
306
307void GameMenu::startLevel(int levelID)
308{
309  this->setNextStoryID( levelID);
310  this->stop();
311}
312
313/**
314* @brief set the Sound to play when switching menu entry.
315* @param selectorSound the sound to load.
316*/
317void GameMenu::setSelectorSound(const std::string& selectorSound)
318{
319  this->selectorSource = OrxSound::SoundEngine::getInstance()->createSource(selectorSound, NULL);
320}
321
322ErrorMessage GameMenu::unloadData()
323{
324  this->unsubscribeEvents(ES_MENU);
325
326  return GameWorld::unloadData();
327}
328
329
330/**
331* @brief start the menu
332*/
333bool GameMenu::start()
334{
335  EventHandler::getInstance()->pushState(ES_MENU);
336
337  this->showMainMenu();
338  OrxGui::GLGuiHandler::getInstance()->activateCursor();
339  OrxGui::GLGuiHandler::getInstance()->activate();
340  OrxGui::GLGuiHandler::getInstance()->cursor()->loadTextureSequence(ResourceManager::getInstance()->getDataDir() + "/" + "maps/reap_mouse/reap_mouse_##.png", 1, 49);
341
342  /* now call the underlying*/
343  return GameWorld::start();
344}
345
346
347
348/**
349* stop the menu
350*/
351bool GameMenu::stop()
352{
353  EventHandler::getInstance()->popState();
354
355  /* now call the underlying*/
356  return GameWorld::stop();
357}
358
359
360/**
361*  override the standard tick for more functionality
362*/
363void GameMenu::tick()
364{
365  GameWorld::tick();
366
367  // Make the GLGui tick.
368  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
369
370  this->animateScene(this->dtS);
371}
372
373
374/**
375* @brief no collision detection in the menu
376*/
377void GameMenu::collide()
378{
379  //   this->dataTank->localCamera->
380}
381
382
383/**
384* @brief animate the scene
385*/
386void GameMenu::animateScene(float dt)
387{
388  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
389  this->cameraVector = q.apply(this->cameraVector);
390  this->dataTank->localCamera->setRelCoor(this->cameraVector);
391  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
392}
393
394void GameMenu::quitMenu()
395{
396  this->setNextStoryID(WORLD_ID_GAMEEND);
397  this->stop();
398}
399
400
401/**
402* @brief event dispatcher funciton
403* @param event the incoming event
404*/
405void GameMenu::process(const Event &event)
406{
407  if( event.type == SDLK_ESCAPE && event.bPressed == true)
408  {
409    this->setNextStoryID(WORLD_ID_GAMEEND);
410    this->stop();
411  }
412
413}
414
415
416
417/**********************************************************************************************
418GameMenuData
419**********************************************************************************************/
420
421
422/**
423* GameMenuData constructor
424*/
425GameMenuData::GameMenuData()
426{}
427
428/**
429* GameMenuData decontructor
430*/
431GameMenuData::~GameMenuData()
432{}
433
434
435/**
436*  initialize the GameWorldDataData
437*/
438ErrorMessage GameMenuData::init()
439{
440  /* call underlying function */
441  return GameWorldData::init();
442}
443
444
445/**
446*  loads the GUI data
447* @param root reference to the xml root element
448*/
449ErrorMessage GameMenuData::loadGUI(const TiXmlElement* root)
450{
451  /* call underlying function */
452  return GameWorldData::loadGUI(root);
453}
454
455
456/**
457*  unloads the GUI data
458*/
459ErrorMessage GameMenuData::unloadGUI()
460{
461  /* call underlying function */
462  return GameWorldData::unloadGUI();
463}
464
465
466/**
467*  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
468* @param root reference to the xml root parameter
469*/
470ErrorMessage GameMenuData::loadWorldEntities(const TiXmlElement* root)
471{
472  return GameWorldData::loadWorldEntities(root);
473}
474
475
476/**
477*  unloads the world entities from the xml file
478*/
479ErrorMessage GameMenuData::unloadWorldEntities()
480{
481  /* call underlying function */
482  return GameWorldData::unloadWorldEntities();
483}
484
485
486/**
487*  loads the scene data
488* @param root reference to the xml root element
489*/
490ErrorMessage GameMenuData::loadScene(const TiXmlElement* root)
491{
492  /* call underlying function */
493  return GameWorldData::loadScene(root);
494}
495
496
497/**
498*  unloads the scene data
499*/
500ErrorMessage GameMenuData::unloadScene()
501{
502  /* call underlying function */
503  return GameWorldData::unloadScene();
504}
505
506/**
507 * show controls to join network game
508 */
509void GameMenu::showClientMenu( )
510{
511  if ( this->serverNetworkBox )
512  {
513    delete this->serverNetworkBox;
514    this->serverNetworkBox = NULL;
515  }
516 
517  if ( !this->clientNetworkBox )
518  {
519    this->clientNetworkBox = new OrxGui::GLGuiBox();
520    {
521      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
522      text->setText( "Host:" );
523      this->clientNetworkBox->pack( text );
524     
525      this->ipInputLine = new OrxGui::GLGuiInputLine( );
526      this->ipInputLine->setText( "tardis-d08" );
527      this->clientNetworkBox->pack( this->ipInputLine );
528      this->ipInputLine->connect(SIGNAL(ipInputLine, enterPushed), this, SLOT(GameMenu, connectToServer));
529      this->ipInputLine->select();
530     
531      OrxGui::GLGuiButton* connectButton = new OrxGui::GLGuiPushButton("Connect");
532      clientNetworkBox->pack(connectButton);
533      connectButton->connect(SIGNAL(connectButton, released), this, SLOT(GameMenu, connectToServer));
534    }
535  }
536 
537  this->clientNetworkBox->showAll();
538 
539  this->clientNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
540}
541
542/**
543 * show controls to create new network game
544 */
545void GameMenu::showServerMenu( )
546{
547  if ( this->clientNetworkBox )
548  {
549    delete this->clientNetworkBox;
550    this->clientNetworkBox = NULL;
551  }
552 
553  if ( !this->serverNetworkBox )
554  {
555    this->serverNetworkBox = new OrxGui::GLGuiBox();
556    {
557      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
558      text->setText( "Map:" );
559      this->serverNetworkBox->pack( text );
560     
561      OrxGui::GLGuiText * text2 = new OrxGui::GLGuiText();
562      text2->setText( "Multiplayer TeamDeathMatch Arena" );
563      this->serverNetworkBox->pack( text2 );
564     
565      OrxGui::GLGuiButton* createButton = new OrxGui::GLGuiPushButton("Create Server");
566      serverNetworkBox->pack(createButton);
567      createButton->connect(SIGNAL(createButton, released), this, SLOT(GameMenu, createServer));
568    }
569  }
570 
571  this->serverNetworkBox->showAll();
572 
573  this->serverNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
574}
575
576/**
577 * connect to host
578 */
579void GameMenu::connectToServer( )
580{
581  PRINTF(0)("Connecting to %s\n", this->ipInputLine->_getText().c_str() );
582 
583  State::setOnline(true);
584  NetworkManager::getInstance()->establishConnection( this->ipInputLine->_getText(), 9999 );
585 
586  this->startLevel( 5 );
587}
588
589void GameMenu::createServer( )
590{
591  PRINTF(0)("Create server\n" );
592 
593  State::setOnline(true);
594  NetworkManager::getInstance()->createServer( 9999 );
595 
596  this->startLevel( 5 );
597}
598
599
600
Note: See TracBrowser for help on using the repository browser.