Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

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