Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/story_entities/campaign.cc @ 5757

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

orxonox/trunk: renamed class LoadParam to CLoadParam and Macro LoadParamNew to LoadParam

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