[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 | co-programmer: ... |
---|
| 16 | */ |
---|
| 17 | |
---|
[5300] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
| 19 | |
---|
[2636] | 20 | #include "game_loader.h" |
---|
[7193] | 21 | #include "util/loading/load_param.h" |
---|
[5139] | 22 | |
---|
[9727] | 23 | #include "debug.h" |
---|
[2636] | 24 | #include "campaign.h" |
---|
[6152] | 25 | |
---|
[7193] | 26 | #include "util/loading/resource_manager.h" |
---|
[6152] | 27 | |
---|
[7868] | 28 | #include "key_mapper.h" |
---|
[2636] | 29 | |
---|
[9715] | 30 | ObjectListDefinition(GameLoader); |
---|
[2636] | 31 | |
---|
[7868] | 32 | GameLoader* GameLoader::singletonRef = NULL; |
---|
[5162] | 33 | |
---|
| 34 | |
---|
[7868] | 35 | /** |
---|
| 36 | * simple constructor |
---|
| 37 | */ |
---|
| 38 | GameLoader::GameLoader () |
---|
| 39 | { |
---|
[9684] | 40 | this->registerObject(this, GameLoader::_objectList); |
---|
[7868] | 41 | this->setName("GameLoader"); |
---|
| 42 | this->bRun = true; |
---|
| 43 | } |
---|
[5162] | 44 | |
---|
[2636] | 45 | |
---|
[7868] | 46 | /** |
---|
| 47 | * simple deconstructor |
---|
| 48 | */ |
---|
| 49 | GameLoader::~GameLoader () |
---|
| 50 | { |
---|
| 51 | if( this->currentCampaign) |
---|
| 52 | delete this->currentCampaign; |
---|
| 53 | this->currentCampaign = NULL; |
---|
[9684] | 54 | |
---|
[9110] | 55 | GameLoader::singletonRef = NULL; |
---|
[7868] | 56 | } |
---|
[2636] | 57 | |
---|
| 58 | |
---|
[7868] | 59 | /** |
---|
| 60 | * initializes the GameLoader |
---|
| 61 | */ |
---|
| 62 | ErrorMessage GameLoader::init() |
---|
| 63 | { |
---|
| 64 | if(this->currentCampaign != NULL) |
---|
| 65 | this->currentCampaign->init(); |
---|
[2636] | 66 | |
---|
[7868] | 67 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_PAUSE); |
---|
| 68 | this->subscribeEvent(ES_ALL, EV_MAIN_QUIT); //< External Quit Event |
---|
| 69 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_QUIT); |
---|
| 70 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_NEXT_WORLD); |
---|
| 71 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD); |
---|
[8717] | 72 | |
---|
| 73 | return ErrorMessage(); |
---|
[7868] | 74 | } |
---|
[4411] | 75 | |
---|
[2636] | 76 | |
---|
[7868] | 77 | /** |
---|
| 78 | * reads a campaign definition file into a campaign class |
---|
| 79 | * @param fileName to be loaded |
---|
| 80 | * @returns the loaded campaign |
---|
| 81 | * |
---|
| 82 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 83 | */ |
---|
| 84 | ErrorMessage GameLoader::loadCampaign(const std::string& fileName) |
---|
| 85 | { |
---|
| 86 | ErrorMessage errorCode; |
---|
| 87 | std::string campaignName = ResourceManager::getFullName(fileName); |
---|
| 88 | if (!campaignName.empty()) |
---|
[7374] | 89 | { |
---|
[7868] | 90 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
[7374] | 91 | } |
---|
[8717] | 92 | |
---|
| 93 | return ErrorMessage(); |
---|
[7868] | 94 | } |
---|
[2636] | 95 | |
---|
[6139] | 96 | |
---|
[7868] | 97 | /** |
---|
| 98 | * reads a campaign definition file into a campaign class |
---|
| 99 | * @param fileName to be loaded |
---|
| 100 | * @returns the loaded campaign |
---|
| 101 | * |
---|
| 102 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 103 | */ |
---|
| 104 | ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName) |
---|
| 105 | { |
---|
| 106 | ErrorMessage errorCode; |
---|
| 107 | std::string campaignName = ResourceManager::getFullName(fileName); |
---|
| 108 | if (!campaignName.empty()) |
---|
[6139] | 109 | { |
---|
[7868] | 110 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
[6139] | 111 | } |
---|
[8717] | 112 | |
---|
| 113 | return ErrorMessage(); |
---|
[7868] | 114 | } |
---|
[6139] | 115 | |
---|
| 116 | |
---|
[7868] | 117 | /** |
---|
| 118 | * loads a debug campaign for test purposes only. |
---|
| 119 | * @param campaignID the identifier of the campaign. |
---|
| 120 | * @returns error message if not able to do so. |
---|
| 121 | */ |
---|
| 122 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
| 123 | { |
---|
| 124 | switch(campaignID) |
---|
[7374] | 125 | { |
---|
[7868] | 126 | /* |
---|
| 127 | Debug Level 0: Debug level used to test the base frame work. |
---|
| 128 | As you can see, all storyentity data is allocated before game |
---|
| 129 | start. the storyentity will load themselfs shortly before start |
---|
| 130 | through the StoryEntity::init() funtion. |
---|
| 131 | */ |
---|
| 132 | case DEBUG_CAMPAIGN_0: |
---|
| 133 | { |
---|
| 134 | /* Campaign* debugCampaign = new Campaign(); |
---|
[3220] | 135 | |
---|
[7868] | 136 | World* world0 = new World(DEBUG_WORLD_0); |
---|
| 137 | world0->setNextStoryID(WORLD_ID_1); |
---|
| 138 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
[3220] | 139 | |
---|
[7868] | 140 | World* world1 = new World(DEBUG_WORLD_1); |
---|
| 141 | world1->setNextStoryID(WORLD_ID_2); |
---|
| 142 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
[2636] | 143 | |
---|
[7868] | 144 | World* world2 = new World(DEBUG_WORLD_2); |
---|
| 145 | world2->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 146 | debugCampaign->addEntity(world2, WORLD_ID_2); |
---|
[3727] | 147 | |
---|
[7868] | 148 | this->currentCampaign = debugCampaign; |
---|
| 149 | break;*/ |
---|
| 150 | } |
---|
[7374] | 151 | } |
---|
[8717] | 152 | |
---|
| 153 | return ErrorMessage(); |
---|
[7868] | 154 | } |
---|
[2636] | 155 | |
---|
[4443] | 156 | |
---|
[7868] | 157 | /** |
---|
| 158 | * starts the current entity |
---|
| 159 | * @returns error code if this action has caused a error |
---|
| 160 | */ |
---|
| 161 | ErrorMessage GameLoader::start() |
---|
| 162 | { |
---|
| 163 | if(this->currentCampaign != NULL) |
---|
[6862] | 164 | { |
---|
[7868] | 165 | this->currentCampaign->start(); |
---|
[6862] | 166 | } |
---|
[8717] | 167 | |
---|
| 168 | return ErrorMessage(); |
---|
[7868] | 169 | } |
---|
[2636] | 170 | |
---|
| 171 | |
---|
[7868] | 172 | /** |
---|
| 173 | * stops the current entity |
---|
| 174 | * @returns error code if this action has caused a error |
---|
| 175 | * |
---|
| 176 | * ATTENTION: this function shouldn't call other functions, or if so, they must return |
---|
| 177 | * after finishing. If you ignore or forget to do so, the current entity is not able to |
---|
| 178 | * terminate and it will run in the background or the ressources can't be freed or even |
---|
| 179 | * worse: are freed and the program will end in a segmentation fault! |
---|
| 180 | * hehehe, have ya seen it... :) |
---|
| 181 | */ |
---|
| 182 | void GameLoader::stop() |
---|
| 183 | { |
---|
| 184 | if(this->currentCampaign != NULL) |
---|
| 185 | this->currentCampaign->stop(); |
---|
| 186 | } |
---|
[2636] | 187 | |
---|
| 188 | |
---|
[7868] | 189 | /** |
---|
| 190 | * pause the current entity |
---|
| 191 | * @returns error code if this action has caused a error |
---|
| 192 | * |
---|
| 193 | * this pauses the current entity or passes this call forth to the running entity. |
---|
| 194 | */ |
---|
| 195 | ErrorMessage GameLoader::pause() |
---|
| 196 | { |
---|
| 197 | this->isPaused = true; |
---|
| 198 | if(this->currentCampaign != NULL) |
---|
| 199 | this->currentCampaign->pause(); |
---|
[8717] | 200 | |
---|
| 201 | return ErrorMessage(); |
---|
[7868] | 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * resumes a pause |
---|
| 207 | * @returns error code if this action has caused a error |
---|
| 208 | * |
---|
| 209 | * this resumess the current entity or passes this call forth to the running entity. |
---|
| 210 | */ |
---|
| 211 | ErrorMessage GameLoader::resume() |
---|
| 212 | { |
---|
| 213 | this->isPaused = false; |
---|
| 214 | if(this->currentCampaign != NULL) |
---|
| 215 | this->currentCampaign->resume(); |
---|
[8717] | 216 | |
---|
| 217 | return ErrorMessage(); |
---|
[7868] | 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * reads a campaign definition file into a campaign class |
---|
| 223 | * @param fileName to be loaded |
---|
| 224 | * @returns the loaded campaign |
---|
| 225 | * |
---|
| 226 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 227 | */ |
---|
| 228 | Campaign* GameLoader::fileToCampaign(const std::string& fileName) |
---|
| 229 | { |
---|
| 230 | /* do not entirely load the campaign. just the current world |
---|
| 231 | before start of each world, it has to be initialized so it |
---|
| 232 | can load everything it needs into memory then. |
---|
| 233 | */ |
---|
| 234 | |
---|
| 235 | if( fileName.empty()) |
---|
[7374] | 236 | { |
---|
[7868] | 237 | PRINTF(2)("No filename specified for loading"); |
---|
| 238 | return NULL; |
---|
[7374] | 239 | } |
---|
[2636] | 240 | |
---|
[7868] | 241 | TiXmlDocument XMLDoc(fileName); |
---|
| 242 | // load the campaign document |
---|
| 243 | if( !XMLDoc.LoadFile(fileName)) |
---|
[7374] | 244 | { |
---|
[7868] | 245 | // report an error |
---|
| 246 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName.c_str(), XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol()); |
---|
| 247 | return NULL; |
---|
[7374] | 248 | } |
---|
[2636] | 249 | |
---|
[7868] | 250 | // check basic validity |
---|
| 251 | TiXmlElement* root = XMLDoc.RootElement(); |
---|
| 252 | assert( root != NULL); |
---|
[4443] | 253 | |
---|
[7868] | 254 | if( strcmp( root->Value(), "Campaign")) |
---|
[7374] | 255 | { |
---|
[7868] | 256 | // report an error |
---|
| 257 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
| 258 | return NULL; |
---|
| 259 | } |
---|
[4597] | 260 | |
---|
[7868] | 261 | // construct campaign |
---|
| 262 | return new Campaign( root); |
---|
| 263 | } |
---|
[4597] | 264 | |
---|
| 265 | |
---|
| 266 | |
---|
[7868] | 267 | /** |
---|
| 268 | * handle keyboard commands |
---|
| 269 | * @param event the event to handle |
---|
| 270 | */ |
---|
| 271 | void GameLoader::process(const Event& event) |
---|
| 272 | { |
---|
| 273 | if( event.type == KeyMapper::PEV_NEXT_WORLD) |
---|
| 274 | { |
---|
| 275 | if( likely(event.bPressed)) |
---|
[4010] | 276 | { |
---|
[7868] | 277 | this->switchToNextLevel(); |
---|
[4010] | 278 | } |
---|
[7374] | 279 | } |
---|
[7868] | 280 | else if( event.type == KeyMapper::PEV_PAUSE) |
---|
[5553] | 281 | { |
---|
[7868] | 282 | if( likely(event.bPressed)) |
---|
[4410] | 283 | { |
---|
[7868] | 284 | if(this->isPaused) |
---|
| 285 | this->resume(); |
---|
| 286 | else |
---|
| 287 | this->pause(); |
---|
[4410] | 288 | } |
---|
[5553] | 289 | } |
---|
[7868] | 290 | else if( event.type == KeyMapper::PEV_QUIT) |
---|
[5553] | 291 | { |
---|
[7868] | 292 | if( event.bPressed) this->stop(); |
---|
[5553] | 293 | } |
---|
[7868] | 294 | else if (event.type == EV_MAIN_QUIT) |
---|
| 295 | this->stop(); |
---|
| 296 | } |
---|
[4410] | 297 | |
---|
[7868] | 298 | |
---|
| 299 | /** |
---|
| 300 | * this changes to the next level |
---|
| 301 | */ |
---|
| 302 | void GameLoader::switchToNextLevel() |
---|
| 303 | { |
---|
| 304 | if(this->currentCampaign != NULL) |
---|
| 305 | this->currentCampaign->switchToNextLevel(); |
---|
[2636] | 306 | } |
---|
[7868] | 307 | |
---|