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