[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 |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | #include "campaign.h" |
---|
[3629] | 19 | |
---|
[5982] | 20 | #include "factory.h" |
---|
[4261] | 21 | #include "load_param.h" |
---|
| 22 | |
---|
[5651] | 23 | |
---|
[2636] | 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | |
---|
[6374] | 27 | /** |
---|
| 28 | * the constructor |
---|
| 29 | * @param root the XML root element |
---|
| 30 | * |
---|
| 31 | * this constructor is always called in a XML context (loading procedure) |
---|
| 32 | */ |
---|
| 33 | Campaign::Campaign ( TiXmlElement* root) |
---|
[2636] | 34 | { |
---|
[4597] | 35 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
---|
[2636] | 36 | this->isInit = false; |
---|
[4261] | 37 | |
---|
[5300] | 38 | PRINTF(4)("Loading Campaign...\n"); |
---|
[4010] | 39 | assert( root != NULL); |
---|
[4597] | 40 | |
---|
[4598] | 41 | this->loadParams(root); |
---|
[6374] | 42 | } |
---|
[4597] | 43 | |
---|
| 44 | |
---|
[6374] | 45 | /** |
---|
| 46 | * the campaign destructor |
---|
| 47 | */ |
---|
[4816] | 48 | Campaign::~Campaign () |
---|
| 49 | {} |
---|
[2636] | 50 | |
---|
| 51 | |
---|
[6368] | 52 | /** |
---|
| 53 | * initializes the class |
---|
| 54 | */ |
---|
[3222] | 55 | ErrorMessage Campaign::init() |
---|
[2636] | 56 | { |
---|
| 57 | this->isInit = true; |
---|
| 58 | } |
---|
| 59 | |
---|
[6368] | 60 | |
---|
[4598] | 61 | /** |
---|
[6368] | 62 | * loads the Parameters of a Campaign |
---|
| 63 | * @param root: The XML-element to load from |
---|
[4598] | 64 | */ |
---|
| 65 | void Campaign::loadParams(const TiXmlElement* root) |
---|
| 66 | { |
---|
[6376] | 67 | static_cast<StoryEntity*>(this)->loadParams(root); |
---|
[2636] | 68 | |
---|
[6374] | 69 | LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams) |
---|
| 70 | .describe("A List of Worlds to be loaded in this Campaign"); |
---|
[6376] | 71 | |
---|
| 72 | PRINTF(4)("Loaded Campaign specific stuff\n"); |
---|
[4598] | 73 | } |
---|
| 74 | |
---|
[6368] | 75 | |
---|
[4598] | 76 | /** |
---|
[6368] | 77 | * loads a WorldList |
---|
| 78 | * @param root: the XML-element to load from |
---|
[4598] | 79 | */ |
---|
| 80 | void Campaign::loadWorldListParams(const TiXmlElement* root) |
---|
| 81 | { |
---|
[6374] | 82 | if( root == NULL) |
---|
[5651] | 83 | return; |
---|
| 84 | |
---|
[5751] | 85 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[4598] | 86 | { |
---|
[5982] | 87 | StoryEntity* created = (StoryEntity*) Factory::fabricate(element); |
---|
[4598] | 88 | if( created != NULL) |
---|
| 89 | this->addEntity( created); |
---|
[6374] | 90 | PRINTF(3)("Campaign: Constructor: adding a world with name \"%s\" and id %i\n", |
---|
| 91 | created->getName(), created->getStoryID()); |
---|
[4598] | 92 | } |
---|
[5751] | 93 | LOAD_PARAM_END_CYCLE(element); |
---|
[6374] | 94 | |
---|
[4598] | 95 | } |
---|
| 96 | |
---|
[6374] | 97 | |
---|
[6368] | 98 | /** |
---|
| 99 | * starts the campaing |
---|
| 100 | */ |
---|
[3222] | 101 | ErrorMessage Campaign::start() |
---|
[2636] | 102 | { |
---|
| 103 | this->start(0); |
---|
| 104 | } |
---|
| 105 | |
---|
[6368] | 106 | |
---|
| 107 | /** |
---|
| 108 | * starts the campaing with a specific ID |
---|
| 109 | * @param storyID the id of the StoryEntity |
---|
| 110 | */ |
---|
[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) |
---|
[6374] | 120 | { |
---|
| 121 | PRINTF(0)("Campaign is starting StoryEntity nr:%i\n", se->getStoryID()); |
---|
[6370] | 122 | |
---|
[6374] | 123 | se->init(); |
---|
| 124 | se->loadData(); |
---|
[6370] | 125 | |
---|
[6374] | 126 | se->preStart(); |
---|
| 127 | se->start(); |
---|
[6152] | 128 | |
---|
[6370] | 129 | |
---|
[6374] | 130 | int nextWorldID = se->getNextStoryID(); |
---|
[4597] | 131 | |
---|
[6374] | 132 | this->entities.remove(se); |
---|
| 133 | delete se; |
---|
[5215] | 134 | |
---|
[6374] | 135 | se = this->getStoryEntity(nextWorldID); |
---|
| 136 | this->currentEntity = se; |
---|
| 137 | if( ( nextWorldID == WORLD_ID_GAMEEND) || ( se == NULL) ) |
---|
| 138 | { |
---|
| 139 | PRINTF(4)("Quitting campaing story loop\n"); |
---|
| 140 | if(se != NULL) |
---|
| 141 | delete se; |
---|
| 142 | return errorCode; |
---|
[4816] | 143 | } |
---|
[6374] | 144 | } |
---|
[4597] | 145 | |
---|
[6374] | 146 | /* clean up all world that have not been loaded and therefore are still in the world list - |
---|
| 147 | this probably does not belong into the start function. remove this later |
---|
| 148 | */ |
---|
| 149 | list<StoryEntity*>::iterator it; |
---|
| 150 | for(it = this->entities.begin(); it != entities.end(); it++) |
---|
| 151 | { |
---|
| 152 | delete (*it); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | PRINTF(2)("There is no StoryEnity left to play, quitting\n"); |
---|
[2636] | 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
[6368] | 159 | /** |
---|
| 160 | * pauses the campaing |
---|
| 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) |
---|
[6374] | 180 | { |
---|
| 181 | this->currentEntity->stop(); |
---|
| 182 | } |
---|
[3459] | 183 | } |
---|
| 184 | |
---|
[6371] | 185 | /* |
---|
[3459] | 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 | } |
---|
[6371] | 194 | }*/ |
---|
[3220] | 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 | { |
---|
[5773] | 214 | this->entities.push_back(se); |
---|
[3459] | 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 | { |
---|
[5773] | 227 | this->entities.remove(se); |
---|
[3459] | 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 | if( storyID == WORLD_ID_GAMEEND) |
---|
| 257 | return NULL; |
---|
[3608] | 258 | |
---|
[6374] | 259 | int id; |
---|
[5773] | 260 | list<StoryEntity*>::iterator it; |
---|
[6374] | 261 | for( it = this->entities.begin(); it != this->entities.end(); it++) |
---|
[5773] | 262 | { |
---|
[6374] | 263 | id = (*it)->getStoryID(); |
---|
| 264 | if( id == storyID) |
---|
| 265 | return (*it); |
---|
| 266 | } |
---|
[3608] | 267 | |
---|
[2636] | 268 | return NULL; |
---|
| 269 | } |
---|