Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formation/src/orxonox/controllers/HumanController.cc @ 8939

Last change on this file since 8939 was 8939, checked in by willis, 12 years ago

moved formationflight to Masterable.* from Artificialcontroller, added formationflight to HumanPlayer

  • Property svn:eol-style set to native
File size: 13.7 KB
Line 
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
40namespace 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().setAsInputCommand();
53    SetConsoleCommand("HumanController", __CC_fire_name,           &HumanController::fire          ).addShortcut().keybindMode(KeybindMode::OnHold);
54    SetConsoleCommand("HumanController", "reload",                 &HumanController::reload        ).addShortcut();
55    SetConsoleCommand("HumanController", __CC_boost_name,          &HumanController::keepBoost     ).addShortcut().keybindMode(KeybindMode::OnHold);
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", "addBots",                &HumanController::addBots       ).addShortcut().defaultValues(1);
62    SetConsoleCommand("HumanController", "killBots",               &HumanController::killBots      ).addShortcut().defaultValues(0);
63    SetConsoleCommand("HumanController", "cycleNavigationFocus",   &HumanController::cycleNavigationFocus).addShortcut();
64    SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut();
65    SetConsoleCommand("HumanController", "myposition",             &HumanController::myposition    ).addShortcut();
66
67    CreateUnloadableFactory(HumanController);
68
69    HumanController* HumanController::localController_s = 0;
70    /*static*/ const float HumanController::BOOSTING_TIME = 0.1f;
71
72    HumanController::HumanController(BaseObject* creator) : Masterable(creator)
73    {
74        RegisterObject(HumanController);
75
76        this->controlPaused_ = false;
77        this->boosting_ = false;
78        this->boosting_ = false;
79
80        HumanController::localController_s = this;
81        this->boostingTimeout_.setTimer(HumanController::BOOSTING_TIME, false, createExecutor(createFunctor(&HumanController::terminateBoosting, this)));
82        this->boostingTimeout_.stopTimer();
83        this->state_=MASTER;
84    }
85
86    HumanController::~HumanController()
87    {
88        HumanController::localController_s = 0;
89        if (this->state_==MASTER)
90            freeSlaves();
91    }
92
93    void HumanController::tick(float dt)
94    {
95        if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_)
96        {
97            Camera* camera = HumanController::localController_s->controllableEntity_->getCamera();
98            if (!camera)
99                orxout(internal_warning) << "HumanController, Warning: Using a ControllableEntity without Camera" << endl;
100        }
101
102        // commandslaves when Master of a formation
103        if (this->state_==MASTER)
104        {
105            this->commandSlaves();
106        }
107    }
108
109    void HumanController::moveFrontBack(const Vector2& value)
110    {
111        if (HumanController::localController_s)
112            HumanController::localController_s->frontback(value);
113    }
114
115    void HumanController::frontback(const Vector2& value)
116    {
117        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
118            HumanController::localController_s->controllableEntity_->moveFrontBack(value);
119    }
120
121    void HumanController::moveRightLeft(const Vector2& value)
122    {
123        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
124            HumanController::localController_s->controllableEntity_->moveRightLeft(value);
125    }
126
127    void HumanController::moveUpDown(const Vector2& value)
128    {
129        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
130            HumanController::localController_s->controllableEntity_->moveUpDown(value);
131    }
132
133    void HumanController::yaw(const Vector2& value)
134    {
135        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
136            HumanController::localController_s->controllableEntity_->rotateYaw(value);
137    }
138
139    void HumanController::pitch(const Vector2& value)
140    {
141        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
142            HumanController::localController_s->controllableEntity_->rotatePitch(value);
143    }
144
145    void HumanController::rotateYaw(const Vector2& value)
146    {
147        if (HumanController::localController_s)
148            HumanController::localController_s->yaw(value);
149    }
150
151    void HumanController::rotatePitch(const Vector2& value)
152    {
153        if (HumanController::localController_s)
154            HumanController::localController_s->pitch(value);
155    }
156
157    void HumanController::rotateRoll(const Vector2& value)
158    {
159        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
160            HumanController::localController_s->controllableEntity_->rotateRoll(value);
161    }
162
163    void HumanController::fire(unsigned int firemode)
164    {
165        if (HumanController::localController_s)
166            HumanController::localController_s->doFire(firemode);
167    }
168
169    void HumanController::doFire(unsigned int firemode)
170    {
171        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
172            HumanController::localController_s->controllableEntity_->fire(firemode);
173    }
174
175    void HumanController::reload()
176    {
177        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
178            HumanController::localController_s->controllableEntity_->reload();
179    }
180
181    /**
182    @brief
183        Static method,keeps boosting.
184    */
185    /*static*/ void HumanController::keepBoost()
186    {
187        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
188            HumanController::localController_s->keepBoosting();
189    }
190   
191    /**
192    @brief
193        Starts, or keeps the boosting mode.
194        Resets the boosting timeout and ells the ControllableEntity to boost (or not boost anymore).
195    */
196    void HumanController::keepBoosting(void)
197    {
198        if(this->boostingTimeout_.isActive())
199        {
200            this->boostingTimeout_.stopTimer();
201            this->boostingTimeout_.startTimer();
202        }
203        else
204        {
205            this->boosting_ = true;
206            this->boostingTimeout_.startTimer();
207           
208            this->controllableEntity_->boost(this->boosting_);
209//            orxout() << "Start boosting" << endl;
210        }
211    }
212
213    /**
214    @brief
215        Terminates the boosting mode.
216    */
217    void HumanController::terminateBoosting(void)
218    {
219        this->boosting_ = false;
220        this->boostingTimeout_.stopTimer();
221
222        this->controllableEntity_->boost(this->boosting_);
223//        orxout() << "Stop boosting" << endl;
224    }
225
226    void HumanController::greet()
227    {
228        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
229            HumanController::localController_s->controllableEntity_->greet();
230    }
231
232    void HumanController::switchCamera()
233    {
234        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
235            HumanController::localController_s->controllableEntity_->switchCamera();
236    }
237
238    void HumanController::mouseLook()
239    {
240        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
241            HumanController::localController_s->controllableEntity_->mouseLook();
242    }
243
244    void HumanController::suicide()
245    {
246        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
247        {
248            Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_);
249            if (pawn)
250                pawn->kill();
251            else if (HumanController::localController_s->player_)
252                HumanController::localController_s->player_->stopControl();
253        }
254    }
255
256    void HumanController::toggleGodMode()
257    {
258        if (HumanController::localController_s)
259            HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode());
260    }
261
262    void HumanController::myposition()
263    {
264        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
265        {
266            const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition();
267            const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation();
268
269            orxout(message) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" "
270                            << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << endl;
271        }
272    }
273
274    void HumanController::toggleFormationFlight()
275    {
276        if (HumanController::localController_s)
277        {
278            if (HumanController::localController_s->state_==MASTER)
279            {
280                HumanController::localController_s->freeSlaves();
281                HumanController::localController_s->state_=FREE;
282            } else //SLAVE or FREE
283            {
284                HumanController::localController_s->takeLeadOfFormation();
285                //HumanController::localController_s->state_=MASTER;
286            }
287        }
288
289    }
290
291    void HumanController::addBots(unsigned int amount)
292    {
293        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
294            HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount);
295    }
296
297    void HumanController::killBots(unsigned int amount)
298    {
299        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
300            HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount);
301    }
302
303    Pawn* HumanController::getLocalControllerEntityAsPawn()
304    {
305        if (HumanController::localController_s)
306            return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity());
307        else
308            return NULL;
309    }
310
311    void HumanController::cycleNavigationFocus()
312    {
313        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
314            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus();
315    }
316
317    void HumanController::releaseNavigationFocus()
318    {
319        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
320            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus();
321    }
322
323    void HumanController::pauseControl()
324    {
325        if (HumanController::localController_s)
326            HumanController::localController_s->doPauseControl();
327    }
328
329    void HumanController::resumeControl()
330    {
331        if (HumanController::localController_s)
332            HumanController::localController_s->doResumeControl();
333    }
334}
Note: See TracBrowser for help on using the repository browser.