| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      Fabian 'x3n' Landau | 
|---|
| 26 |  *      Benjamin Knecht | 
|---|
| 27 |  * | 
|---|
| 28 |  */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "GSLevel.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <OgreCompositorManager.h> | 
|---|
| 33 |  | 
|---|
| 34 | #include "util/Clock.h" | 
|---|
| 35 | #include "core/input/InputManager.h" | 
|---|
| 36 | #include "core/input/InputState.h" | 
|---|
| 37 | #include "core/input/KeyBinderManager.h" | 
|---|
| 38 | #include "core/ConsoleCommand.h" | 
|---|
| 39 | #include "core/Game.h" | 
|---|
| 40 | #include "core/GameMode.h" | 
|---|
| 41 | #include "core/GUIManager.h" | 
|---|
| 42 | #include "core/Loader.h" | 
|---|
| 43 | #include "core/XMLFile.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "LevelManager.h" | 
|---|
| 46 | #include "PlayerManager.h" | 
|---|
| 47 |  | 
|---|
| 48 | namespace orxonox | 
|---|
| 49 | { | 
|---|
| 50 |     DeclareGameState(GSLevel, "level", false, false); | 
|---|
| 51 |  | 
|---|
| 52 |     GSLevel::GSLevel(const GameStateInfo& info) | 
|---|
| 53 |         : GameState(info) | 
|---|
| 54 |         , gameInputState_(0) | 
|---|
| 55 |         , guiMouseOnlyInputState_(0) | 
|---|
| 56 |         , guiKeysOnlyInputState_(0) | 
|---|
| 57 |         , startFile_(0) | 
|---|
| 58 |     { | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |     GSLevel::~GSLevel() | 
|---|
| 62 |     { | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |     void GSLevel::activate() | 
|---|
| 66 |     { | 
|---|
| 67 |         if (GameMode::showsGraphics()) | 
|---|
| 68 |         { | 
|---|
| 69 |             gameInputState_ = InputManager::getInstance().createInputState("game"); | 
|---|
| 70 |             gameInputState_->setMouseMode(MouseMode::Exclusive); | 
|---|
| 71 |             gameInputState_->setHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); | 
|---|
| 72 |             KeyBinderManager::getInstance().setToDefault(); | 
|---|
| 73 |  | 
|---|
| 74 |             guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly"); | 
|---|
| 75 |             guiMouseOnlyInputState_->setMouseMode(MouseMode::Exclusive); | 
|---|
| 76 |             guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr()); | 
|---|
| 77 |  | 
|---|
| 78 |             guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly"); | 
|---|
| 79 |             guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr()); | 
|---|
| 80 |  | 
|---|
| 81 |             CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&GSLevel::showIngameGUI, this), "showIngameGUI")); | 
|---|
| 82 |         } | 
|---|
| 83 |  | 
|---|
| 84 |         if (GameMode::isMaster()) | 
|---|
| 85 |         { | 
|---|
| 86 |             this->loadLevel(); | 
|---|
| 87 |         } | 
|---|
| 88 |  | 
|---|
| 89 |         if (GameMode::showsGraphics()) | 
|---|
| 90 |         { | 
|---|
| 91 |             // level is loaded: we can start capturing the input | 
|---|
| 92 |             InputManager::getInstance().enterState("game"); | 
|---|
| 93 |              | 
|---|
| 94 |             // connect the HumanPlayer to the game | 
|---|
| 95 |             PlayerManager::getInstance().clientConnected(0); | 
|---|
| 96 |         } | 
|---|
| 97 |     } | 
|---|
| 98 |  | 
|---|
| 99 |     void GSLevel::showIngameGUI(bool show) | 
|---|
| 100 |     { | 
|---|
| 101 |         if (show) | 
|---|
| 102 |         { | 
|---|
| 103 |             GUIManager::getInstance().showGUI("inGameTest"); | 
|---|
| 104 |             GUIManager::getInstance().executeCode("showCursor()"); | 
|---|
| 105 |             InputManager::getInstance().enterState("guiMouseOnly"); | 
|---|
| 106 |         } | 
|---|
| 107 |         else | 
|---|
| 108 |         { | 
|---|
| 109 |             GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")"); | 
|---|
| 110 |             GUIManager::getInstance().executeCode("hideCursor()"); | 
|---|
| 111 |             InputManager::getInstance().leaveState("guiMouseOnly"); | 
|---|
| 112 |         } | 
|---|
| 113 |     } | 
|---|
| 114 |  | 
|---|
| 115 |     void GSLevel::deactivate() | 
|---|
| 116 |     { | 
|---|
| 117 |         if (GameMode::showsGraphics()) | 
|---|
| 118 |         { | 
|---|
| 119 |             // unload all compositors (this is only necessary because we don't yet destroy all resources!) | 
|---|
| 120 |             Ogre::CompositorManager::getSingleton().removeAll(); | 
|---|
| 121 |  | 
|---|
| 122 |             InputManager::getInstance().leaveState("game"); | 
|---|
| 123 |         } | 
|---|
| 124 |          | 
|---|
| 125 |         // disconnect all HumanPlayers | 
|---|
| 126 |         PlayerManager::getInstance().disconnectAllClients(); | 
|---|
| 127 |  | 
|---|
| 128 |         if (GameMode::isMaster()) | 
|---|
| 129 |             this->unloadLevel(); | 
|---|
| 130 |  | 
|---|
| 131 |         if (GameMode::showsGraphics()) | 
|---|
| 132 |         { | 
|---|
| 133 |             gameInputState_->setHandler(0); | 
|---|
| 134 |             guiMouseOnlyInputState_->setHandler(0); | 
|---|
| 135 |             guiKeysOnlyInputState_->setHandler(0); | 
|---|
| 136 |             InputManager::getInstance().destroyState("game"); | 
|---|
| 137 |             InputManager::getInstance().destroyState("guiKeysOnly"); | 
|---|
| 138 |             InputManager::getInstance().destroyState("guiMouseOnly"); | 
|---|
| 139 |         } | 
|---|
| 140 |     } | 
|---|
| 141 |  | 
|---|
| 142 |     void GSLevel::update(const Clock& time) | 
|---|
| 143 |     { | 
|---|
| 144 |         // Note: Temporarily moved to GSRoot. | 
|---|
| 145 |         //// Call the scene objects | 
|---|
| 146 |         //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| 147 |         //    it->tick(time.getDeltaTime() * this->timeFactor_); | 
|---|
| 148 |     } | 
|---|
| 149 |  | 
|---|
| 150 |     void GSLevel::loadLevel() | 
|---|
| 151 |     { | 
|---|
| 152 |         for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it) | 
|---|
| 153 |             this->staticObjects_.insert(*it); | 
|---|
| 154 |          | 
|---|
| 155 |         // call the loader | 
|---|
| 156 |         COUT(0) << "Loading level..." << std::endl; | 
|---|
| 157 |         startFile_ = new XMLFile(LevelManager::getInstance().getDefaultLevel()); | 
|---|
| 158 |         Loader::open(startFile_); | 
|---|
| 159 |     } | 
|---|
| 160 |  | 
|---|
| 161 |     void GSLevel::unloadLevel() | 
|---|
| 162 |     { | 
|---|
| 163 |         Loader::unload(startFile_); | 
|---|
| 164 |         delete startFile_; | 
|---|
| 165 |  | 
|---|
| 166 |         COUT(3) << "Unloaded level. Remaining objects:" << std::endl; | 
|---|
| 167 |         unsigned int i = 0; | 
|---|
| 168 |         for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ++it) | 
|---|
| 169 |         { | 
|---|
| 170 |             std::set<BaseObject*>::const_iterator find = this->staticObjects_.find(*it); | 
|---|
| 171 |             if (find == this->staticObjects_.end()) | 
|---|
| 172 |             { | 
|---|
| 173 |                 COUT(3) << ++i << ": " << it->getIdentifier()->getName() << " (" << *it << ")" << std::endl; | 
|---|
| 174 |             } | 
|---|
| 175 |         } | 
|---|
| 176 |         COUT(3) << i << " objects remaining."; | 
|---|
| 177 |         if (i == 0) | 
|---|
| 178 |             COUT(3) << " Well done!" << std::endl; | 
|---|
| 179 |         else | 
|---|
| 180 |             COUT(3) << " Try harder!" << std::endl; | 
|---|
| 181 |     } | 
|---|
| 182 | } | 
|---|