Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9486 was 9110, checked in by bensch, 18 years ago

orxonox/trunk: merged the Presentation back

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