| [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] | 31 | using namespace std; | 
|---|
 | 32 |  | 
|---|
 | 33 |  | 
|---|
| [4597] | 34 | Campaign::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] | 41 | Campaign::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] | 59 | Campaign::~Campaign () | 
|---|
 | 60 | {} | 
|---|
| [2636] | 61 |  | 
|---|
 | 62 |  | 
|---|
| [3222] | 63 | ErrorMessage 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 |  */ | 
|---|
 | 72 | void 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 |  */ | 
|---|
 | 87 | void 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] | 111 | ErrorMessage Campaign::start() | 
|---|
| [2636] | 112 | { | 
|---|
 | 113 |   this->start(0); | 
|---|
 | 114 | } | 
|---|
 | 115 |  | 
|---|
| [3459] | 116 |  | 
|---|
| [3222] | 117 | ErrorMessage 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; | 
|---|
| [2636] | 163 | } | 
|---|
 | 164 |  | 
|---|
 | 165 |  | 
|---|
| [3222] | 166 | ErrorMessage Campaign::pause() | 
|---|
| [2636] | 167 | { | 
|---|
 | 168 |   if(this->currentEntity != NULL) | 
|---|
 | 169 |     this->isPaused = true; | 
|---|
 | 170 | } | 
|---|
 | 171 |  | 
|---|
 | 172 |  | 
|---|
| [3222] | 173 | ErrorMessage Campaign::resume() | 
|---|
| [2636] | 174 | { | 
|---|
 | 175 |   if(this->currentEntity != NULL) | 
|---|
 | 176 |     this->isPaused = false; | 
|---|
 | 177 | } | 
|---|
 | 178 |  | 
|---|
 | 179 |  | 
|---|
| [3459] | 180 | ErrorMessage Campaign::stop() | 
|---|
| [3220] | 181 | { | 
|---|
| [3459] | 182 |   this->running = false; | 
|---|
| [4597] | 183 |   if(this->currentEntity != NULL) | 
|---|
| [3459] | 184 |     { | 
|---|
 | 185 |       this->currentEntity->stop(); | 
|---|
 | 186 |     } | 
|---|
 | 187 | } | 
|---|
 | 188 |  | 
|---|
 | 189 |  | 
|---|
 | 190 | ErrorMessage Campaign::destroy() | 
|---|
 | 191 | { | 
|---|
| [3220] | 192 |   if(this->currentEntity != NULL) | 
|---|
 | 193 |     { | 
|---|
 | 194 |       this->currentEntity->destroy(); | 
|---|
 | 195 |       delete this->currentEntity; | 
|---|
 | 196 |       this->currentEntity = NULL; | 
|---|
 | 197 |     } | 
|---|
 | 198 | } | 
|---|
 | 199 |  | 
|---|
| [3459] | 200 |  | 
|---|
| [4597] | 201 | /** | 
|---|
| [4836] | 202 |   *  adds an game stroy entity to the campaign | 
|---|
| [3459] | 203 |  | 
|---|
| [4836] | 204 |   * @param se: The entity | 
|---|
 | 205 |   * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign | 
|---|
| [3459] | 206 |  | 
|---|
 | 207 |     An entity can be a world (playable), a cinematic, a shop, sounds, whatever you | 
|---|
 | 208 |     want to queue up in the campaign. | 
|---|
 | 209 | */ | 
|---|
 | 210 | void Campaign::addEntity(StoryEntity* se, int storyID) | 
|---|
 | 211 | { | 
|---|
 | 212 |   se->setStoryID(storyID); | 
|---|
 | 213 |   this->addEntity(se); | 
|---|
 | 214 | } | 
|---|
 | 215 |  | 
|---|
 | 216 | void Campaign::addEntity(StoryEntity* se) | 
|---|
 | 217 | { | 
|---|
 | 218 |   this->entities->add(se); | 
|---|
 | 219 | } | 
|---|
 | 220 |  | 
|---|
 | 221 |  | 
|---|
 | 222 | void Campaign::removeEntity(int storyID) | 
