Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/ll2trunktemp/src/game_loader.cc @ 3994

Last change on this file since 3994 was 3989, checked in by bensch, 20 years ago

orxonox/branches/ll2trunktemp: some more headers, now the level really gets loaded

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