| 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 "story_entity.h" |
|---|
| 22 | |
|---|
| 23 | #include "world.h" |
|---|
| 24 | #include "camera.h" |
|---|
| 25 | #include "factory.h" |
|---|
| 26 | #include "game_loader.h" |
|---|
| 27 | |
|---|
| 28 | #include "list.h" |
|---|
| 29 | |
|---|
| 30 | using namespace std; |
|---|
| 31 | |
|---|
| 32 | CREATE_FACTORY(Campaign); |
|---|
| 33 | |
|---|
| 34 | Campaign::Campaign () |
|---|
| 35 | { |
|---|
| 36 | this->entities = new tList<StoryEntity>(); |
|---|
| 37 | this->isInit = false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | Campaign::~Campaign () {} |
|---|
| 42 | |
|---|
| 43 | Campaign::Campaign ( TiXmlElement* root) |
|---|
| 44 | { |
|---|
| 45 | TiXmlElement* element; |
|---|
| 46 | const char* string; |
|---|
| 47 | int id; |
|---|
| 48 | |
|---|
| 49 | PRINTF0("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 | PRINTF0("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 | PRINTF0("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 | StoryEntity* created = (StoryEntity*) loader->fabricate( element); |
|---|
| 80 | if( lastCreated != NULL) created->setNextStoryID( lastCreated->getStoryID()); |
|---|
| 81 | if( created != NULL) |
|---|
| 82 | { |
|---|
| 83 | this->addEntity( created); |
|---|
| 84 | lastCreated = created; |
|---|
| 85 | } |
|---|
| 86 | element = element->NextSiblingElement(); |
|---|
| 87 | } |
|---|
| 88 | if( lastCreated != NULL) lastCreated->setStoryID( WORLD_ID_GAMEEND); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | ErrorMessage Campaign::init() |
|---|
| 92 | { |
|---|
| 93 | this->isInit = true; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | ErrorMessage Campaign::start(int storyID) |
|---|
| 97 | { |
|---|
| 98 | printf("Camapaign::start() - starting new StoryEntity Nr:%i\n", storyID); |
|---|
| 99 | ErrorMessage errorCode; |
|---|
| 100 | if( !this->isInit) return errorCode; |
|---|
| 101 | if( storyID == WORLD_ID_GAMEEND) return errorCode; |
|---|
| 102 | this->running = true; |
|---|
| 103 | StoryEntity* se = this->getStoryEntity(storyID); |
|---|
| 104 | this->currentEntity = se; |
|---|
| 105 | if( se == NULL) |
|---|
| 106 | { |
|---|
| 107 | printf("Camapaign::start() - Start StoryEntity not found\n"); |
|---|
| 108 | return errorCode; |
|---|
| 109 | } |
|---|
| 110 | while( se != NULL && this->running) |
|---|
| 111 | { |
|---|
| 112 | se->displayLoadScreen(); |
|---|
| 113 | se->preLoad(); |
|---|
| 114 | se->load(); |
|---|
| 115 | se->init(); |
|---|
| 116 | se->releaseLoadScreen(); |
|---|
| 117 | se->start(); |
|---|
| 118 | se->destroy(); |
|---|
| 119 | |
|---|
| 120 | delete se; |
|---|
| 121 | |
|---|
| 122 | int nextWorldID = se->getNextStoryID(); |
|---|
| 123 | //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID); |
|---|
| 124 | se = this->getStoryEntity(nextWorldID); |
|---|
| 125 | this->currentEntity = se; |
|---|
| 126 | if( ( nextWorldID == WORLD_ID_GAMEEND) ||( se == NULL) ) |
|---|
| 127 | { |
|---|
| 128 | printf("Campaign::start() - quitting campaing story loop\n"); |
|---|
| 129 | if(se != NULL) |
|---|
| 130 | delete se; |
|---|
| 131 | return errorCode; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | ErrorMessage Campaign::pause() |
|---|
| 139 | { |
|---|
| 140 | if(this->currentEntity != NULL) |
|---|
| 141 | this->isPaused = true; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | ErrorMessage Campaign::resume() |
|---|
| 146 | { |
|---|
| 147 | if(this->currentEntity != NULL) |
|---|
| 148 | this->isPaused = false; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | ErrorMessage Campaign::stop() |
|---|
| 153 | { |
|---|
| 154 | this->running = false; |
|---|
| 155 | if(this->currentEntity != NULL) |
|---|
| 156 | { |
|---|
| 157 | this->currentEntity->stop(); |
|---|
| 158 | //delete this->currentEntity; |
|---|
| 159 | //this->currentEntity = NULL; |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | ErrorMessage Campaign::destroy() |
|---|
| 165 | { |
|---|
| 166 | if(this->currentEntity != NULL) |
|---|
| 167 | { |
|---|
| 168 | this->currentEntity->destroy(); |
|---|
| 169 | delete this->currentEntity; |
|---|
| 170 | this->currentEntity = NULL; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | \brief adds an game stroy entity to the campaign |
|---|
| 177 | |
|---|
| 178 | \param se: The entity |
|---|
| 179 | \param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign |
|---|
| 180 | |
|---|
| 181 | An entity can be a world (playable), a cinematic, a shop, sounds, whatever you |
|---|
| 182 | want to queue up in the campaign. |
|---|
| 183 | */ |
|---|
| 184 | void Campaign::addEntity(StoryEntity* se, int storyID) |
|---|
| 185 | { |
|---|
| 186 | se->setStoryID(storyID); |
|---|
| 187 | this->addEntity(se); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void Campaign::addEntity(StoryEntity* se) |
|---|
| 191 | { |
|---|
| 192 | this->entities->add(se); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | void Campaign::removeEntity(int storyID) |
|---|
| 197 | { |
|---|
| 198 | this->removeEntity(this->getStoryEntity(storyID)); |
|---|
| 199 | |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | void Campaign::removeEntity(StoryEntity* se) |
|---|
| 204 | { |
|---|
| 205 | this->entities->remove(se); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | /* |
|---|
| 210 | \brief this changes to the next level |
|---|
| 211 | */ |
|---|
| 212 | void Campaign::nextLevel() |
|---|
| 213 | { |
|---|
| 214 | printf("Campaign:nextLevel()\n"); |
|---|
| 215 | //int nextID = this->currentEntity->getNextStoryID(); |
|---|
| 216 | //this->stop(); |
|---|
| 217 | //this->start(nextID); |
|---|
| 218 | this->currentEntity->stop(); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /* |
|---|
| 222 | \brief change to the previous level - not implemented |
|---|
| 223 | |
|---|
| 224 | this propably useless |
|---|
| 225 | */ |
|---|
| 226 | void Campaign::previousLevel() |
|---|
| 227 | {} |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | /* |
|---|
| 231 | \brief lookup a entity with a given id |
|---|
| 232 | \param story id to be lookuped |
|---|
| 233 | \returns the entity found or NULL if search ended without match |
|---|
| 234 | */ |
|---|
| 235 | StoryEntity* Campaign::getStoryEntity(int storyID) |
|---|
| 236 | { |
|---|
| 237 | //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID); |
|---|
| 238 | if( storyID == WORLD_ID_GAMEEND) |
|---|
| 239 | return NULL; |
|---|
| 240 | |
|---|
| 241 | /* |
|---|
| 242 | tList<StoryEntity>* l; |
|---|
| 243 | StoryEntity* entity = NULL; |
|---|
| 244 | l = this->entities->getNext(); |
|---|
| 245 | while( l != NULL) |
|---|
| 246 | { |
|---|
| 247 | entity = l->getObject(); |
|---|
| 248 | l = l->getNext(); |
|---|
| 249 | |
|---|
| 250 | int id = entity->getStoryID(); |
|---|
| 251 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
|---|
| 252 | if(id == storyID) |
|---|
| 253 | { |
|---|
| 254 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
|---|
| 255 | return entity; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | } |
|---|
| 259 | */ |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | StoryEntity* entity = this->entities->enumerate(); |
|---|
| 264 | while( entity != NULL) |
|---|
| 265 | { |
|---|
| 266 | int id = entity->getStoryID(); |
|---|
| 267 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
|---|
| 268 | if(id == storyID) |
|---|
| 269 | { |
|---|
| 270 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
|---|
| 271 | return entity; |
|---|
| 272 | } |
|---|
| 273 | entity = this->entities->nextElement(); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | return NULL; |
|---|
| 279 | } |
|---|