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