Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

slight change in the make process

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