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