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