Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/game_loader.cc @ 7930

Last change on this file since 7930 was 7868, checked in by bensch, 18 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

File size: 7.0 KB
RevLine 
[2636]1
2
[4597]3/*
[2636]4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
[5300]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
19
[2636]20#include "game_loader.h"
[7193]21#include "util/loading/load_param.h"
[5139]22
23#include "shell_command.h"
[2636]24#include "campaign.h"
[6152]25
[7193]26#include "util/loading/resource_manager.h"
[6152]27
[7868]28#include "key_mapper.h"
[2636]29
30
31
[7868]32SHELL_COMMAND(quit, GameLoader, stop)
33->describe("quits the game")
34->setAlias("orxoquit");
[2636]35
36
[7868]37GameLoader* GameLoader::singletonRef = NULL;
[5162]38
39
[7868]40/**
41 *  simple constructor
42 */
43GameLoader::GameLoader ()
44{
45  this->setClassID(CL_GAME_LOADER, "GameLoader");
46  this->setName("GameLoader");
47  this->bRun = true;
48}
[5162]49
[2636]50
[7868]51/**
52 *  simple deconstructor
53 */
54GameLoader::~GameLoader ()
55{
56  if( this->currentCampaign)
57    delete this->currentCampaign;
58  this->currentCampaign = NULL;
59}
[2636]60
61
[7868]62/**
63 *  initializes the GameLoader
64 */
65ErrorMessage GameLoader::init()
66{
67  if(this->currentCampaign != NULL)
68    this->currentCampaign->init();
[2636]69
[7868]70  this->subscribeEvent(ES_GAME, KeyMapper::PEV_PAUSE);
71  this->subscribeEvent(ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
72  this->subscribeEvent(ES_GAME, KeyMapper::PEV_QUIT);
73  this->subscribeEvent(ES_GAME, KeyMapper::PEV_NEXT_WORLD);
74  this->subscribeEvent(ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
75}
[4411]76
[2636]77
[7868]78/**
79 *  reads a campaign definition file into a campaign class
80 * @param fileName to be loaded
81 * @returns the loaded campaign
82 *
83 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
84 */
85ErrorMessage GameLoader::loadCampaign(const std::string& fileName)
86{
87  ErrorMessage errorCode;
88  std::string campaignName = ResourceManager::getFullName(fileName);
89  if (!campaignName.empty())
[7374]90  {
[7868]91    this->currentCampaign = this->fileToCampaign(campaignName);
[7374]92  }
[7868]93}
[2636]94
[6139]95
[7868]96/**
97 *  reads a campaign definition file into a campaign class
98 * @param fileName to be loaded
99 * @returns the loaded campaign
100 *
101 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
102 */
103ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName)
104{
105  ErrorMessage errorCode;
106  std::string campaignName = ResourceManager::getFullName(fileName);
107  if (!campaignName.empty())
[6139]108  {
[7868]109    this->currentCampaign = this->fileToCampaign(campaignName);
[6139]110  }
[7868]111}
[6139]112
113
[7868]114/**
115 *  loads a debug campaign for test purposes only.
116 * @param campaignID the identifier of the campaign.
117 * @returns error message if not able to do so.
118 */
119ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
120{
121  switch(campaignID)
[7374]122  {
[7868]123      /*
124         Debug Level 0: Debug level used to test the base frame work.
125         As you can see, all storyentity data is allocated before game
126         start. the storyentity will load themselfs shortly before start
127         through the StoryEntity::init() funtion.
128      */
129    case DEBUG_CAMPAIGN_0:
130      {
131        /*        Campaign* debugCampaign = new Campaign();
[3220]132
[7868]133                World* world0 = new World(DEBUG_WORLD_0);
134                world0->setNextStoryID(WORLD_ID_1);
135                debugCampaign->addEntity(world0, WORLD_ID_0);
[3220]136
[7868]137                World* world1 = new World(DEBUG_WORLD_1);
138                world1->setNextStoryID(WORLD_ID_2);
139                debugCampaign->addEntity(world1, WORLD_ID_1);
[2636]140
[7868]141                World* world2 = new World(DEBUG_WORLD_2);
142                world2->setNextStoryID(WORLD_ID_GAMEEND);
143                debugCampaign->addEntity(world2, WORLD_ID_2);
[3727]144
[7868]145                this->currentCampaign = debugCampaign;
146                break;*/
147      }
[7374]148  }
[7868]149}
[2636]150
[4443]151
[7868]152/**
153 *  starts the current entity
154 * @returns error code if this action has caused a error
155 */
156ErrorMessage GameLoader::start()
157{
158  if(this->currentCampaign != NULL)
[6862]159  {
[7868]160    this->currentCampaign->start();
[6862]161  }
[7868]162}
[2636]163
164
[7868]165/**
166 *  stops the current entity
167 * @returns error code if this action has caused a error
168 *
169 *  ATTENTION: this function shouldn't call other functions, or if so, they must return
170 *  after finishing. If you ignore or forget to do so, the current entity is not able to
171 *  terminate and it will run in the background or the ressources can't be freed or even
172 *  worse: are freed and the program will end in a segmentation fault!
173 *  hehehe, have ya seen it... :)
174 */
175void GameLoader::stop()
176{
177  if(this->currentCampaign != NULL)
178    this->currentCampaign->stop();
179}
[2636]180
181
[7868]182/**
183 *  pause the current entity
184 * @returns error code if this action has caused a error
185 *
186 * this pauses the current entity or passes this call forth to the running entity.
187 */
188ErrorMessage GameLoader::pause()
189{
190  this->isPaused = true;
191  if(this->currentCampaign != NULL)
192    this->currentCampaign->pause();
193}
194
195
196/**
197 *  resumes a pause
198 * @returns error code if this action has caused a error
199 *
200 *  this resumess the current entity or passes this call forth to the running entity.
201 */
202ErrorMessage GameLoader::resume()
203{
204  this->isPaused = false;
205  if(this->currentCampaign != NULL)
206    this->currentCampaign->resume();
207}
208
209
210/**
211 *  reads a campaign definition file into a campaign class
212 * @param fileName to be loaded
213 * @returns the loaded campaign
214 *
215 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
216 */
217Campaign* GameLoader::fileToCampaign(const std::string& fileName)
218{
219  /* do not entirely load the campaign. just the current world
220     before start of each world, it has to be initialized so it
221     can load everything it needs into memory then.
222  */
223
224  if( fileName.empty())
[7374]225  {
[7868]226    PRINTF(2)("No filename specified for loading");
227    return NULL;
[7374]228  }
[2636]229
[7868]230  TiXmlDocument XMLDoc(fileName);
231  // load the campaign document
232  if( !XMLDoc.LoadFile(fileName))
[7374]233  {
[7868]234    // report an error
235    PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName.c_str(), XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol());
236    return NULL;
[7374]237  }
[2636]238
[7868]239  // check basic validity
240  TiXmlElement* root = XMLDoc.RootElement();
241  assert( root != NULL);
[4443]242
[7868]243  if( strcmp( root->Value(), "Campaign"))
[7374]244  {
[7868]245    // report an error
246    PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
247    return NULL;
248  }
[4597]249
[7868]250  // construct campaign
251  return new Campaign( root);
252}
[4597]253
254
255
[7868]256/**
257 *  handle keyboard commands
258 * @param event the event to handle
259 */
260void GameLoader::process(const Event& event)
261{
262  if( event.type == KeyMapper::PEV_NEXT_WORLD)
263  {
264    if( likely(event.bPressed))
[4010]265    {
[7868]266      this->switchToNextLevel();
[4010]267    }
[7374]268  }
[7868]269  else if( event.type == KeyMapper::PEV_PAUSE)
[5553]270  {
[7868]271    if( likely(event.bPressed))
[4410]272    {
[7868]273      if(this->isPaused)
274        this->resume();
275      else
276        this->pause();
[4410]277    }
[5553]278  }
[7868]279  else if( event.type == KeyMapper::PEV_QUIT)
[5553]280  {
[7868]281    if( event.bPressed) this->stop();
[5553]282  }
[7868]283  else if (event.type == EV_MAIN_QUIT)
284    this->stop();
285}
[4410]286
[7868]287
288/**
289 *  this changes to the next level
290 */
291void GameLoader::switchToNextLevel()
292{
293  if(this->currentCampaign != NULL)
294    this->currentCampaign->switchToNextLevel();
[2636]295}
[7868]296
Note: See TracBrowser for help on using the repository browser.