Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: valgrind mem-leak-recovered

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