Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/story_entities/campaign.cc @ 4599

Last change on this file since 4599 was 4599, checked in by bensch, 19 years ago

orxonox/trunk: loading of XML-elements is real smooth now

File size: 6.1 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 "campaign.h"
20
21#include "game_loader.h"
22#include "story_entity.h"
23
24#include "world.h"
25#include "camera.h"
26
27#include "list.h"
28
29#include "load_param.h"
30
31using namespace std;
32
33
34Campaign::Campaign ()
35{
36  this->setClassID(CL_CAMPAIGN, "Campaign");
37  this->entities = new tList<StoryEntity>();
38  this->isInit = false;
39}
40
41Campaign::Campaign ( TiXmlElement* root)
42{
43  this->setClassID(CL_CAMPAIGN, "Campaign");
44
45  PRINTF(3)("Loading Campaign...\n");
46
47  assert( root != NULL);
48
49  this->entities = new tList<StoryEntity>();
50  this->isInit = false;
51
52  this->loadParams(root);
53
54
55  //if( lastCreated != NULL)
56  //lastCreated->setStoryID( WORLD_ID_GAMEEND);
57}
58
59Campaign::~Campaign () {}
60
61
62ErrorMessage Campaign::init()
63{
64  this->isInit = true;
65}
66
67/**
68  \brief loads the Parameters of a Campaign
69  \param root: The XML-element to load from
70 */
71void Campaign::loadParams(const TiXmlElement* root)
72{
73  TiXmlElement* element;
74  const char* string;
75
76  LoadParam<Campaign>(root, "identifier", this, &Campaign::setStoryID);
77
78  LoadParam<Campaign>(root, "WorldList", this, &Campaign::loadWorldListParams);
79}
80
81/**
82  \brief loads a WorldList
83  \param root: the XML-element to load from
84 */
85void Campaign::loadWorldListParams(const TiXmlElement* root)
86{
87  const TiXmlElement* element = root->FirstChildElement();
88  // load Worlds/Subcampaigns/Whatever
89  StoryEntity* lastCreated = NULL;
90  while( element != NULL)
91  {
92    printf("Campaign: Constructor: adding a world\n");
93    StoryEntity* created = (StoryEntity*) GameLoader::getInstance()->fabricate(element);
94      /*
95    if( lastCreated != NULL)
96    created->setNextStoryID( lastCreated->getStoryID());
97    else
98    created->setNextStoryID( WORLD_ID_GAMEEND);
99      */
100    if( created != NULL)
101    {
102      this->addEntity( created);
103      lastCreated = created;
104    }
105    element = element->NextSiblingElement();
106  }
107}
108
109ErrorMessage Campaign::start()
110{
111  this->start(0);
112}
113
114
115ErrorMessage Campaign::start(int storyID = 0)
116{
117  ErrorMessage errorCode;
118  if( !this->isInit) return errorCode;
119  if( storyID == WORLD_ID_GAMEEND) return errorCode;
120  this->running = true;
121  StoryEntity* se = this->getStoryEntity(storyID);
122  this->currentEntity = se;
123  while( se != NULL && this->running)
124    {
125      PRINTF(0)("Starting new StoryEntity Nr:%i\n", se->getStoryID());
126      se->displayLoadScreen();
127      se->preLoad();
128      se->load();
129      se->init();
130      se->releaseLoadScreen();
131      se->start();
132      se->destroy();
133
134      delete se;
135
136      int nextWorldID = se->getNextStoryID();
137      //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID);
138      se = this->getStoryEntity(nextWorldID);
139      this->currentEntity = se;
140      if( ( nextWorldID == WORLD_ID_GAMEEND) ||( se == NULL) )
141        {
142          PRINTF(0)("Quitting campaing story loop\n");
143          if(se != NULL)
144            delete se;
145          return errorCode;
146        }
147
148    }
149}
150
151
152ErrorMessage Campaign::pause()
153{
154  if(this->currentEntity != NULL)
155    this->isPaused = true;
156}
157
158
159ErrorMessage Campaign::resume()
160{
161  if(this->currentEntity != NULL)
162    this->isPaused = false;
163}
164
165
166ErrorMessage Campaign::stop()
167{
168  this->running = false;
169  if(this->currentEntity != NULL)
170    {
171      this->currentEntity->stop();
172      //delete this->currentEntity;
173      //this->currentEntity = NULL;
174    }
175}
176
177
178ErrorMessage Campaign::destroy()
179{
180  if(this->currentEntity != NULL)
181    {
182      this->currentEntity->destroy();
183      delete this->currentEntity;
184      this->currentEntity = NULL;
185    }
186}
187
188
189/**
190    \brief adds an game stroy entity to the campaign
191
192    \param se: The entity
193    \param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign
194
195    An entity can be a world (playable), a cinematic, a shop, sounds, whatever you
196    want to queue up in the campaign.
197*/
198void Campaign::addEntity(StoryEntity* se, int storyID)
199{
200  se->setStoryID(storyID);
201  this->addEntity(se);
202}
203
204void Campaign::addEntity(StoryEntity* se)
205{
206  this->entities->add(se);
207}
208
209
210void Campaign::removeEntity(int storyID)
211{
212  this->removeEntity(this->getStoryEntity(storyID));
213
214}
215
216
217void Campaign::removeEntity(StoryEntity* se)
218{
219  this->entities->remove(se);
220}
221
222
223/*
224  \brief this changes to the next level
225*/
226void Campaign::nextLevel()
227{
228  printf("Campaign:nextLevel()\n");
229  //int nextID = this->currentEntity->getNextStoryID();
230  //this->stop();
231  //this->start(nextID);
232  this->currentEntity->stop();
233}
234
235/*
236  \brief change to the previous level - not implemented
237
238  this propably useless
239*/
240void Campaign::previousLevel()
241{}
242
243
244/*
245  \brief lookup a entity with a given id
246  \param story id to be lookuped
247  \returns the entity found or NULL if search ended without match
248*/
249StoryEntity* Campaign::getStoryEntity(int storyID)
250{
251  //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID);
252  if( storyID == WORLD_ID_GAMEEND)
253    return NULL;
254
255  /*
256  tList<StoryEntity>* l;
257  StoryEntity* entity = NULL;
258  l = this->entities->getNext();
259  while( l != NULL)
260    {
261      entity = l->getObject();
262      l = l->getNext();
263
264      int id = entity->getStoryID();
265      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
266      if(id == storyID)
267        {
268          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
269          return entity;
270        }
271
272    }
273  */
274
275
276  tIterator<StoryEntity>* iterator = this->entities->getIterator();
277  StoryEntity* entity = iterator->nextElement();
278  while( entity != NULL)
279    {
280      int id = entity->getStoryID();
281      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
282      if(id == storyID)
283        {
284          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
285          return entity;
286        }
287      entity = iterator->nextElement();
288    }
289  delete iterator;
290
291
292  return NULL;
293}
Note: See TracBrowser for help on using the repository browser.