Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

docu

File size: 6.0 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 "factory.h"
22#include "game_loader.h"
23#include "story_entity.h"
24
25#include "world.h"
26#include "camera.h"
27
28#include "load_param.h"
29
30
31using namespace std;
32
33
34Campaign::Campaign ()
35{
36  this->setClassID(CL_CAMPAIGN, "Campaign");
37  this->isInit = false;
38}
39
40Campaign::Campaign ( TiXmlElement* root)
41{
42  this->setClassID(CL_CAMPAIGN, "Campaign");
43
44  PRINTF(4)("Loading Campaign...\n");
45
46  assert( root != NULL);
47
48  this->isInit = false;
49
50  this->loadParams(root);
51
52
53  //if( lastCreated != NULL)
54  //lastCreated->setStoryID( WORLD_ID_GAMEEND);
55}
56
57Campaign::~Campaign ()
58{}
59
60
61ErrorMessage Campaign::init()
62{
63  this->isInit = true;
64}
65
66/**
67  \brief loads the Parameters of a Campaign
68* @param root: The XML-element to load from
69 */
70void Campaign::loadParams(const TiXmlElement* root)
71{
72  static_cast<BaseObject*>(this)->loadParams(root);
73
74  LoadParam(root, "identifier", this, Campaign, setStoryID)
75     .describe("A Unique Identifier for this Campaign");
76
77   LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams)
78      .describe("A List of Worlds to be loaded in this Campaign");
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  if (root == NULL)
88    return;
89
90  LOAD_PARAM_START_CYCLE(root, element);
91  {
92    PRINTF(5)("Campaign: Constructor: adding a world\n");
93    StoryEntity* created = (StoryEntity*) Factory::fabricate(element);
94    if( created != NULL)
95    {
96      this->addEntity( created);
97    }
98  }
99  LOAD_PARAM_END_CYCLE(element);
100}
101
102ErrorMessage Campaign::start()
103{
104  this->start(0);
105}
106
107//! @todo boky -> fix it
108ErrorMessage Campaign::start(int storyID = 0)
109{
110  ErrorMessage errorCode;
111  if( !this->isInit) return errorCode;
112  if( storyID == WORLD_ID_GAMEEND) return errorCode;
113  this->running = true;
114  StoryEntity* se = this->getStoryEntity(storyID);
115  this->currentEntity = se;
116  while( se != NULL && this->running)
117    {
118      PRINTF(0)("Starting new StoryEntity Nr:%i\n", se->getStoryID());
119      se->preLoad();
120      se->load();
121      se->postLoad();
122
123      se->init();
124      se->preStart();
125      se->start();
126      se->destroy();
127
128      int nextWorldID = se->getNextStoryID();
129
130      this->entities.remove(se);
131      delete se;
132
133      //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID);
134      se = this->getStoryEntity(nextWorldID);
135      this->currentEntity = se;
136      if( ( nextWorldID == WORLD_ID_GAMEEND) ||( se == NULL) )
137        {
138          PRINTF(4)("Quitting campaing story loop\n");
139          if(se != NULL)
140            delete se;
141          return errorCode;
142        }
143    }
144
145    /* clean up all world that have not been loaded and therefore are still in the world list  -
146       this probably does not belong into the start function. remove this later
147    */
148    list<StoryEntity*>::iterator it;
149    for(it = this->entities.begin(); it != entities.end(); it++)
150    {
151      delete (*it);
152    }
153    PRINTF(1)("There is no StoryEnity left to play, quitting\n");
154}
155
156
157ErrorMessage Campaign::pause()
158{
159  if(this->currentEntity != NULL)
160    this->isPaused = true;
161}
162
163
164ErrorMessage Campaign::resume()
165{
166  if(this->currentEntity != NULL)
167    this->isPaused = false;
168}
169
170
171ErrorMessage Campaign::stop()
172{
173  this->running = false;
174  if(this->currentEntity != NULL)
175    {
176      this->currentEntity->stop();
177    }
178}
179
180
181ErrorMessage Campaign::destroy()
182{
183  if(this->currentEntity != NULL)
184    {
185      this->currentEntity->destroy();
186      delete this->currentEntity;
187      this->currentEntity = NULL;
188    }
189}
190
191
192/**
193  *  adds an game stroy entity to the campaign
194
195  * @param se: The entity
196  * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign
197
198    An entity can be a world (playable), a cinematic, a shop, sounds, whatever you
199    want to queue up in the campaign.
200*/
201void Campaign::addEntity(StoryEntity* se, int storyID)
202{
203  se->setStoryID(storyID);
204  this->addEntity(se);
205}
206
207void Campaign::addEntity(StoryEntity* se)
208{
209  this->entities.push_back(se);
210}
211
212
213void Campaign::removeEntity(int storyID)
214{
215  this->removeEntity(this->getStoryEntity(storyID));
216
217}
218
219
220void Campaign::removeEntity(StoryEntity* se)
221{
222  this->entities.remove(se);
223}
224
225
226/*
227  \brief this changes to the next level
228*/
229void Campaign::nextLevel()
230{
231  PRINTF(4)("Campaign:nextLevel()\n");
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  list<StoryEntity*>::iterator it;
277  for (it = this->entities.begin(); it != this->entities.end(); it++)
278  {
279      int id = (*it)->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 (*it);
285        }
286    }
287
288
289
290  return NULL;
291}
Note: See TracBrowser for help on using the repository browser.