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 "core/input/InputManager.h" |
---|
35 | #include "core/input/InputState.h" |
---|
36 | #include "core/input/KeyBinder.h" |
---|
37 | #include "core/Clock.h" |
---|
38 | #include "core/ConsoleCommand.h" |
---|
39 | #include "core/ConfigValueIncludes.h" |
---|
40 | #include "core/CoreIncludes.h" |
---|
41 | #include "core/Game.h" |
---|
42 | #include "core/GameMode.h" |
---|
43 | #include "core/Core.h" |
---|
44 | #include "core/GraphicsManager.h" |
---|
45 | #include "core/GUIManager.h" |
---|
46 | #include "core/Loader.h" |
---|
47 | #include "core/XMLFile.h" |
---|
48 | |
---|
49 | #include "interfaces/Tickable.h" |
---|
50 | #include "objects/Radar.h" |
---|
51 | #include "objects/quest/QuestManager.h" |
---|
52 | #include "overlays/notifications/NotificationManager.h" |
---|
53 | #include "CameraManager.h" |
---|
54 | #include "LevelManager.h" |
---|
55 | #include "PlayerManager.h" |
---|
56 | |
---|
57 | namespace orxonox |
---|
58 | { |
---|
59 | DeclareGameState(GSLevel, "level", false, false); |
---|
60 | SetConsoleCommand(GSLevel, showIngameGUI, true); |
---|
61 | |
---|
62 | XMLFile* GSLevel::startFile_s = NULL; |
---|
63 | |
---|
64 | GSLevel::GSLevel(const GameStateInfo& info) |
---|
65 | : GameState(info) |
---|
66 | , keyBinder_(0) |
---|
67 | , gameInputState_(0) |
---|
68 | , guiMouseOnlyInputState_(0) |
---|
69 | , guiKeysOnlyInputState_(0) |
---|
70 | , radar_(0) |
---|
71 | , cameraManager_(0) |
---|
72 | { |
---|
73 | RegisterObject(GSLevel); |
---|
74 | |
---|
75 | this->ccKeybind_ = 0; |
---|
76 | this->ccTkeybind_ = 0; |
---|
77 | } |
---|
78 | |
---|
79 | GSLevel::~GSLevel() |
---|
80 | { |
---|
81 | } |
---|
82 | |
---|
83 | void GSLevel::setConfigValues() |
---|
84 | { |
---|
85 | SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName="); |
---|
86 | } |
---|
87 | |
---|
88 | void GSLevel::activate() |
---|
89 | { |
---|
90 | setConfigValues(); |
---|
91 | |
---|
92 | if (GameMode::showsGraphics()) |
---|
93 | { |
---|
94 | gameInputState_ = InputManager::getInstance().createInputState("game"); |
---|
95 | keyBinder_ = new KeyBinder(); |
---|
96 | keyBinder_->loadBindings("keybindings.ini"); |
---|
97 | gameInputState_->setHandler(keyBinder_); |
---|
98 | |
---|
99 | guiMouseOnlyInputState_ = InputManager::getInstance().createInputState("guiMouseOnly"); |
---|
100 | guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr()); |
---|
101 | |
---|
102 | guiKeysOnlyInputState_ = InputManager::getInstance().createInputState("guiKeysOnly"); |
---|
103 | guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr()); |
---|
104 | |
---|
105 | // create the global CameraManager |
---|
106 | this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport()); |
---|
107 | |
---|
108 | // Start the Radar |
---|
109 | this->radar_ = new Radar(); |
---|
110 | } |
---|
111 | |
---|
112 | this->playerManager_ = new PlayerManager(); |
---|
113 | |
---|
114 | this->questManager_ = new QuestManager(); |
---|
115 | |
---|
116 | this->notificationManager_ = new NotificationManager(); |
---|
117 | |
---|
118 | if (GameMode::isMaster()) |
---|
119 | { |
---|
120 | this->loadLevel(); |
---|
121 | } |
---|
122 | |
---|
123 | if (GameMode::showsGraphics()) |
---|
124 | { |
---|
125 | // keybind console command |
---|
126 | FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind); |
---|
127 | functor1->setObject(this); |
---|
128 | ccKeybind_ = createConsoleCommand(functor1, "keybind"); |
---|
129 | CommandExecutor::addConsoleCommandShortcut(ccKeybind_); |
---|
130 | FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind); |
---|
131 | functor2->setObject(this); |
---|
132 | ccTkeybind_ = createConsoleCommand(functor2, "tkeybind"); |
---|
133 | CommandExecutor::addConsoleCommandShortcut(ccTkeybind_); |
---|
134 | // set our console command as callback for the key detector |
---|
135 | InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_); |
---|
136 | |
---|
137 | // level is loaded: we can start capturing the input |
---|
138 | InputManager::getInstance().enterState("game"); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | void GSLevel::showIngameGUI(bool show) |
---|
143 | { |
---|
144 | if (show) |
---|
145 | { |
---|
146 | GUIManager::getInstance().showGUI("inGameTest"); |
---|
147 | GUIManager::getInstance().executeCode("showCursor()"); |
---|
148 | InputManager::getInstance().enterState("guiMouseOnly"); |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | GUIManager::getInstance().executeCode("hideGUI(\"inGameTest\")"); |
---|
153 | GUIManager::getInstance().executeCode("hideCursor()"); |
---|
154 | InputManager::getInstance().leaveState("guiMouseOnly"); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | void GSLevel::deactivate() |
---|
159 | { |
---|
160 | /* |
---|
161 | // destroy console commands |
---|
162 | if (this->ccKeybind_) |
---|
163 | { |
---|
164 | delete this->ccKeybind_; |
---|
165 | this->ccKeybind_ = 0; |
---|
166 | } |
---|
167 | if (this->ccTkeybind_) |
---|
168 | { |
---|
169 | delete this->ccTkeybind_; |
---|
170 | this->ccTkeybind_ = 0; |
---|
171 | } |
---|
172 | */ |
---|
173 | |
---|
174 | if (GameMode::showsGraphics()) |
---|
175 | { |
---|
176 | // unload all compositors (this is only necessary because we don't yet destroy all resources!) |
---|
177 | Ogre::CompositorManager::getSingleton().removeAll(); |
---|
178 | } |
---|
179 | |
---|
180 | // this call will delete every BaseObject! |
---|
181 | // But currently this will call methods of objects that exist no more |
---|
182 | // The only 'memory leak' is the ParticleSpawer. They would be deleted here |
---|
183 | // and call a sceneNode method that has already been destroy by the corresponding space ship. |
---|
184 | //Loader::close(); |
---|
185 | |
---|
186 | if (GameMode::showsGraphics()) |
---|
187 | InputManager::getInstance().leaveState("game"); |
---|
188 | |
---|
189 | if (GameMode::isMaster()) |
---|
190 | this->unloadLevel(); |
---|
191 | |
---|
192 | if (this->radar_) |
---|
193 | { |
---|
194 | delete this->radar_; |
---|
195 | this->radar_ = 0; |
---|
196 | } |
---|
197 | |
---|
198 | if (this->cameraManager_) |
---|
199 | { |
---|
200 | delete this->cameraManager_; |
---|
201 | this->cameraManager_ = 0; |
---|
202 | } |
---|
203 | |
---|
204 | if (this->playerManager_) |
---|
205 | { |
---|
206 | delete this->playerManager_; |
---|
207 | this->playerManager_ = 0; |
---|
208 | } |
---|
209 | |
---|
210 | if (this->questManager_) |
---|
211 | { |
---|
212 | delete this->questManager_; |
---|
213 | this->questManager_ = NULL; |
---|
214 | } |
---|
215 | |
---|
216 | if (this->notificationManager_) |
---|
217 | { |
---|
218 | delete this->notificationManager_; |
---|
219 | this->notificationManager_ = NULL; |
---|
220 | } |
---|
221 | |
---|
222 | if (GameMode::showsGraphics()) |
---|
223 | { |
---|
224 | gameInputState_->setHandler(0); |
---|
225 | guiMouseOnlyInputState_->setHandler(0); |
---|
226 | guiKeysOnlyInputState_->setHandler(0); |
---|
227 | InputManager::getInstance().destroyState("game"); |
---|
228 | if (this->keyBinder_) |
---|
229 | { |
---|
230 | delete this->keyBinder_; |
---|
231 | this->keyBinder_ = 0; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | void GSLevel::update(const Clock& time) |
---|
237 | { |
---|
238 | // Note: Temporarily moved to GSGraphics. |
---|
239 | //// Call the scene objects |
---|
240 | //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
241 | // it->tick(time.getDeltaTime() * this->timeFactor_); |
---|
242 | } |
---|
243 | |
---|
244 | void GSLevel::loadLevel() |
---|
245 | { |
---|
246 | // call the loader |
---|
247 | COUT(0) << "Loading level..." << std::endl; |
---|
248 | startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel()); |
---|
249 | Loader::open(startFile_s); |
---|
250 | } |
---|
251 | |
---|
252 | void GSLevel::unloadLevel() |
---|
253 | { |
---|
254 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
255 | // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // |
---|
256 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
257 | // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY / |
---|
258 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
259 | |
---|
260 | delete startFile_s; |
---|
261 | } |
---|
262 | |
---|
263 | void GSLevel::keybind(const std::string &command) |
---|
264 | { |
---|
265 | this->keybindInternal(command, false); |
---|
266 | } |
---|
267 | |
---|
268 | void GSLevel::tkeybind(const std::string &command) |
---|
269 | { |
---|
270 | this->keybindInternal(command, true); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | @brief |
---|
275 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector. |
---|
276 | @param command |
---|
277 | Command string that can be executed by the CommandExecutor |
---|
278 | OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify |
---|
279 | the key/button/axis that has been activated. This is configured above in activate(). |
---|
280 | */ |
---|
281 | void GSLevel::keybindInternal(const std::string& command, bool bTemporary) |
---|
282 | { |
---|
283 | if (GameMode::showsGraphics()) |
---|
284 | { |
---|
285 | static std::string bindingString = ""; |
---|
286 | static bool bTemporarySaved = false; |
---|
287 | static bool bound = true; |
---|
288 | // note: We use a long name to make 'sure' that the user doesn't use it accidentally. |
---|
289 | // Howerver there will be no real issue if it happens anyway. |
---|
290 | if (command.find(keyDetectorCallbackCode_) != 0) |
---|
291 | { |
---|
292 | if (bound) |
---|
293 | { |
---|
294 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
295 | InputManager::getInstance().enterState("detector"); |
---|
296 | bindingString = command; |
---|
297 | bTemporarySaved = bTemporary; |
---|
298 | bound = false; |
---|
299 | } |
---|
300 | //else: We're still in a keybind command. ignore this call. |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | if (!bound) |
---|
305 | { |
---|
306 | // user has pressed the key |
---|
307 | std::string name = command.substr(this->keyDetectorCallbackCode_.size()); |
---|
308 | COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; |
---|
309 | this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); |
---|
310 | InputManager::getInstance().leaveState("detector"); |
---|
311 | bound = true; |
---|
312 | } |
---|
313 | // else: A key was pressed within the same tick, ignore it. |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|