Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6362 was 6360, checked in by patrick, 19 years ago

network: repear work on the world chaos. cleaned up campaing.cc btw

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