[2636] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 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 | |
---|
| 18 | |
---|
| 19 | #include "game_loader.h" |
---|
| 20 | #include "campaign.h" |
---|
| 21 | #include "world.h" |
---|
| 22 | #include "player.h" |
---|
| 23 | #include "orxonox.h" |
---|
| 24 | #include "camera.h" |
---|
| 25 | #include "command_node.h" |
---|
| 26 | #include "vector.h" |
---|
[4094] | 27 | #include "resource_manager.h" |
---|
[4010] | 28 | #include "factory.h" |
---|
[2636] | 29 | |
---|
| 30 | #include <string.h> |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | GameLoader* GameLoader::singletonRef = 0; |
---|
| 37 | |
---|
| 38 | |
---|
[4017] | 39 | GameLoader::GameLoader () |
---|
| 40 | { |
---|
| 41 | first = NULL; |
---|
| 42 | } |
---|
[2636] | 43 | |
---|
| 44 | |
---|
| 45 | GameLoader::~GameLoader () {} |
---|
| 46 | |
---|
| 47 | |
---|
[3225] | 48 | /** |
---|
| 49 | \brief this class is a singleton class |
---|
| 50 | \returns an instance of itself |
---|
| 51 | |
---|
| 52 | if you are unsure about singleton classes, check the theory out on the internet :) |
---|
| 53 | */ |
---|
[2636] | 54 | GameLoader* GameLoader::getInstance() |
---|
| 55 | { |
---|
| 56 | if(singletonRef == NULL) |
---|
| 57 | singletonRef = new GameLoader(); |
---|
| 58 | return singletonRef; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
[3222] | 62 | ErrorMessage GameLoader::init() |
---|
[2636] | 63 | { |
---|
| 64 | if(this->currentCampaign != NULL) |
---|
| 65 | this->currentCampaign->init(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
[3225] | 69 | /** |
---|
| 70 | \brief reads a campaign definition file into a campaign class |
---|
| 71 | \param filename to be loaded |
---|
| 72 | \returns the loaded campaign |
---|
| 73 | |
---|
| 74 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 75 | */ |
---|
[4216] | 76 | ErrorMessage GameLoader::loadCampaign(const char* name) |
---|
[2636] | 77 | { |
---|
[3222] | 78 | ErrorMessage errorCode; |
---|
[4216] | 79 | char* campaignName = ResourceManager::getFullName(name); |
---|
| 80 | if (campaignName) |
---|
[4094] | 81 | { |
---|
| 82 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
| 83 | delete campaignName; |
---|
| 84 | } |
---|
[2636] | 85 | } |
---|
| 86 | |
---|
[3225] | 87 | |
---|
| 88 | /** |
---|
| 89 | \brief loads a debug campaign for test purposes only. |
---|
| 90 | \param the identifier of the campaign. |
---|
| 91 | \returns error message if not able to do so. |
---|
| 92 | */ |
---|
[3222] | 93 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
[2636] | 94 | { |
---|
| 95 | switch(campaignID) |
---|
| 96 | { |
---|
[3629] | 97 | /* |
---|
| 98 | Debug Level 0: Debug level used to test the base frame work. |
---|
| 99 | As you can see, all storyentity data is allocated before game |
---|
| 100 | start. the storyentity will load themselfs shortly before start |
---|
| 101 | through the StoryEntity::init() funtion. |
---|
| 102 | */ |
---|
[2636] | 103 | case DEBUG_CAMPAIGN_0: |
---|
| 104 | { |
---|
| 105 | Campaign* debugCampaign = new Campaign(); |
---|
[3220] | 106 | |
---|
[2636] | 107 | World* world0 = new World(DEBUG_WORLD_0); |
---|
[3220] | 108 | world0->setNextStoryID(WORLD_ID_1); |
---|
[2636] | 109 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
[3220] | 110 | |
---|
[2636] | 111 | World* world1 = new World(DEBUG_WORLD_1); |
---|
[3727] | 112 | world1->setNextStoryID(WORLD_ID_2); |
---|
[2636] | 113 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
| 114 | |
---|
[3727] | 115 | World* world2 = new World(DEBUG_WORLD_2); |
---|
| 116 | world2->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 117 | debugCampaign->addEntity(world2, WORLD_ID_2); |
---|
| 118 | |
---|
[2636] | 119 | this->currentCampaign = debugCampaign; |
---|
| 120 | break; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[3222] | 125 | ErrorMessage GameLoader::start() |
---|
[2636] | 126 | { |
---|
| 127 | if(this->currentCampaign != NULL) |
---|
| 128 | this->currentCampaign->start(); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
[3222] | 132 | ErrorMessage GameLoader::stop() |
---|
[2636] | 133 | { |
---|
| 134 | if(this->currentCampaign != NULL) |
---|
| 135 | this->currentCampaign->stop(); |
---|
| 136 | this->currentCampaign = NULL; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[3222] | 140 | ErrorMessage GameLoader::pause() |
---|
[2636] | 141 | { |
---|
| 142 | this->isPaused = true; |
---|
| 143 | if(this->currentCampaign != NULL) |
---|
| 144 | this->currentCampaign->pause(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[3222] | 148 | ErrorMessage GameLoader::resume() |
---|
[2636] | 149 | { |
---|
| 150 | this->isPaused = false; |
---|
| 151 | if(this->currentCampaign != NULL) |
---|
| 152 | this->currentCampaign->resume(); |
---|
| 153 | } |
---|
| 154 | |
---|
[3225] | 155 | /** |
---|
| 156 | \brief release the mem |
---|
| 157 | */ |
---|
| 158 | ErrorMessage GameLoader::destroy() |
---|
[2636] | 159 | {} |
---|
| 160 | |
---|
| 161 | |
---|
[3225] | 162 | /** |
---|
| 163 | \brief reads a campaign definition file into a campaign class |
---|
| 164 | \param filename to be loaded |
---|
| 165 | \returns the loaded campaign |
---|
| 166 | |
---|
| 167 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 168 | */ |
---|
[4113] | 169 | Campaign* GameLoader::fileToCampaign(const char *name) |
---|
[2636] | 170 | { |
---|
| 171 | /* do not entirely load the campaign. just the current world |
---|
| 172 | before start of each world, it has to be initialized so it |
---|
| 173 | can load everything it needs into memory then. |
---|
| 174 | */ |
---|
[4010] | 175 | |
---|
| 176 | if( name == NULL) |
---|
| 177 | { |
---|
[4113] | 178 | PRINTF(2)("No filename specified for loading"); |
---|
[4010] | 179 | return NULL; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | TiXmlDocument* XMLDoc = new TiXmlDocument( name); |
---|
| 183 | // load the campaign document |
---|
| 184 | if( !XMLDoc->LoadFile()) |
---|
| 185 | { |
---|
| 186 | // report an error |
---|
[4113] | 187 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", name, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
[4010] | 188 | delete XMLDoc; |
---|
| 189 | return NULL; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | // check basic validity |
---|
| 193 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
| 194 | assert( root != NULL); |
---|
| 195 | |
---|
| 196 | if( strcmp( root->Value(), "Campaign")) |
---|
| 197 | { |
---|
| 198 | // report an error |
---|
[4113] | 199 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
[4010] | 200 | delete XMLDoc; |
---|
| 201 | return NULL; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // construct campaign |
---|
| 205 | Campaign* c = new Campaign( root); |
---|
| 206 | |
---|
| 207 | // free the XML data |
---|
| 208 | delete XMLDoc; |
---|
| 209 | |
---|
| 210 | return c; |
---|
[2636] | 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | \brief handle keyboard commands |
---|
| 216 | \param cmd: the command to handle |
---|
[3225] | 217 | \returns true if the command was handled by the system |
---|
[2636] | 218 | */ |
---|
| 219 | bool GameLoader::worldCommand (Command* cmd) |
---|
| 220 | { |
---|
[4091] | 221 | if( !strcmp( cmd->cmd, CONFIG_NAME_NEXT_WORLD)) |
---|
[2636] | 222 | { |
---|
| 223 | if( !cmd->bUp) |
---|
| 224 | { |
---|
| 225 | this->nextLevel(); |
---|
| 226 | } |
---|
| 227 | return true; |
---|
| 228 | } |
---|
[4091] | 229 | else if( !strcmp( cmd->cmd, CONFIG_NAME_PREV_WORLD)) |
---|
[2636] | 230 | { |
---|
| 231 | if( !cmd->bUp) |
---|
| 232 | { |
---|
| 233 | this->previousLevel(); |
---|
| 234 | } |
---|
| 235 | return true; |
---|
| 236 | } |
---|
[4091] | 237 | else if( !strcmp( cmd->cmd, CONFIG_NAME_PAUSE)) |
---|
[2636] | 238 | { |
---|
| 239 | if( !cmd->bUp) |
---|
| 240 | { |
---|
| 241 | if(this->isPaused) |
---|
| 242 | this->resume(); |
---|
| 243 | else |
---|
| 244 | this->pause(); |
---|
| 245 | } |
---|
| 246 | return true; |
---|
| 247 | } |
---|
[4091] | 248 | else if( !strcmp( cmd->cmd, CONFIG_NAME_QUIT)) |
---|
[3220] | 249 | { |
---|
| 250 | if( !cmd->bUp) this->stop(); |
---|
| 251 | return true; |
---|
| 252 | } |
---|
[2636] | 253 | return false; |
---|
| 254 | } |
---|
| 255 | |
---|
[3225] | 256 | |
---|
| 257 | /* |
---|
| 258 | \brief this changes to the next level |
---|
| 259 | */ |
---|
[2636] | 260 | void GameLoader::nextLevel() |
---|
| 261 | { |
---|
| 262 | if(this->currentCampaign != NULL) |
---|
| 263 | this->currentCampaign->nextLevel(); |
---|
| 264 | } |
---|
| 265 | |
---|
[3225] | 266 | |
---|
| 267 | /* |
---|
| 268 | \brief change to the previous level - not implemented |
---|
| 269 | |
---|
| 270 | this propably useless |
---|
| 271 | */ |
---|
[2636] | 272 | void GameLoader::previousLevel() |
---|
| 273 | { |
---|
| 274 | if(this->currentCampaign != NULL) |
---|
| 275 | this->currentCampaign->previousLevel(); |
---|
| 276 | } |
---|
[4010] | 277 | |
---|
| 278 | /** |
---|
| 279 | \brief add a Factory to the Factory Q |
---|
| 280 | \param factory a Factory to be registered |
---|
| 281 | */ |
---|
| 282 | void GameLoader::registerFactory( Factory* factory) |
---|
| 283 | { |
---|
| 284 | assert( factory != NULL); |
---|
| 285 | |
---|
[4114] | 286 | PRINTF(4)("Registered factory for '%s'\n", factory->getFactoryName()); |
---|
[4010] | 287 | |
---|
| 288 | if( first == NULL) first = factory; |
---|
| 289 | else first->registerFactory( factory); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /** |
---|
| 293 | \brief load a StoryEntity |
---|
| 294 | \param element a XMLElement containing all the needed info |
---|
| 295 | */ |
---|
| 296 | BaseObject* GameLoader::fabricate( TiXmlElement* element) |
---|
| 297 | { |
---|
[4114] | 298 | assert( element != NULL); |
---|
[4010] | 299 | |
---|
[4114] | 300 | if( first == NULL) |
---|
| 301 | { |
---|
| 302 | PRINTF(1)("GameLoader does not know any factories, fabricate() failed\n"); |
---|
| 303 | return NULL; |
---|
| 304 | } |
---|
[4010] | 305 | |
---|
[4114] | 306 | if( element->Value() != NULL) |
---|
| 307 | { |
---|
| 308 | PRINTF(4)("Attempting fabrication of a '%s'\n", element->Value()); |
---|
| 309 | BaseObject* b = first->fabricate( element); |
---|
| 310 | if( b == NULL) |
---|
| 311 | PRINTF(2)("Failed to fabricate a '%s'\n", element->Value()); |
---|
| 312 | else |
---|
| 313 | PRINTF(4)("Successfully fabricated a '%s'\n", element->Value()); |
---|
| 314 | return b; |
---|
| 315 | } |
---|
[4010] | 316 | |
---|
[4114] | 317 | PRINTF(2)("Fabricate failed, TiXmlElement did not contain a value\n"); |
---|
[4010] | 318 | |
---|
[4114] | 319 | return NULL; |
---|
[4010] | 320 | } |
---|