Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8948 was 8948, checked in by willis, 13 years ago

debugging

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