Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

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