Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/game_loader.cc @ 9684

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

orxonox/branches/new_class_id: slowly but surely reimplementing to the new groove… way to go

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