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