[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" |
---|
[5139] | 21 | |
---|
| 22 | #include "shell_command.h" |
---|
[2636] | 23 | #include "campaign.h" |
---|
| 24 | #include "world.h" |
---|
| 25 | #include "orxonox.h" |
---|
| 26 | #include "camera.h" |
---|
| 27 | #include "vector.h" |
---|
[4094] | 28 | #include "resource_manager.h" |
---|
[4010] | 29 | #include "factory.h" |
---|
[4410] | 30 | #include "event.h" |
---|
| 31 | #include "event_handler.h" |
---|
[2636] | 32 | #include <string.h> |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | using namespace std; |
---|
| 36 | |
---|
| 37 | |
---|
[5195] | 38 | SHELL_COMMAND(quit, GameLoader, stop) |
---|
| 39 | ->describe("quits the game") |
---|
| 40 | ->setAlias("orxoquit"); |
---|
[2636] | 41 | |
---|
[5162] | 42 | |
---|
| 43 | GameLoader* GameLoader::singletonRef = NULL; |
---|
| 44 | |
---|
| 45 | |
---|
[4445] | 46 | /** |
---|
[4836] | 47 | * simple constructor |
---|
[6063] | 48 | */ |
---|
[4597] | 49 | GameLoader::GameLoader () |
---|
[4017] | 50 | { |
---|
[4597] | 51 | this->setClassID(CL_GAME_LOADER, "GameLoader"); |
---|
| 52 | this->setName("GameLoader"); |
---|
[5139] | 53 | |
---|
[4017] | 54 | } |
---|
[2636] | 55 | |
---|
| 56 | |
---|
[4445] | 57 | /** |
---|
[4836] | 58 | * simple deconstructor |
---|
[6063] | 59 | */ |
---|
[4816] | 60 | GameLoader::~GameLoader () |
---|
| 61 | { |
---|
| 62 | if( this->currentCampaign) |
---|
| 63 | delete this->currentCampaign; |
---|
| 64 | this->currentCampaign = NULL; |
---|
| 65 | } |
---|
[2636] | 66 | |
---|
| 67 | |
---|
[3225] | 68 | /** |
---|
[4836] | 69 | * this class is a singleton class |
---|
| 70 | * @returns an instance of itself |
---|
[6063] | 71 | * |
---|
| 72 | * if you are unsure about singleton classes, check the theory out on the internet :) |
---|
| 73 | */ |
---|
[2636] | 74 | GameLoader* GameLoader::getInstance() |
---|
| 75 | { |
---|
| 76 | if(singletonRef == NULL) |
---|
| 77 | singletonRef = new GameLoader(); |
---|
| 78 | return singletonRef; |
---|
| 79 | } |
---|
| 80 | |
---|
[4487] | 81 | /** |
---|
[4836] | 82 | * initializes the GameLoader |
---|
[6063] | 83 | */ |
---|
[3222] | 84 | ErrorMessage GameLoader::init() |
---|
[2636] | 85 | { |
---|
| 86 | if(this->currentCampaign != NULL) |
---|
| 87 | this->currentCampaign->init(); |
---|
[4411] | 88 | |
---|
| 89 | this->eventHandler = EventHandler::getInstance(); |
---|
[5093] | 90 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE); |
---|
[5553] | 91 | this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT); //< External Quit Event |
---|
[4411] | 92 | this->eventHandler->subscribe(this, ES_ALL, KeyMapper::PEV_QUIT); |
---|
[5093] | 93 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD); |
---|
| 94 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD); |
---|
[2636] | 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[3225] | 98 | /** |
---|
[4836] | 99 | * reads a campaign definition file into a campaign class |
---|
| 100 | * @param fileName to be loaded |
---|
| 101 | * @returns the loaded campaign |
---|
[6063] | 102 | * |
---|
| 103 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 104 | */ |
---|
[4487] | 105 | ErrorMessage GameLoader::loadCampaign(const char* fileName) |
---|
[2636] | 106 | { |
---|
[3222] | 107 | ErrorMessage errorCode; |
---|
[4487] | 108 | char* campaignName = ResourceManager::getFullName(fileName); |
---|
[4216] | 109 | if (campaignName) |
---|
[4094] | 110 | { |
---|
| 111 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
[5208] | 112 | delete[] campaignName; |
---|
[4094] | 113 | } |
---|
[4816] | 114 | // World* world0 = new World(DEBUG_WORLD_0); |
---|
| 115 | // world0->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 116 | // this->currentCampaign->addEntity(world0, WORLD_ID_2); |
---|
[2636] | 117 | } |
---|
| 118 | |
---|
[6000] | 119 | |
---|
[3225] | 120 | /** |
---|
[6000] | 121 | * reads a campaign definition file into a campaign class |
---|
| 122 | * @param fileName to be loaded |
---|
| 123 | * @returns the loaded campaign |
---|
[6063] | 124 | * |
---|
| 125 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
[6000] | 126 | */ |
---|
[6063] | 127 | ErrorMessage GameLoader::loadNetworkCampaign(const char* fileName, int nodeState) |
---|
[6000] | 128 | { |
---|
| 129 | ErrorMessage errorCode; |
---|
| 130 | char* campaignName = ResourceManager::getFullName(fileName); |
---|
| 131 | if (campaignName) |
---|
| 132 | { |
---|
[6063] | 133 | this->currentCampaign = this->fileToNetworkCampaign(campaignName, nodeState); |
---|
[6000] | 134 | delete[] campaignName; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /** |
---|
[4836] | 140 | * loads a debug campaign for test purposes only. |
---|
| 141 | * @param campaignID the identifier of the campaign. |
---|
| 142 | * @returns error message if not able to do so. |
---|
[6063] | 143 | */ |
---|
[3222] | 144 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
[2636] | 145 | { |
---|
| 146 | switch(campaignID) |
---|
| 147 | { |
---|
[4597] | 148 | /* |
---|
| 149 | Debug Level 0: Debug level used to test the base frame work. |
---|
| 150 | As you can see, all storyentity data is allocated before game |
---|
| 151 | start. the storyentity will load themselfs shortly before start |
---|
| 152 | through the StoryEntity::init() funtion. |
---|
[3629] | 153 | */ |
---|
[2636] | 154 | case DEBUG_CAMPAIGN_0: |
---|
| 155 | { |
---|
[4597] | 156 | Campaign* debugCampaign = new Campaign(); |
---|
[3220] | 157 | |
---|
[4597] | 158 | World* world0 = new World(DEBUG_WORLD_0); |
---|
| 159 | world0->setNextStoryID(WORLD_ID_1); |
---|
| 160 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
[3220] | 161 | |
---|
[4597] | 162 | World* world1 = new World(DEBUG_WORLD_1); |
---|
| 163 | world1->setNextStoryID(WORLD_ID_2); |
---|
| 164 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
[2636] | 165 | |
---|
[4597] | 166 | World* world2 = new World(DEBUG_WORLD_2); |
---|
| 167 | world2->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 168 | debugCampaign->addEntity(world2, WORLD_ID_2); |
---|
[3727] | 169 | |
---|
[4597] | 170 | this->currentCampaign = debugCampaign; |
---|
| 171 | break; |
---|
[2636] | 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
[4443] | 176 | |
---|
[4597] | 177 | /** |
---|
[6063] | 178 | * starts the current entity |
---|
| 179 | * @returns error code if this action has caused a error |
---|
| 180 | */ |
---|
[3222] | 181 | ErrorMessage GameLoader::start() |
---|
[2636] | 182 | { |
---|
| 183 | if(this->currentCampaign != NULL) |
---|
| 184 | this->currentCampaign->start(); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
[4597] | 188 | /** |
---|
[6063] | 189 | * stops the current entity |
---|
| 190 | * @returns error code if this action has caused a error |
---|
| 191 | * |
---|
| 192 | * ATTENTION: this function shouldn't call other functions, or if so, they must return |
---|
| 193 | * after finishing. If you ignore or forget to do so, the current entity is not able to |
---|
| 194 | * terminate and it will run in the background or the ressources can't be freed or even |
---|
| 195 | * worse: are freed and the program will end in a segmentation fault! |
---|
| 196 | * hehehe, have ya seen it... :) |
---|
| 197 | */ |
---|
[5139] | 198 | void GameLoader::stop() |
---|
[2636] | 199 | { |
---|
| 200 | if(this->currentCampaign != NULL) |
---|
| 201 | this->currentCampaign->stop(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
[4597] | 205 | /** |
---|
[6063] | 206 | * pause the current entity |
---|
| 207 | * @returns error code if this action has caused a error |
---|
| 208 | * |
---|
| 209 | * this pauses the current entity or passes this call forth to the running entity. |
---|
| 210 | */ |
---|
[3222] | 211 | ErrorMessage GameLoader::pause() |
---|
[2636] | 212 | { |
---|
| 213 | this->isPaused = true; |
---|
| 214 | if(this->currentCampaign != NULL) |
---|
| 215 | this->currentCampaign->pause(); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
[4597] | 219 | /** |
---|
[6063] | 220 | * resumes a pause |
---|
| 221 | * @returns error code if this action has caused a error |
---|
| 222 | * |
---|
| 223 | * this resumess the current entity or passes this call forth to the running entity. |
---|
| 224 | */ |
---|
[3222] | 225 | ErrorMessage GameLoader::resume() |
---|
[2636] | 226 | { |
---|
| 227 | this->isPaused = false; |
---|
| 228 | if(this->currentCampaign != NULL) |
---|
| 229 | this->currentCampaign->resume(); |
---|
| 230 | } |
---|
| 231 | |
---|
[4443] | 232 | |
---|
[3225] | 233 | /** |
---|
[4836] | 234 | * release the mem ATTENTION: not implemented |
---|
[3225] | 235 | */ |
---|
| 236 | ErrorMessage GameLoader::destroy() |
---|
[4487] | 237 | { |
---|
[2636] | 238 | |
---|
[4487] | 239 | } |
---|
[2636] | 240 | |
---|
[4487] | 241 | |
---|
[3225] | 242 | /** |
---|
[4836] | 243 | * reads a campaign definition file into a campaign class |
---|
| 244 | * @param fileName to be loaded |
---|
| 245 | * @returns the loaded campaign |
---|
[6063] | 246 | * |
---|
| 247 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 248 | */ |
---|
[4487] | 249 | Campaign* GameLoader::fileToCampaign(const char* fileName) |
---|
[2636] | 250 | { |
---|
| 251 | /* do not entirely load the campaign. just the current world |
---|
| 252 | before start of each world, it has to be initialized so it |
---|
| 253 | can load everything it needs into memory then. |
---|
| 254 | */ |
---|
[4597] | 255 | |
---|
[4487] | 256 | if( fileName == NULL) |
---|
[4010] | 257 | { |
---|
[4113] | 258 | PRINTF(2)("No filename specified for loading"); |
---|
[4010] | 259 | return NULL; |
---|
| 260 | } |
---|
[4597] | 261 | |
---|
[4487] | 262 | TiXmlDocument* XMLDoc = new TiXmlDocument( fileName); |
---|
[4010] | 263 | // load the campaign document |
---|
| 264 | if( !XMLDoc->LoadFile()) |
---|
| 265 | { |
---|
| 266 | // report an error |
---|
[4487] | 267 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
[4010] | 268 | delete XMLDoc; |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
[4597] | 271 | |
---|
[4010] | 272 | // check basic validity |
---|
| 273 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
| 274 | assert( root != NULL); |
---|
[4597] | 275 | |
---|
[4010] | 276 | if( strcmp( root->Value(), "Campaign")) |
---|
| 277 | { |
---|
| 278 | // report an error |
---|
[4113] | 279 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
[4010] | 280 | delete XMLDoc; |
---|
| 281 | return NULL; |
---|
| 282 | } |
---|
[4597] | 283 | |
---|
[4010] | 284 | // construct campaign |
---|
| 285 | Campaign* c = new Campaign( root); |
---|
[4597] | 286 | |
---|
[4010] | 287 | // free the XML data |
---|
| 288 | delete XMLDoc; |
---|
[4496] | 289 | |
---|
[4010] | 290 | return c; |
---|
[2636] | 291 | } |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | /** |
---|
[6000] | 295 | * reads a campaign definition file into a campaign class |
---|
| 296 | * @param fileName to be loaded |
---|
| 297 | * @returns the loaded campaign |
---|
[6063] | 298 | * |
---|
| 299 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
[6000] | 300 | */ |
---|
[6063] | 301 | Campaign* GameLoader::fileToNetworkCampaign(const char* fileName, int nodeState) |
---|
[6000] | 302 | { |
---|
| 303 | /* do not entirely load the campaign. just the current world |
---|
| 304 | before start of each world, it has to be initialized so it |
---|
| 305 | can load everything it needs into memory then. |
---|
| 306 | */ |
---|
| 307 | |
---|
| 308 | if( fileName == NULL) |
---|
| 309 | { |
---|
| 310 | PRINTF(2)("No filename specified for loading"); |
---|
| 311 | return NULL; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | TiXmlDocument* XMLDoc = new TiXmlDocument( fileName); |
---|
| 315 | // load the campaign document |
---|
| 316 | if( !XMLDoc->LoadFile()) |
---|
| 317 | { |
---|
| 318 | // report an error |
---|
| 319 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
| 320 | delete XMLDoc; |
---|
| 321 | return NULL; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | // check basic validity |
---|
| 325 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
| 326 | assert( root != NULL); |
---|
| 327 | |
---|
| 328 | if( strcmp( root->Value(), "Campaign")) |
---|
| 329 | { |
---|
| 330 | // report an error |
---|
| 331 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
| 332 | delete XMLDoc; |
---|
| 333 | return NULL; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | // construct campaign |
---|
| 337 | Campaign* c = new Campaign( root); |
---|
| 338 | |
---|
| 339 | // free the XML data |
---|
| 340 | delete XMLDoc; |
---|
| 341 | |
---|
| 342 | return c; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | |
---|
| 346 | /** |
---|
[4836] | 347 | * handle keyboard commands |
---|
| 348 | * @param event the event to handle |
---|
[5553] | 349 | */ |
---|
[4410] | 350 | void GameLoader::process(const Event& event) |
---|
| 351 | { |
---|
| 352 | if( event.type == KeyMapper::PEV_NEXT_WORLD) |
---|
[5553] | 353 | { |
---|
| 354 | if( likely(event.bPressed)) |
---|
[4410] | 355 | { |
---|
[5553] | 356 | this->nextLevel(); |
---|
[4410] | 357 | } |
---|
[5553] | 358 | } |
---|
[4410] | 359 | else if( event.type == KeyMapper::PEV_PREVIOUS_WORLD) |
---|
[5553] | 360 | { |
---|
| 361 | if( likely(event.bPressed)) |
---|
[4410] | 362 | { |
---|
[5553] | 363 | this->previousLevel(); |
---|
[4410] | 364 | } |
---|
[5553] | 365 | } |
---|
[4410] | 366 | else if( event.type == KeyMapper::PEV_PAUSE) |
---|
[5553] | 367 | { |
---|
| 368 | if( likely(event.bPressed)) |
---|
[4410] | 369 | { |
---|
[5553] | 370 | if(this->isPaused) |
---|
| 371 | this->resume(); |
---|
| 372 | else |
---|
| 373 | this->pause(); |
---|
[4410] | 374 | } |
---|
[5553] | 375 | } |
---|
[4410] | 376 | else if( event.type == KeyMapper::PEV_QUIT) |
---|
[5553] | 377 | { |
---|
| 378 | if( event.bPressed) this->stop(); |
---|
| 379 | } |
---|
| 380 | else if (event.type == EV_MAIN_QUIT) |
---|
| 381 | this->stop(); |
---|
[4410] | 382 | } |
---|
| 383 | |
---|
[4443] | 384 | |
---|
[4487] | 385 | /** |
---|
[6063] | 386 | * \brief this changes to the next level |
---|
| 387 | */ |
---|
[2636] | 388 | void GameLoader::nextLevel() |
---|
| 389 | { |
---|
| 390 | if(this->currentCampaign != NULL) |
---|
| 391 | this->currentCampaign->nextLevel(); |
---|
| 392 | } |
---|
| 393 | |
---|
[3225] | 394 | |
---|
[4487] | 395 | /** |
---|
[6063] | 396 | * change to the previous level - not implemented |
---|
| 397 | * |
---|
| 398 | * this propably useless |
---|
| 399 | */ |
---|
[2636] | 400 | void GameLoader::previousLevel() |
---|
| 401 | { |
---|
| 402 | if(this->currentCampaign != NULL) |
---|
| 403 | this->currentCampaign->previousLevel(); |
---|
| 404 | } |
---|