Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/lib/util/loading/game_loader.cc @ 10613

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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