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 | // Commented by 1337: Temporarily moved to GSGraphics. |
---|
166 | //// Call the scene objects |
---|
167 | //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) |
---|
168 | // it->tick(time.getDeltaTime() * this->timeFactor_); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | @brief |
---|
173 | Changes the speed of Orxonox |
---|
174 | */ |
---|
175 | void GSLevel::setTimeFactor(float factor) |
---|
176 | { |
---|
177 | /* |
---|
178 | float change = factor / this->timeFactor_; |
---|
179 | */ |
---|
180 | this->timeFactor_ = factor; |
---|
181 | /* |
---|
182 | for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it) |
---|
183 | it->setSpeedFactor(it->getSpeedFactor() * change); |
---|
184 | |
---|
185 | for (ObjectList<Backlight>::iterator it = ObjectList<Backlight>::begin(); it; ++it) |
---|
186 | it->setTimeFactor(timeFactor_); |
---|
187 | */ |
---|
188 | } |
---|
189 | |
---|
190 | void GSLevel::loadLevel() |
---|
191 | { |
---|
192 | // call the loader |
---|
193 | COUT(0) << "Loading level..." << std::endl; |
---|
194 | startFile_ = new XMLFile(Settings::getDataPath() + "levels/sample2.oxw"); |
---|
195 | Loader::open(startFile_); |
---|
196 | } |
---|
197 | |
---|
198 | void GSLevel::unloadLevel() |
---|
199 | { |
---|
200 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
201 | // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // |
---|
202 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
203 | // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY / |
---|
204 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
205 | |
---|
206 | delete this->startFile_; |
---|
207 | } |
---|
208 | |
---|
209 | void GSLevel::keybind(const std::string &command) |
---|
210 | { |
---|
211 | this->keybindInternal(command, false); |
---|
212 | } |
---|
213 | |
---|
214 | void GSLevel::tkeybind(const std::string &command) |
---|
215 | { |
---|
216 | this->keybindInternal(command, true); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | @brief |
---|
221 | Assigns a command string to a key/button/axis. The name is determined via KeyDetector. |
---|
222 | @param command |
---|
223 | Command string that can be executed by the CommandExecutor |
---|
224 | OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify |
---|
225 | the key/button/axis that has been activated. This is configured above in enter(). |
---|
226 | */ |
---|
227 | void GSLevel::keybindInternal(const std::string& command, bool bTemporary) |
---|
228 | { |
---|
229 | if (Core::showsGraphics()) |
---|
230 | { |
---|
231 | static std::string bindingString = ""; |
---|
232 | static bool bTemporarySaved = false; |
---|
233 | static bool bound = true; |
---|
234 | // note: We use a long name to make 'sure' that the user doesn't use it accidentally. |
---|
235 | // Howerver there will be no real issue if it happens anyway. |
---|
236 | if (command.find(keyDetectorCallbackCode_) != 0) |
---|
237 | { |
---|
238 | if (bound) |
---|
239 | { |
---|
240 | COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl; |
---|
241 | InputManager::getInstance().requestEnterState("detector"); |
---|
242 | bindingString = command; |
---|
243 | bTemporarySaved = bTemporary; |
---|
244 | bound = false; |
---|
245 | } |
---|
246 | //else: We're still in a keybind command. ignore this call. |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | if (!bound) |
---|
251 | { |
---|
252 | // user has pressed the key |
---|
253 | std::string name = command.substr(this->keyDetectorCallbackCode_.size()); |
---|
254 | COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl; |
---|
255 | this->keyBinder_->setBinding(bindingString, name, bTemporarySaved); |
---|
256 | InputManager::getInstance().requestLeaveState("detector"); |
---|
257 | bound = true; |
---|
258 | } |
---|
259 | // else: A key was pressed within the same tick, ignore it. |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|