Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7069 was 6862, checked in by patrick, 19 years ago

trunk: the game now always jumps back to the menu

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