Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/loading/game_loader.cc @ 5974

Last change on this file since 5974 was 5974, checked in by patrick, 18 years ago

network: cleaned up the game_loader

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