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