|---|
 | 223 | { | 
|---|
 | 224 |   this->removeEntity(this->getStoryEntity(storyID)); | 
|---|
| [4597] | 225 |  | 
|---|
| [3459] | 226 | } | 
|---|
 | 227 |  | 
|---|
 | 228 |  | 
|---|
 | 229 | void Campaign::removeEntity(StoryEntity* se) | 
|---|
 | 230 | { | 
|---|
 | 231 |   this->entities->remove(se); | 
|---|
 | 232 | } | 
|---|
 | 233 |  | 
|---|
 | 234 |  | 
|---|
| [3225] | 235 | /* | 
|---|
 | 236 |   \brief this changes to the next level | 
|---|
 | 237 | */ | 
|---|
| [2636] | 238 | void Campaign::nextLevel() | 
|---|
 | 239 | { | 
|---|
| [3220] | 240 |   printf("Campaign:nextLevel()\n"); | 
|---|
 | 241 |   this->currentEntity->stop(); | 
|---|
| [2636] | 242 | } | 
|---|
 | 243 |  | 
|---|
| [3225] | 244 | /* | 
|---|
 | 245 |   \brief change to the previous level - not implemented | 
|---|
 | 246 |  | 
|---|
 | 247 |   this propably useless | 
|---|
 | 248 | */ | 
|---|
| [2636] | 249 | void Campaign::previousLevel() | 
|---|
 | 250 | {} | 
|---|
 | 251 |  | 
|---|
 | 252 |  | 
|---|
| [3225] | 253 | /* | 
|---|
 | 254 |   \brief lookup a entity with a given id | 
|---|
| [4836] | 255 | * @param story id to be lookuped | 
|---|
 | 256 |   @returns the entity found or NULL if search ended without match | 
|---|
| [3225] | 257 | */ | 
|---|
| [3220] | 258 | StoryEntity* Campaign::getStoryEntity(int storyID) | 
|---|
| [2636] | 259 | { | 
|---|
| [3220] | 260 |   //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID); | 
|---|
 | 261 |   if( storyID == WORLD_ID_GAMEEND) | 
|---|
 | 262 |     return NULL; | 
|---|
| [3608] | 263 |  | 
|---|
 | 264 |   /* | 
|---|
 | 265 |   tList<StoryEntity>* l; | 
|---|
| [3220] | 266 |   StoryEntity* entity = NULL; | 
|---|
| [4597] | 267 |   l = this->entities->getNext(); | 
|---|
 | 268 |   while( l != NULL) | 
|---|
 | 269 |     { | 
|---|
| [3231] | 270 |       entity = l->getObject(); | 
|---|
 | 271 |       l = l->getNext(); | 
|---|
| [3608] | 272 |  | 
|---|
| [3220] | 273 |       int id = entity->getStoryID(); | 
|---|
 | 274 |       //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); | 
|---|
 | 275 |       if(id == storyID) | 
|---|
| [4597] | 276 |         { | 
|---|
 | 277 |           //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); | 
|---|
 | 278 |           return entity; | 
|---|
 | 279 |         } | 
|---|
| [3608] | 280 |  | 
|---|
| [2636] | 281 |     } | 
|---|
| [3608] | 282 |   */ | 
|---|
 | 283 |  | 
|---|
 | 284 |  | 
|---|
| [3832] | 285 |   tIterator<StoryEntity>* iterator = this->entities->getIterator(); | 
|---|
| [5115] | 286 |   StoryEntity* entity = iterator->firstElement(); | 
|---|
| [4597] | 287 |   while( entity != NULL) | 
|---|
 | 288 |     { | 
|---|
| [3608] | 289 |       int id = entity->getStoryID(); | 
|---|
 | 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"); | 
|---|
 | 294 |           return entity; | 
|---|
 | 295 |         } | 
|---|
| [3832] | 296 |       entity = iterator->nextElement(); | 
|---|
| [3608] | 297 |     } | 
|---|
| [3832] | 298 |   delete iterator; | 
|---|
| [3608] | 299 |  | 
|---|
 | 300 |  | 
|---|
| [2636] | 301 |   return NULL; | 
|---|
 | 302 | } | 
|---|