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