[6501] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | #include "simple_game_menu.h" |
---|
| 20 | |
---|
[6502] | 21 | #include "state.h" |
---|
| 22 | #include "class_list.h" |
---|
| 23 | |
---|
| 24 | #include "load_param.h" |
---|
| 25 | #include "fast_factory.h" |
---|
| 26 | #include "factory.h" |
---|
| 27 | |
---|
[6521] | 28 | #include "p_node.h" |
---|
[6502] | 29 | #include "world_entity.h" |
---|
[6520] | 30 | #include "image_entity.h" |
---|
[6502] | 31 | #include "terrain.h" |
---|
[6521] | 32 | #include "camera.h" |
---|
[6502] | 33 | |
---|
[6504] | 34 | #include "event_handler.h" |
---|
[6524] | 35 | #include "graphics_engine.h" |
---|
[6841] | 36 | #include "object_manager.h" |
---|
[6504] | 37 | |
---|
[6841] | 38 | |
---|
[6502] | 39 | #include "cd_engine.h" |
---|
| 40 | |
---|
| 41 | |
---|
[6501] | 42 | using namespace std; |
---|
| 43 | |
---|
| 44 | |
---|
[6502] | 45 | //! This creates a Factory to fabricate a SimpleGameMenu |
---|
| 46 | CREATE_FACTORY(SimpleGameMenu, CL_SIMPLE_GAME_MENU); |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | SimpleGameMenu::SimpleGameMenu(const TiXmlElement* root) |
---|
| 51 | : GameWorld(root) |
---|
| 52 | { |
---|
| 53 | this->setClassID(CL_SIMPLE_GAME_MENU, "SimpleGameMenu"); |
---|
| 54 | this->setName("SimpleGameMenu uninitialized"); |
---|
| 55 | |
---|
| 56 | this->dataTank = new SimpleGameMenuData(); |
---|
| 57 | |
---|
[6521] | 58 | this->cameraVector = Vector(50.0, 0.0, 0.0); |
---|
[6835] | 59 | this->menuLayer.push_back(new MenuLayer()); |
---|
[6839] | 60 | this->menuLayer.push_back(new MenuLayer()); |
---|
[6837] | 61 | this->layerIndex = 0; |
---|
[6521] | 62 | |
---|
[6502] | 63 | this->loadParams(root); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
[6501] | 67 | /** |
---|
[6502] | 68 | * remove the SimpleGameMenu from memory |
---|
| 69 | * |
---|
| 70 | * delete everything explicitly, that isn't contained in the parenting tree! |
---|
| 71 | * things contained in the tree are deleted automaticaly |
---|
[6501] | 72 | */ |
---|
[6502] | 73 | SimpleGameMenu::~SimpleGameMenu () |
---|
| 74 | { |
---|
| 75 | PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n"); |
---|
[6501] | 76 | |
---|
[6502] | 77 | if( this->dataTank) |
---|
| 78 | delete this->dataTank; |
---|
| 79 | } |
---|
[6501] | 80 | |
---|
[6502] | 81 | |
---|
| 82 | /** |
---|
| 83 | * loads the parameters of a SimpleGameMenu from an XML-element |
---|
| 84 | * @param root the XML-element to load from |
---|
[6501] | 85 | */ |
---|
[6502] | 86 | void SimpleGameMenu::loadParams(const TiXmlElement* root) |
---|
| 87 | { |
---|
| 88 | /* skip the GameWorld, since it does not define any useful loadParams for this class */ |
---|
[6696] | 89 | //static_cast<GameWorld*>(this)->loadParams(root); |
---|
| 90 | GameWorld::loadParams(root); |
---|
[6501] | 91 | |
---|
[6502] | 92 | PRINTF(4)("Loaded SimpleGameMenu specific stuff\n"); |
---|
| 93 | } |
---|
[6501] | 94 | |
---|
[6502] | 95 | |
---|
[6501] | 96 | /** |
---|
[6504] | 97 | * this is executed just before load |
---|
| 98 | * |
---|
| 99 | * since the load function sometimes needs data, that has been initialized |
---|
| 100 | * before the load and after the proceeding storyentity has finished |
---|
| 101 | */ |
---|
| 102 | ErrorMessage SimpleGameMenu::init() |
---|
| 103 | { |
---|
| 104 | /* call underlying init funciton */ |
---|
| 105 | GameWorld::init(); |
---|
| 106 | |
---|
| 107 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_UP); |
---|
| 108 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_DOWN); |
---|
| 109 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_RETURN); |
---|
| 110 | EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_SPACE); |
---|
[6521] | 111 | |
---|
| 112 | this->dataTank->localCamera->setRelCoor(this->cameraVector); |
---|
[6524] | 113 | |
---|
| 114 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
[6504] | 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
[6524] | 118 | /** |
---|
| 119 | * load the data |
---|
| 120 | */ |
---|
[6521] | 121 | ErrorMessage SimpleGameMenu::loadData() |
---|
[6504] | 122 | { |
---|
[6521] | 123 | GameWorld::loadData(); |
---|
[6504] | 124 | |
---|
[6845] | 125 | if (this->dataXML != NULL) |
---|
| 126 | { |
---|
| 127 | TiXmlElement* element = this->dataXML->FirstChildElement("Elements"); |
---|
| 128 | |
---|
| 129 | if( element == NULL) |
---|
| 130 | { |
---|
| 131 | PRINTF(1)("SimpleGameMenu is missing 'Elements'\n"); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | element = element->FirstChildElement(); |
---|
| 136 | // load Players/Objects/Whatever |
---|
| 137 | PRINTF(4)("Loading Elements\n"); |
---|
| 138 | while( element != NULL) |
---|
| 139 | { |
---|
| 140 | BaseObject* created = Factory::fabricate(element); |
---|
| 141 | if( created != NULL ) |
---|
| 142 | PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
[6851] | 143 | element = element->NextSiblingElement(); |
---|
[6845] | 144 | } |
---|
| 145 | PRINTF(4)("Done loading Elements\n"); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[6520] | 149 | /* get the menu list */ |
---|
| 150 | const std::list<BaseObject*>* imageEntityList = ClassList::getList(CL_IMAGE_ENTITY); |
---|
| 151 | std::list<BaseObject*>::const_iterator entity; |
---|
| 152 | for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++) |
---|
| 153 | { |
---|
| 154 | |
---|
| 155 | if( !strcmp("Selector_Menu", (*entity)->getName())) |
---|
| 156 | { |
---|
| 157 | this->menuSelector = dynamic_cast<ImageEntity*>(*entity); |
---|
| 158 | } |
---|
| 159 | else if( !strcmp( "StartGame_Menu", (*entity)->getName())) |
---|
| 160 | { |
---|
| 161 | this->menuStartGame = dynamic_cast<ImageEntity*>(*entity); |
---|
[6835] | 162 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
| 163 | |
---|
[6520] | 164 | } |
---|
| 165 | else if( !strcmp( "Multiplayer_Menu", (*entity)->getName())) |
---|
| 166 | { |
---|
| 167 | this->menuStartMultiplayerGame = dynamic_cast<ImageEntity*>(*entity); |
---|
[6835] | 168 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
[6520] | 169 | } |
---|
| 170 | else if( !strcmp( "Quit_Menu", (*entity)->getName())) |
---|
| 171 | { |
---|
| 172 | this->menuQuitGame = dynamic_cast<ImageEntity*>(*entity); |
---|
[6835] | 173 | this->menuLayer[0]->menuList.push_back(dynamic_cast<ImageEntity*>(*entity)); |
---|
[6520] | 174 | } |
---|
| 175 | } |
---|
| 176 | this->menuSelectedIndex = 0; |
---|
[6835] | 177 | this->menuSelected = this->menuLayer[0]->menuList[this->menuSelectedIndex]; |
---|
[6520] | 178 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
[6839] | 179 | |
---|
| 180 | |
---|
| 181 | // loading the storyentities submenu (singleplayer) |
---|
| 182 | const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY); |
---|
| 183 | std::list<BaseObject*>::const_iterator it; |
---|
| 184 | for( it = storyEntities->begin(); it != storyEntities->end(); it++) |
---|
| 185 | { |
---|
| 186 | StoryEntity* se = dynamic_cast<StoryEntity*>(*it); |
---|
| 187 | if( se->isContainedInMenu()) |
---|
| 188 | { |
---|
| 189 | this->menuLayer[1]->storyList.push_back(se); |
---|
| 190 | PRINTF(0)("Got a new menu entry |%s|\n", se->getName()); |
---|
[6841] | 191 | ImageEntity* ie = new ImageEntity(); |
---|
[6848] | 192 | ie->setTexture(se->getMenuItemImage()); |
---|
| 193 | ie->setRelCoor(0.0f,- (this->menuLayer[1]->menuList.size() * 10.0f), 0.0f); |
---|
| 194 | ie->setVisibility(false); |
---|
| 195 | this->menuLayer[1]->menuList.push_back(ie); |
---|
[6839] | 196 | } |
---|
| 197 | } |
---|
[6521] | 198 | } |
---|
[6520] | 199 | |
---|
[6521] | 200 | |
---|
| 201 | |
---|
[6524] | 202 | /** |
---|
| 203 | * start the menu |
---|
| 204 | */ |
---|
[6521] | 205 | bool SimpleGameMenu::start() |
---|
| 206 | { |
---|
| 207 | EventHandler::getInstance()->pushState(ES_MENU); |
---|
| 208 | |
---|
[6504] | 209 | /* now call the underlying*/ |
---|
| 210 | GameWorld::start(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | |
---|
[6524] | 215 | /** |
---|
| 216 | * stop the menu |
---|
| 217 | */ |
---|
[6504] | 218 | bool SimpleGameMenu::stop() |
---|
| 219 | { |
---|
| 220 | EventHandler::getInstance()->popState(); |
---|
| 221 | |
---|
| 222 | /* now call the underlying*/ |
---|
| 223 | GameWorld::stop(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | |
---|
[6521] | 227 | /** |
---|
| 228 | * override the standard tick for more functionality |
---|
| 229 | */ |
---|
| 230 | void SimpleGameMenu::tick() |
---|
| 231 | { |
---|
| 232 | GameWorld::tick(); |
---|
[6504] | 233 | |
---|
[6521] | 234 | this->animateScene(this->dt); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | |
---|
[6504] | 238 | /** |
---|
[6521] | 239 | * no collision detection in the menu |
---|
[6501] | 240 | */ |
---|
[6502] | 241 | void SimpleGameMenu::collide() |
---|
[6521] | 242 | { |
---|
| 243 | // this->dataTank->localCamera-> |
---|
| 244 | } |
---|
[6501] | 245 | |
---|
| 246 | |
---|
[6504] | 247 | /** |
---|
[6521] | 248 | * animate the scene |
---|
| 249 | */ |
---|
| 250 | void SimpleGameMenu::animateScene(float dt) |
---|
| 251 | { |
---|
[6524] | 252 | Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0)); |
---|
[6521] | 253 | this->cameraVector = q.apply(this->cameraVector); |
---|
| 254 | this->dataTank->localCamera->setRelCoor(this->cameraVector); |
---|
| 255 | this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | /** |
---|
[6504] | 260 | * event dispatcher funciton |
---|
| 261 | * @param event the incoming event |
---|
| 262 | */ |
---|
| 263 | void SimpleGameMenu::process(const Event &event) |
---|
| 264 | { |
---|
[6839] | 265 | /* ----------------- LAYER 1 ---------------*/ |
---|
[6837] | 266 | if( this->layerIndex == 0) |
---|
[6504] | 267 | { |
---|
[6837] | 268 | if( event.type == SDLK_RETURN && event.bPressed == true) |
---|
[6520] | 269 | { |
---|
[6837] | 270 | if( this->menuSelected == this->menuQuitGame) |
---|
| 271 | { |
---|
| 272 | this->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 273 | this->stop(); |
---|
| 274 | } |
---|
| 275 | if( this->menuSelected == this->menuStartGame) |
---|
| 276 | { |
---|
| 277 | //this->stop(); |
---|
| 278 | // switch to first submenu |
---|
[6839] | 279 | if( this->menuLayer[1]->menuList.size() == 0) |
---|
[6837] | 280 | { |
---|
[6839] | 281 | PRINTF(1)("Haven't got any StoryEntities to play!\n"); |
---|
[6837] | 282 | return; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | this->switchMenuLayer(this->layerIndex, 1); |
---|
| 286 | |
---|
| 287 | } |
---|
[6520] | 288 | } |
---|
[6837] | 289 | else if( event.type == SDLK_DOWN && event.bPressed == true) |
---|
[6520] | 290 | { |
---|
[6837] | 291 | // ImageEntity* |
---|
| 292 | if(this->menuSelectedIndex < (this->menuLayer[this->layerIndex]->menuList.size() - 1)) |
---|
| 293 | { |
---|
| 294 | this->menuSelected = this->menuLayer[this->layerIndex]->menuList[++this->menuSelectedIndex]; |
---|
| 295 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
| 296 | } |
---|
[6520] | 297 | } |
---|
[6837] | 298 | else if( event.type == SDLK_UP && event.bPressed == true) |
---|
[6520] | 299 | { |
---|
[6837] | 300 | if(this->menuSelectedIndex > 0) |
---|
| 301 | { |
---|
| 302 | this->menuSelected = this->menuLayer[this->layerIndex]->menuList[--this->menuSelectedIndex]; |
---|
| 303 | this->menuSelector->setAbsCoor(this->menuSelected->getAbsCoor()); |
---|
| 304 | } |
---|
[6520] | 305 | } |
---|
[6839] | 306 | } /* ----------------- LAYER 2 ---------------*/ |
---|
[6837] | 307 | else if( this->layerIndex == 1) |
---|
[6520] | 308 | { |
---|
[6848] | 309 | if( event.type == SDLK_RETURN && event.bPressed == true) |
---|
| 310 | { |
---|
| 311 | this->setNextStoryID( this->menuLayer[1]->storyList[this->menuSelectedIndex]->getStoryID()); |
---|
| 312 | this->stop(); |
---|
| 313 | } |
---|
[6520] | 314 | } |
---|
[6504] | 315 | } |
---|
[6502] | 316 | |
---|
| 317 | |
---|
[6837] | 318 | /** |
---|
| 319 | * switches to from one meny layer to an other |
---|
| 320 | * @param layer1 from layer |
---|
| 321 | * @param layer2 to layer |
---|
| 322 | */ |
---|
| 323 | void SimpleGameMenu::switchMenuLayer(int layer1, int layer2) |
---|
| 324 | { |
---|
| 325 | // wrong sizes |
---|
| 326 | if(layer1 >= this->menuLayer.size() || layer2 >= this->menuLayer.size()) |
---|
| 327 | return; |
---|
[6502] | 328 | |
---|
| 329 | |
---|
[6837] | 330 | PRINTF(0)("Removing layer %i\n", layer1); |
---|
| 331 | std::vector<ImageEntity*>::iterator it; |
---|
| 332 | // fade old menu |
---|
| 333 | for( it = this->menuLayer[layer1]->menuList.begin(); it != this->menuLayer[layer1]->menuList.end(); it++ ) |
---|
| 334 | { |
---|
[6848] | 335 | (*it)->setVisibility(false); |
---|
[6837] | 336 | } |
---|
[6502] | 337 | |
---|
| 338 | |
---|
[6837] | 339 | PRINTF(0)("Showing layer %i\n", layer1); |
---|
| 340 | // beam here the new menu |
---|
| 341 | for( it = this->menuLayer[layer2]->menuList.begin(); it != this->menuLayer[layer2]->menuList.end(); it++ ) |
---|
[6848] | 342 | { |
---|
| 343 | (*it)->setVisibility(true); |
---|
| 344 | } |
---|
[6837] | 345 | |
---|
| 346 | this->layerIndex = layer2; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
[6502] | 352 | /********************************************************************************************** |
---|
| 353 | SimpleGameMenuData |
---|
| 354 | **********************************************************************************************/ |
---|
| 355 | |
---|
| 356 | |
---|
[6501] | 357 | /** |
---|
[6502] | 358 | * SimpleGameMenuData constructor |
---|
[6501] | 359 | */ |
---|
[6502] | 360 | SimpleGameMenuData::SimpleGameMenuData() |
---|
[6501] | 361 | {} |
---|
| 362 | |
---|
| 363 | /** |
---|
[6502] | 364 | * SimpleGameMenuData decontructor |
---|
[6501] | 365 | */ |
---|
[6502] | 366 | SimpleGameMenuData::~SimpleGameMenuData() |
---|
[6501] | 367 | {} |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | /** |
---|
[6502] | 371 | * initialize the GameWorldDataData |
---|
[6501] | 372 | */ |
---|
[6502] | 373 | ErrorMessage SimpleGameMenuData::init() |
---|
| 374 | { |
---|
| 375 | /* call underlying function */ |
---|
| 376 | GameWorldData::init(); |
---|
| 377 | } |
---|
[6501] | 378 | |
---|
| 379 | |
---|
| 380 | /** |
---|
[6502] | 381 | * loads the GUI data |
---|
| 382 | * @param root reference to the xml root element |
---|
[6501] | 383 | */ |
---|
[6502] | 384 | ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root) |
---|
| 385 | { |
---|
| 386 | /* call underlying function */ |
---|
| 387 | GameWorldData::loadGUI(root); |
---|
| 388 | } |
---|
[6501] | 389 | |
---|
| 390 | |
---|
| 391 | /** |
---|
[6502] | 392 | * unloads the GUI data |
---|
[6501] | 393 | */ |
---|
[6502] | 394 | ErrorMessage SimpleGameMenuData::unloadGUI() |
---|
| 395 | { |
---|
| 396 | /* call underlying function */ |
---|
| 397 | GameWorldData::unloadGUI(); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | |
---|
| 401 | /** |
---|
| 402 | * overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff) |
---|
| 403 | * @param root reference to the xml root parameter |
---|
| 404 | */ |
---|
| 405 | ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root) |
---|
| 406 | { |
---|
| 407 | TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
| 408 | |
---|
| 409 | if( element != NULL) |
---|
| 410 | { |
---|
| 411 | element = element->FirstChildElement(); |
---|
| 412 | PRINTF(4)("Loading WorldEntities\n"); |
---|
| 413 | while( element != NULL) |
---|
| 414 | { |
---|
| 415 | BaseObject* created = Factory::fabricate(element); |
---|
| 416 | if( created != NULL ) |
---|
| 417 | printf("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
| 418 | |
---|
| 419 | if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) |
---|
| 420 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
| 421 | if( element->Value() != NULL && !strcmp( element->Value(), "Terrain")) |
---|
| 422 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
| 423 | element = element->NextSiblingElement(); |
---|
| 424 | } |
---|
| 425 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | /* init the pnode tree */ |
---|
| 429 | PNode::getNullParent()->init(); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | /** |
---|
| 434 | * unloads the world entities from the xml file |
---|
| 435 | */ |
---|
| 436 | ErrorMessage SimpleGameMenuData::unloadWorldEntities() |
---|
| 437 | { |
---|
| 438 | /* call underlying function */ |
---|
| 439 | GameWorldData::unloadWorldEntities(); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | /** |
---|
| 444 | * loads the scene data |
---|
| 445 | * @param root reference to the xml root element |
---|
| 446 | */ |
---|
| 447 | ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root) |
---|
| 448 | { |
---|
| 449 | /* call underlying function */ |
---|
| 450 | GameWorldData::loadScene(root); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | /** |
---|
| 455 | * unloads the scene data |
---|
| 456 | */ |
---|
| 457 | ErrorMessage SimpleGameMenuData::unloadScene() |
---|
| 458 | { |
---|
| 459 | /* call underlying function */ |
---|
| 460 | GameWorldData::unloadScene(); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | |
---|