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