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