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