Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/loading/game_loader.cc @ 4410

Last change on this file since 4410 was 4410, checked in by patrick, 19 years ago

orxonox/trunk: prepeared to implement all system wide commands

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