Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/story_entities/campaign.cc @ 6372

Last change on this file since 6372 was 6372, checked in by patrick, 18 years ago

network: more cleanup in the StoryEntity, thightening the interface and function renames

File size: 6.1 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
15*/
16
17
18#include "campaign.h"
[3629]19
[5982]20#include "factory.h"
[4261]21#include "load_param.h"
22
[5651]23
[2636]24using namespace std;
25
26
[4597]27Campaign::Campaign ()
[2636]28{
[4597]29  this->setClassID(CL_CAMPAIGN, "Campaign");
[2636]30  this->isInit = false;
31}
[4261]32
[4597]33Campaign::Campaign ( TiXmlElement* root)
[4010]34{
[4597]35  this->setClassID(CL_CAMPAIGN, "Campaign");
36
[5300]37  PRINTF(4)("Loading Campaign...\n");
[4597]38
[4010]39  assert( root != NULL);
[4597]40
[4010]41  this->isInit = false;
[4597]42
[4598]43  this->loadParams(root);
[4597]44
45
46  //if( lastCreated != NULL)
[4010]47  //lastCreated->setStoryID( WORLD_ID_GAMEEND);
48}
[2636]49
[4816]50Campaign::~Campaign ()
51{}
[2636]52
53
[6368]54/**
55 *  initializes the class
56 */
[3222]57ErrorMessage Campaign::init()
[2636]58{
59  this->isInit = true;
60}
61
[6368]62
[4598]63/**
[6368]64 *  loads the Parameters of a Campaign
65 * @param root: The XML-element to load from
[4598]66 */
67void Campaign::loadParams(const TiXmlElement* root)
68{
[4600]69  static_cast<BaseObject*>(this)->loadParams(root);
[2636]70
[5671]71  LoadParam(root, "identifier", this, Campaign, setStoryID)
[5651]72     .describe("A Unique Identifier for this Campaign");
[4598]73
[5651]74   LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams)
[4600]75      .describe("A List of Worlds to be loaded in this Campaign");
[4598]76}
77
[6368]78
[4598]79/**
[6368]80 *  loads a WorldList
81 * @param root: the XML-element to load from
[4598]82 */
83void Campaign::loadWorldListParams(const TiXmlElement* root)
84{
[5651]85  if (root == NULL)
86    return;
87
[5751]88  LOAD_PARAM_START_CYCLE(root, element);
[4598]89  {
[5300]90    PRINTF(5)("Campaign: Constructor: adding a world\n");
[5982]91    StoryEntity* created = (StoryEntity*) Factory::fabricate(element);
[4598]92    if( created != NULL)
93    {
94      this->addEntity( created);
95    }
96  }
[5751]97  LOAD_PARAM_END_CYCLE(element);
[4598]98}
99
[6368]100/**
101 *  starts the campaing
102 */
[3222]103ErrorMessage Campaign::start()
[2636]104{
105  this->start(0);
106}
107
[6368]108
109/**
110 *  starts the campaing with a specific ID
111 * @param storyID the id of the StoryEntity
112 */
[3222]113ErrorMessage Campaign::start(int storyID = 0)
[2636]114{
[3222]115  ErrorMessage errorCode;
[4597]116  if( !this->isInit) return errorCode;
[3221]117  if( storyID == WORLD_ID_GAMEEND) return errorCode;
[2636]118  this->running = true;
119  StoryEntity* se = this->getStoryEntity(storyID);
[3220]120  this->currentEntity = se;
[3221]121  while( se != NULL && this->running)
[2636]122    {
[4324]123      PRINTF(0)("Starting new StoryEntity Nr:%i\n", se->getStoryID());
[6370]124
125      se->init();
126
127      //se->preLoad();
[6372]128      se->loadData();
[6370]129      //se->postLoad();
[6152]130
[6370]131
[6152]132      se->preStart();
[2636]133      se->start();
[4597]134
[5215]135      int nextWorldID = se->getNextStoryID();
136
[5773]137      this->entities.remove(se);
[3220]138      delete se;
139
140      //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID);
[2636]141      se = this->getStoryEntity(nextWorldID);
[3220]142      this->currentEntity = se;
[6368]143      if( ( nextWorldID == WORLD_ID_GAMEEND) || ( se == NULL) )
[4597]144        {
[5305]145          PRINTF(4)("Quitting campaing story loop\n");
[4597]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    */
[5773]155    list<StoryEntity*>::iterator it;
156    for(it = this->entities.begin(); it != entities.end(); it++)
[4816]157    {
[5773]158      delete (*it);
[2636]159    }
[6139]160    PRINTF(1)("There is no StoryEnity left to play, quitting\n");
[2636]161}
162
163
[6368]164/**
165 *  pauses the campaing
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
[6371]190/*
[3459]191ErrorMessage Campaign::destroy()
192{
[3220]193  if(this->currentEntity != NULL)
194    {
195      this->currentEntity->destroy();
196      delete this->currentEntity;
197      this->currentEntity = NULL;
198    }
[6371]199}*/
[3220]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{
[5773]219  this->entities.push_back(se);
[3459]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{
[5773]232  this->entities.remove(se);
[3459]233}
234
235
[3225]236/*
237  \brief this changes to the next level
238*/
[2636]239void Campaign::nextLevel()
240{
[5324]241  PRINTF(4)("Campaign:nextLevel()\n");
[3220]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
[5773]286  list<StoryEntity*>::iterator it;
287  for (it = this->entities.begin(); it != this->entities.end(); it++)
288  {
289      int id = (*it)->getStoryID();
[3608]290      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
291      if(id == storyID)
[4597]292        {
293          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
[5773]294          return (*it);
[4597]295        }
[3608]296    }
297
298
[5773]299
[2636]300  return NULL;
301}
Note: See TracBrowser for help on using the repository browser.