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