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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "HumanController.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "core/command/ConsoleCommandIncludes.h" |
---|
33 | #include "worldentities/ControllableEntity.h" |
---|
34 | #include "worldentities/pawns/Pawn.h" |
---|
35 | #include "gametypes/Gametype.h" |
---|
36 | #include "infos/PlayerInfo.h" |
---|
37 | #include "Radar.h" |
---|
38 | #include "Scene.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | extern const std::string __CC_push_name = "push"; |
---|
43 | extern const std::string __CC_suicide_name = "suicide"; |
---|
44 | extern const std::string __CC_release_name = "release"; |
---|
45 | |
---|
46 | SetConsoleCommand("HumanController", "moveFrontBack", &HumanController::moveFrontBack ).addShortcut().setAsInputCommand(); |
---|
47 | SetConsoleCommand("HumanController", "moveRightLeft", &HumanController::moveRightLeft ).addShortcut().setAsInputCommand(); |
---|
48 | SetConsoleCommand("HumanController", "moveUpDown", &HumanController::moveUpDown ).addShortcut().setAsInputCommand(); |
---|
49 | SetConsoleCommand("HumanController", "rotateYaw", &HumanController::rotateYaw ).addShortcut().setAsInputCommand(); |
---|
50 | SetConsoleCommand("HumanController", "rotatePitch", &HumanController::rotatePitch ).addShortcut().setAsInputCommand(); |
---|
51 | SetConsoleCommand("HumanController", "rotateRoll", &HumanController::rotateRoll ).addShortcut().setAsInputCommand(); |
---|
52 | SetConsoleCommand("HumanController", "toggleFormationFlight", &HumanController::toggleFormationFlight).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
53 | SetConsoleCommand("HumanController", "FFChangeMode", &HumanController::FFChangeMode).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
54 | SetConsoleCommand("HumanController", __CC_push_name, &HumanController::push ).addShortcut().keybindMode(KeybindMode::OnHold); |
---|
55 | SetConsoleCommand("HumanController", __CC_release_name, &HumanController::release ).addShortcut().keybindMode(KeybindMode::OnRelease); |
---|
56 | SetConsoleCommand("HumanController", "reload", &HumanController::reload ).addShortcut(); |
---|
57 | SetConsoleCommand("HumanController", "boost", &HumanController::boost ).addShortcut().setAsInputCommand().keybindMode(KeybindMode::OnPressAndRelease); |
---|
58 | SetConsoleCommand("HumanController", "greet", &HumanController::greet ).addShortcut(); |
---|
59 | SetConsoleCommand("HumanController", "switchCamera", &HumanController::switchCamera ).addShortcut(); |
---|
60 | SetConsoleCommand("HumanController", "mouseLook", &HumanController::mouseLook ).addShortcut(); |
---|
61 | SetConsoleCommand("HumanController", __CC_suicide_name, &HumanController::suicide ).addShortcut(); |
---|
62 | SetConsoleCommand("HumanController", "toggleGodMode", &HumanController::toggleGodMode ).addShortcut(); |
---|
63 | SetConsoleCommand("HumanController", "cycleNavigationFocus", &HumanController::cycleNavigationFocus).addShortcut(); |
---|
64 | SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut(); |
---|
65 | SetConsoleCommand("HumanController", "myposition", &HumanController::myposition ).addShortcut(); |
---|
66 | |
---|
67 | RegisterUnloadableClass(HumanController); |
---|
68 | |
---|
69 | HumanController* HumanController::localController_s = nullptr; |
---|
70 | |
---|
71 | HumanController::HumanController(Context* context) : FormationController(context) |
---|
72 | { |
---|
73 | RegisterObject(HumanController); |
---|
74 | |
---|
75 | this->controlPaused_ = false; |
---|
76 | HumanController::localController_s = this; |
---|
77 | } |
---|
78 | |
---|
79 | HumanController::~HumanController() |
---|
80 | { |
---|
81 | if (HumanController::localController_s) |
---|
82 | { |
---|
83 | HumanController::localController_s->removeFromFormation(); |
---|
84 | } |
---|
85 | HumanController::localController_s = nullptr; |
---|
86 | } |
---|
87 | |
---|
88 | void HumanController::tick(float dt) |
---|
89 | { |
---|
90 | if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
91 | { |
---|
92 | Camera* camera = HumanController::localController_s->controllableEntity_->getCamera(); |
---|
93 | if (!camera) |
---|
94 | orxout(internal_warning) << "HumanController, Warning: Using a ControllableEntity without Camera" << endl; |
---|
95 | } |
---|
96 | |
---|
97 | // commandslaves when Master of a formation |
---|
98 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER && FormationController::slaves_.size() > 0) |
---|
99 | { |
---|
100 | if (HumanController::localController_s->formationMode_ != ATTACK) |
---|
101 | HumanController::localController_s->commandSlaves(); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void HumanController::moveFrontBack(const Vector2& value) |
---|
106 | { |
---|
107 | if (HumanController::localController_s) |
---|
108 | HumanController::localController_s->frontback(value); |
---|
109 | } |
---|
110 | |
---|
111 | void HumanController::frontback(const Vector2& value) |
---|
112 | { |
---|
113 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
114 | HumanController::localController_s->controllableEntity_->moveFrontBack(value); |
---|
115 | } |
---|
116 | |
---|
117 | void HumanController::moveRightLeft(const Vector2& value) |
---|
118 | { |
---|
119 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
120 | HumanController::localController_s->controllableEntity_->moveRightLeft(value); |
---|
121 | } |
---|
122 | |
---|
123 | void HumanController::moveUpDown(const Vector2& value) |
---|
124 | { |
---|
125 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
126 | HumanController::localController_s->controllableEntity_->moveUpDown(value); |
---|
127 | } |
---|
128 | |
---|
129 | void HumanController::yaw(const Vector2& value) |
---|
130 | { |
---|
131 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
132 | HumanController::localController_s->controllableEntity_->rotateYaw(value); |
---|
133 | } |
---|
134 | |
---|
135 | void HumanController::pitch(const Vector2& value) |
---|
136 | { |
---|
137 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
138 | HumanController::localController_s->controllableEntity_->rotatePitch(value); |
---|
139 | } |
---|
140 | |
---|
141 | void HumanController::rotateYaw(const Vector2& value) |
---|
142 | { |
---|
143 | if (HumanController::localController_s) |
---|
144 | HumanController::localController_s->yaw(value); |
---|
145 | } |
---|
146 | |
---|
147 | void HumanController::rotatePitch(const Vector2& value) |
---|
148 | { |
---|
149 | if (HumanController::localController_s) |
---|
150 | HumanController::localController_s->pitch(value); |
---|
151 | } |
---|
152 | |
---|
153 | void HumanController::rotateRoll(const Vector2& value) |
---|
154 | { |
---|
155 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
156 | HumanController::localController_s->controllableEntity_->rotateRoll(value); |
---|
157 | } |
---|
158 | |
---|
159 | void HumanController::push(unsigned int firemode) |
---|
160 | { |
---|
161 | if (HumanController::localController_s) |
---|
162 | HumanController::localController_s->doPush(firemode); |
---|
163 | } |
---|
164 | |
---|
165 | void HumanController::doPush(unsigned int firemode) |
---|
166 | { |
---|
167 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
168 | { |
---|
169 | HumanController::localController_s->controllableEntity_->push(firemode); |
---|
170 | //if human fires, set slaves free. See FormationController::forceFreeSlaves() |
---|
171 | if (HumanController::localController_s->state_==MASTER && HumanController::localController_s->formationMode_ == NORMAL) |
---|
172 | { |
---|
173 | HumanController::localController_s->forceFreeSlaves(); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | void HumanController::release(unsigned int firemode) |
---|
179 | { |
---|
180 | if (HumanController::localController_s) |
---|
181 | HumanController::localController_s->doRelease(firemode); |
---|
182 | } |
---|
183 | |
---|
184 | void HumanController::doRelease(unsigned int firemode) |
---|
185 | { |
---|
186 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
187 | { |
---|
188 | HumanController::localController_s->controllableEntity_->release(firemode); |
---|
189 | //if human releases, set slaves free. See FormationController::forceFreeSlaves() |
---|
190 | if (HumanController::localController_s->state_==MASTER && HumanController::localController_s->formationMode_ == NORMAL) |
---|
191 | { |
---|
192 | HumanController::localController_s->forceFreeSlaves(); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void HumanController::reload() |
---|
198 | { |
---|
199 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
200 | HumanController::localController_s->controllableEntity_->reload(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | @brief |
---|
205 | Static method, controls boosting. |
---|
206 | */ |
---|
207 | /*static*/ void HumanController::boost(const Vector2& value) |
---|
208 | { |
---|
209 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
210 | { |
---|
211 | float abs = value.x; |
---|
212 | if (abs > 0) |
---|
213 | HumanController::localController_s->startBoosting(); |
---|
214 | else |
---|
215 | HumanController::localController_s->stopBoosting(); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | @brief |
---|
221 | Starts the boosting mode. |
---|
222 | Resets the boosting timeout and tells the ControllableEntity to boost (or not boost anymore). |
---|
223 | */ |
---|
224 | void HumanController::startBoosting(void) |
---|
225 | { |
---|
226 | if(this->controllableEntity_) |
---|
227 | this->controllableEntity_->boost(true); |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | @brief |
---|
232 | Stops the boosting mode. |
---|
233 | */ |
---|
234 | void HumanController::stopBoosting(void) |
---|
235 | { |
---|
236 | if(this->controllableEntity_) |
---|
237 | this->controllableEntity_->boost(false); |
---|
238 | } |
---|
239 | |
---|
240 | void HumanController::greet() |
---|
241 | { |
---|
242 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
243 | HumanController::localController_s->controllableEntity_->greet(); |
---|
244 | } |
---|
245 | |
---|
246 | void HumanController::switchCamera() |
---|
247 | { |
---|
248 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
249 | HumanController::localController_s->controllableEntity_->switchCamera(); |
---|
250 | } |
---|
251 | |
---|
252 | void HumanController::mouseLook() |
---|
253 | { |
---|
254 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
255 | HumanController::localController_s->controllableEntity_->mouseLook(); |
---|
256 | } |
---|
257 | |
---|
258 | void HumanController::suicide() |
---|
259 | { |
---|
260 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
261 | { |
---|
262 | Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_); |
---|
263 | if (pawn) |
---|
264 | pawn->kill(); |
---|
265 | else if (HumanController::localController_s->player_) |
---|
266 | HumanController::localController_s->player_->stopControl(); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | void HumanController::toggleGodMode() |
---|
271 | { |
---|
272 | if (HumanController::localController_s) |
---|
273 | HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode()); |
---|
274 | } |
---|
275 | |
---|
276 | void HumanController::myposition() |
---|
277 | { |
---|
278 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
279 | { |
---|
280 | const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition(); |
---|
281 | const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation(); |
---|
282 | |
---|
283 | orxout(message) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" " |
---|
284 | << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << endl; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | @brief |
---|
290 | toggle the formation. Not usable, if formationflight is disabled generally (formationFlight_) |
---|
291 | */ |
---|
292 | void HumanController::toggleFormationFlight() |
---|
293 | { |
---|
294 | if (HumanController::localController_s) |
---|
295 | { |
---|
296 | if (!HumanController::localController_s->formationFlight_) |
---|
297 | { |
---|
298 | return; //dont use when formationFlight is disabled |
---|
299 | } |
---|
300 | if (HumanController::localController_s->state_==MASTER) |
---|
301 | { |
---|
302 | HumanController::localController_s->loseMasterState(); |
---|
303 | orxout(message) <<"FormationFlight disabled "<< endl; |
---|
304 | } else //SLAVE or FREE |
---|
305 | { |
---|
306 | HumanController::localController_s->takeLeadOfFormation(); |
---|
307 | orxout(message) <<"FormationFlight enabled "<< endl; |
---|
308 | } |
---|
309 | |
---|
310 | } |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | @brief |
---|
316 | Switch through the different Modes of formationflight. You must be a master of a formation to use. |
---|
317 | */ |
---|
318 | void HumanController::FFChangeMode() |
---|
319 | { |
---|
320 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER) |
---|
321 | { |
---|
322 | switch (HumanController::localController_s->getFormationMode()) { |
---|
323 | case NORMAL: |
---|
324 | HumanController::localController_s->setFormationMode(DEFEND); |
---|
325 | orxout(message) <<"Mode: DEFEND "<< endl; |
---|
326 | break; |
---|
327 | case DEFEND: |
---|
328 | HumanController::localController_s->setFormationMode(ATTACK); |
---|
329 | orxout(message) <<"Mode: ATTACK "<< endl; |
---|
330 | break; |
---|
331 | case ATTACK: |
---|
332 | HumanController::localController_s->setFormationMode(NORMAL); |
---|
333 | orxout(message) <<"Mode: NORMAL "<< endl; |
---|
334 | break; |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | Pawn* HumanController::getLocalControllerEntityAsPawn() |
---|
340 | { |
---|
341 | if (HumanController::localController_s) |
---|
342 | return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity()); |
---|
343 | else |
---|
344 | return nullptr; |
---|
345 | } |
---|
346 | |
---|
347 | void HumanController::cycleNavigationFocus() |
---|
348 | { |
---|
349 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
350 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus(); |
---|
351 | } |
---|
352 | |
---|
353 | void HumanController::releaseNavigationFocus() |
---|
354 | { |
---|
355 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
356 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus(); |
---|
357 | } |
---|
358 | |
---|
359 | void HumanController::pauseControl() |
---|
360 | { |
---|
361 | if (HumanController::localController_s) |
---|
362 | HumanController::localController_s->doPauseControl(); |
---|
363 | } |
---|
364 | |
---|
365 | void HumanController::resumeControl() |
---|
366 | { |
---|
367 | if (HumanController::localController_s) |
---|
368 | HumanController::localController_s->doResumeControl(); |
---|
369 | } |
---|
370 | } |
---|