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