Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/multi_player_map/src/story_entities/menu/game_menu.cc @ 9033

Last change on this file since 9033 was 9033, checked in by rennerc, 18 years ago

starting network games from menu works now

File size: 13.6 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
155      OrxGui::GLGuiButton* networkButton = new OrxGui::GLGuiPushButton("MultiPlayer");
156      networkButton->connect(SIGNAL(networkButton, released), this, SLOT(GameMenu, showMultiPlayer));
157      this->mainMenuBox->pack(networkButton);
158
159      OrxGui::GLGuiButton* optionsButton = new OrxGui::GLGuiPushButton("Options");
160      optionsButton->connect(SIGNAL(optionsButton, released), this, SLOT(GameMenu, showOptionsMenu));
161      this->mainMenuBox->pack(optionsButton);
162
163
164      OrxGui::GLGuiButton* quitButton = new OrxGui::GLGuiPushButton("Quit");
165      this->mainMenuBox->pack(quitButton);
166      quitButton->connect(SIGNAL(quitButton, released), this, SLOT(GameMenu, quitMenu));
167    }
168  }
169  this->mainMenuBox->showAll();
170
171  this->mainMenuBox->setRelCoor2D(200, 100);
172}
173
174
175void GameMenu::showCampaigns()
176{
177  if (this->levelsBox == NULL)
178  {
179    this->levelsBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
180    {
181      OrxGui::GLGuiBox* labelBox = new OrxGui::GLGuiBox();
182
183      OrxGui::GLGuiImage* image = new OrxGui::GLGuiImage();
184      image->show();
185      image->setWidgetSize( 250, 200);
186      image->setAbsCoor2D(400, 150);
187      image->setForegroundColor(Color( 1,1,1,.6));
188
189      const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
190      std::list<BaseObject*>::const_iterator it;
191      for( it = storyEntities->begin(); it != storyEntities->end(); it++)
192      {
193        StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
194        if( se->isContainedInMenu())
195        {
196
197          printf("%s\n", se->getMenuScreenshoot().c_str());
198          OrxGui::GLGuiImageButton* button = new OrxGui::GLGuiImageButton(se->getName(), se->getStoryID(), se->getMenuScreenshoot(), image);
199          button->connect(SIGNAL(button, startLevel), this, SLOT(GameMenu, startLevel));
200          labelBox->pack(button);
201
202          // generating screenshoot item
203          /*
204          ImageEntity* ie = new ImageEntity();
205          ie->setVisibility(false);
206          ie->setBindNode((const PNode*)NULL);
207          ie->setTexture(se->getMenuScreenshoot());
208          ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f);
209          ie->setSize2D(140.0f, 105.0f);
210          this->menuLayers[1].screenshootList.push_back(ie);
211          */
212        }
213      }
214
215      this->levelsBox->pack(labelBox);
216      this->levelsBox->pack(image);
217    }
218
219  }
220
221  this->showSecondLevelElement(this->levelsBox);
222
223}
224
225void GameMenu::showMultiPlayer()
226{
227  if (this->networkBox == NULL)
228  {
229    this->networkBox = new OrxGui::GLGuiBox();
230    {
231      OrxGui::GLGuiButton* clientButton = new OrxGui::GLGuiPushButton("Client");
232      networkBox->pack(clientButton);
233      clientButton->connect(SIGNAL(clientButton, released), this, SLOT(GameMenu, showClientMenu));
234
235      OrxGui::GLGuiButton* serverButton = new OrxGui::GLGuiPushButton("Server");
236      networkBox->pack(serverButton);
237      serverButton->connect(SIGNAL(serverButton, released), this, SLOT(GameMenu, showServerMenu));
238
239
240    }
241  }
242
243  this->showSecondLevelElement(this->networkBox);
244
245}
246
247void GameMenu::showOptionsMenu()
248{
249  if (this->optionsBox == NULL)
250  {
251    this->optionsBox = new OrxGui::GLGuiBox();
252    {
253      OrxGui::GLGuiButton* generalButton = new OrxGui::GLGuiPushButton("General");
254      optionsBox->pack(generalButton);
255
256      OrxGui::GLGuiButton* audioButton = new OrxGui::GLGuiPushButton("Audio");
257      optionsBox->pack(audioButton);
258
259      OrxGui::GLGuiButton* videoButton = new OrxGui::GLGuiPushButton("Video");
260      optionsBox->pack(videoButton);
261
262      OrxGui::GLGuiButton* controlButton = new OrxGui::GLGuiPushButton("Control");
263      optionsBox->pack(controlButton);
264
265
266      //      for (unsigned int i = 0; i <
267      //OrxGui::GLGuiButton*
268
269    }
270  }
271
272  this->showSecondLevelElement(this->optionsBox);
273}
274
275
276void GameMenu::showSecondLevelElement(OrxGui::GLGuiBox* element)
277{
278  if (this->currentlyOpened != NULL && this->currentlyOpened != element)
279    this->currentlyOpened->hideAll();
280
281  element->showAll();
282  element->setRelCoor2D(200, 100);
283
284  this->currentlyOpened = element;
285
286  this->mainMenuBox->setRelCoorSoft2D(50, 100, 5);
287}
288
289
290
291
292
293void GameMenu::startLevel(int levelID)
294{
295  this->setNextStoryID( levelID);
296  this->stop();
297}
298
299/**
300* @brief set the Sound to play when switching menu entry.
301* @param selectorSound the sound to load.
302*/
303void GameMenu::setSelectorSound(const std::string& selectorSound)
304{
305  this->selectorSource = OrxSound::SoundEngine::getInstance()->createSource(selectorSound, NULL);
306}
307
308ErrorMessage GameMenu::unloadData()
309{
310  this->unsubscribeEvents(ES_MENU);
311
312  return GameWorld::unloadData();
313}
314
315
316/**
317* @brief start the menu
318*/
319bool GameMenu::start()
320{
321  EventHandler::getInstance()->pushState(ES_MENU);
322
323  this->showMainMenu();
324  OrxGui::GLGuiHandler::getInstance()->activateCursor();
325  OrxGui::GLGuiHandler::getInstance()->activate();
326  OrxGui::GLGuiHandler::getInstance()->cursor()->loadTextureSequence(ResourceManager::getInstance()->getDataDir() + "/" + "maps/reap_mouse/reap_mouse_##.png", 1, 49);
327
328  /* now call the underlying*/
329  return GameWorld::start();
330}
331
332
333
334/**
335* stop the menu
336*/
337bool GameMenu::stop()
338{
339  EventHandler::getInstance()->popState();
340
341  /* now call the underlying*/
342  return GameWorld::stop();
343}
344
345
346/**
347*  override the standard tick for more functionality
348*/
349void GameMenu::tick()
350{
351  GameWorld::tick();
352
353  // Make the GLGui tick.
354  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
355
356  this->animateScene(this->dtS);
357}
358
359
360/**
361* @brief no collision detection in the menu
362*/
363void GameMenu::collide()
364{
365  //   this->dataTank->localCamera->
366}
367
368
369/**
370* @brief animate the scene
371*/
372void GameMenu::animateScene(float dt)
373{
374  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
375  this->cameraVector = q.apply(this->cameraVector);
376  this->dataTank->localCamera->setRelCoor(this->cameraVector);
377  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
378}
379
380void GameMenu::quitMenu()
381{
382  this->setNextStoryID(WORLD_ID_GAMEEND);
383  this->stop();
384}
385
386
387/**
388* @brief event dispatcher funciton
389* @param event the incoming event
390*/
391void GameMenu::process(const Event &event)
392{
393  if( event.type == SDLK_ESCAPE && event.bPressed == true)
394  {
395    this->setNextStoryID(WORLD_ID_GAMEEND);
396    this->stop();
397  }
398
399}
400
401
402
403/**********************************************************************************************
404GameMenuData
405**********************************************************************************************/
406
407
408/**
409* GameMenuData constructor
410*/
411GameMenuData::GameMenuData()
412{}
413
414/**
415* GameMenuData decontructor
416*/
417GameMenuData::~GameMenuData()
418{}
419
420
421/**
422*  initialize the GameWorldDataData
423*/
424ErrorMessage GameMenuData::init()
425{
426  /* call underlying function */
427  return GameWorldData::init();
428}
429
430
431/**
432*  loads the GUI data
433* @param root reference to the xml root element
434*/
435ErrorMessage GameMenuData::loadGUI(const TiXmlElement* root)
436{
437  /* call underlying function */
438  return GameWorldData::loadGUI(root);
439}
440
441
442/**
443*  unloads the GUI data
444*/
445ErrorMessage GameMenuData::unloadGUI()
446{
447  /* call underlying function */
448  return GameWorldData::unloadGUI();
449}
450
451
452/**
453*  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
454* @param root reference to the xml root parameter
455*/
456ErrorMessage GameMenuData::loadWorldEntities(const TiXmlElement* root)
457{
458  return GameWorldData::loadWorldEntities(root);
459}
460
461
462/**
463*  unloads the world entities from the xml file
464*/
465ErrorMessage GameMenuData::unloadWorldEntities()
466{
467  /* call underlying function */
468  return GameWorldData::unloadWorldEntities();
469}
470
471
472/**
473*  loads the scene data
474* @param root reference to the xml root element
475*/
476ErrorMessage GameMenuData::loadScene(const TiXmlElement* root)
477{
478  /* call underlying function */
479  return GameWorldData::loadScene(root);
480}
481
482
483/**
484*  unloads the scene data
485*/
486ErrorMessage GameMenuData::unloadScene()
487{
488  /* call underlying function */
489  return GameWorldData::unloadScene();
490}
491
492/**
493 * show controls to join network game
494 */
495void GameMenu::showClientMenu( )
496{
497  if ( this->serverNetworkBox )
498  {
499    delete this->serverNetworkBox;
500    this->serverNetworkBox = NULL;
501  }
502 
503  if ( !this->clientNetworkBox )
504  {
505    this->clientNetworkBox = new OrxGui::GLGuiBox();
506    {
507      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
508      text->setText( "Host:" );
509      this->clientNetworkBox->pack( text );
510     
511      this->ipInputLine = new OrxGui::GLGuiInputLine( );
512      this->clientNetworkBox->pack( this->ipInputLine );
513      this->ipInputLine->connect(SIGNAL(ipInputLine, enterPushed), this, SLOT(GameMenu, connectToServer));
514      this->ipInputLine->select();
515     
516      OrxGui::GLGuiButton* connectButton = new OrxGui::GLGuiPushButton("connect to server");
517      clientNetworkBox->pack(connectButton);
518      connectButton->connect(SIGNAL(connectButton, released), this, SLOT(GameMenu, connectToServer));
519    }
520  }
521 
522  this->clientNetworkBox->showAll();
523 
524  this->clientNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
525}
526
527/**
528 * show controls to create new network game
529 */
530void GameMenu::showServerMenu( )
531{
532  if ( this->clientNetworkBox )
533  {
534    delete this->clientNetworkBox;
535    this->clientNetworkBox = NULL;
536  }
537 
538  if ( !this->serverNetworkBox )
539  {
540    this->serverNetworkBox = new OrxGui::GLGuiBox();
541    {
542      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
543      text->setText( "Map:" );
544      this->serverNetworkBox->pack( text );
545     
546      OrxGui::GLGuiText * text2 = new OrxGui::GLGuiText();
547      text2->setText( "Multiplayer TeamDeathMatch Arena" );
548      this->serverNetworkBox->pack( text2 );
549     
550      OrxGui::GLGuiButton* createButton = new OrxGui::GLGuiPushButton("create server");
551      serverNetworkBox->pack(createButton);
552      createButton->connect(SIGNAL(createButton, released), this, SLOT(GameMenu, createServer));
553    }
554  }
555 
556  this->serverNetworkBox->showAll();
557 
558  this->serverNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
559}
560
561/**
562 * connect to host
563 */
564void GameMenu::connectToServer( )
565{
566  PRINTF(0)("Connecting to %s\n", this->ipInputLine->_getText().c_str() );
567 
568  State::setOnline(true);
569  NetworkManager::getInstance()->establishConnection( this->ipInputLine->_getText(), 9999 );
570 
571  this->startLevel( 5 );
572}
573
574void GameMenu::createServer( )
575{
576  PRINTF(0)("Create server\n" );
577 
578  State::setOnline(true);
579  NetworkManager::getInstance()->createServer( 9999 );
580 
581  this->startLevel( 5 );
582}
583
584
585
Note: See TracBrowser for help on using the repository browser.