Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gameimmersion/src/orxonox/controllers/HumanController.cc @ 8574

Last change on this file since 8574 was 8574, checked in by dafrick, 14 years ago

Reverting boost changes.

  • Property svn:eol-style set to native
File size: 12.3 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", __CC_fire_name,           &HumanController::fire          ).addShortcut().keybindMode(KeybindMode::OnHold);
53    SetConsoleCommand("HumanController", "reload",                 &HumanController::reload        ).addShortcut();
54    SetConsoleCommand("HumanController", __CC_boost_name,          &HumanController::toggleBoost   ).addShortcut().keybindMode(KeybindMode::OnPress);
55    SetConsoleCommand("HumanController", "greet",                  &HumanController::greet         ).addShortcut();
56    SetConsoleCommand("HumanController", "switchCamera",           &HumanController::switchCamera  ).addShortcut();
57    SetConsoleCommand("HumanController", "mouseLook",              &HumanController::mouseLook     ).addShortcut();
58    SetConsoleCommand("HumanController", __CC_suicide_name,        &HumanController::suicide       ).addShortcut();
59    SetConsoleCommand("HumanController", "toggleGodMode",          &HumanController::toggleGodMode ).addShortcut();
60    SetConsoleCommand("HumanController", "addBots",                &HumanController::addBots       ).addShortcut().defaultValues(1);
61    SetConsoleCommand("HumanController", "killBots",               &HumanController::killBots      ).addShortcut().defaultValues(0);
62    SetConsoleCommand("HumanController", "cycleNavigationFocus",   &HumanController::cycleNavigationFocus).addShortcut();
63    SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut();
64    SetConsoleCommand("HumanController", "myposition",             &HumanController::myposition    ).addShortcut();
65
66    CreateUnloadableFactory(HumanController);
67
68    HumanController* HumanController::localController_s = 0;
69
70    HumanController::HumanController(BaseObject* creator) : Controller(creator)
71    {
72        RegisterObject(HumanController);
73
74        controlPaused_ = false;
75        this->boosting_ = false;
76
77        HumanController::localController_s = this;
78    }
79
80    HumanController::~HumanController()
81    {
82        HumanController::localController_s = 0;
83    }
84
85    void HumanController::tick(float dt)
86    {
87        if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_)
88        {
89            Camera* camera = HumanController::localController_s->controllableEntity_->getCamera();
90            if (!camera)
91                COUT(3) << "HumanController, Warning: Using a ControllableEntity without Camera" << std::endl;
92        }
93    }
94
95    void HumanController::moveFrontBack(const Vector2& value)
96    {
97        if (HumanController::localController_s)
98            HumanController::localController_s->frontback(value);
99    }
100
101    void HumanController::frontback(const Vector2& value)
102    {
103        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
104            HumanController::localController_s->controllableEntity_->moveFrontBack(value);
105    }
106
107    void HumanController::moveRightLeft(const Vector2& value)
108    {
109        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
110            HumanController::localController_s->controllableEntity_->moveRightLeft(value);
111    }
112
113    void HumanController::moveUpDown(const Vector2& value)
114    {
115        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
116            HumanController::localController_s->controllableEntity_->moveUpDown(value);
117    }
118
119    void HumanController::yaw(const Vector2& value)
120    {
121        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
122            HumanController::localController_s->controllableEntity_->rotateYaw(value);
123    }
124
125    void HumanController::pitch(const Vector2& value)
126    {
127        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
128            HumanController::localController_s->controllableEntity_->rotatePitch(value);
129    }
130
131    void HumanController::rotateYaw(const Vector2& value)
132    {
133        if (HumanController::localController_s)
134            HumanController::localController_s->yaw(value);
135    }
136
137    void HumanController::rotatePitch(const Vector2& value)
138    {
139        if (HumanController::localController_s)
140            HumanController::localController_s->pitch(value);
141    }
142
143    void HumanController::rotateRoll(const Vector2& value)
144    {
145        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
146            HumanController::localController_s->controllableEntity_->rotateRoll(value);
147    }
148
149    void HumanController::fire(unsigned int firemode)
150    {
151        if (HumanController::localController_s)
152            HumanController::localController_s->doFire(firemode);
153    }
154
155    void HumanController::doFire(unsigned int firemode)
156    {
157        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
158            HumanController::localController_s->controllableEntity_->fire(firemode);
159    }
160
161    void HumanController::reload()
162    {
163        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
164            HumanController::localController_s->controllableEntity_->reload();
165    }
166
167    /**
168    @brief
169        Static method,toggles boosting.
170    */
171    /*static*/ void HumanController::toggleBoost()
172    {
173        COUT(3) << "Toggling boost!" << endl; // TODO: Remove!
174        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
175            HumanController::localController_s->toggleBoosting();
176    }
177   
178    /**
179    @brief
180        Toggles the boosting mode.
181        Changes the keybind mode of the boost console command and tells the ControllableEntity to boost (or not boost anymore).
182    */
183    void HumanController::toggleBoosting(void)
184    {
185        this->boosting_ = !this->boosting_;
186       
187        // The keybind mode of the boosting console command is onRelease if in boosting mode and onPress of not in boosting mode.
188        if(this->boosting_)
189            ModifyConsoleCommand(__CC_boost_name).keybindMode(KeybindMode::OnRelease);
190        else
191            ModifyConsoleCommand(__CC_boost_name).keybindMode(KeybindMode::OnPress);
192
193        this->controllableEntity_->boost(this->boosting_);
194    }
195
196    void HumanController::greet()
197    {
198        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
199            HumanController::localController_s->controllableEntity_->greet();
200    }
201
202    void HumanController::switchCamera()
203    {
204        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
205            HumanController::localController_s->controllableEntity_->switchCamera();
206    }
207
208    void HumanController::mouseLook()
209    {
210        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
211            HumanController::localController_s->controllableEntity_->mouseLook();
212    }
213
214    void HumanController::suicide()
215    {
216        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
217        {
218            Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_);
219            if (pawn)
220                pawn->kill();
221            else if (HumanController::localController_s->player_)
222                HumanController::localController_s->player_->stopControl();
223        }
224    }
225
226    void HumanController::toggleGodMode()
227    {
228        if (HumanController::localController_s)
229            HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode());
230    }
231
232    void HumanController::myposition()
233    {
234        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
235        {
236            const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition();
237            const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation();
238
239            COUT(0) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" ";
240            COUT(0) << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << std::endl;
241        }
242    }
243
244    void HumanController::addBots(unsigned int amount)
245    {
246        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
247            HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount);
248    }
249
250    void HumanController::killBots(unsigned int amount)
251    {
252        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype())
253            HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount);
254    }
255
256    Pawn* HumanController::getLocalControllerEntityAsPawn()
257    {
258        if (HumanController::localController_s)
259            return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity());
260        else
261            return NULL;
262    }
263
264    void HumanController::cycleNavigationFocus()
265    {
266        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
267            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus();
268    }
269
270    void HumanController::releaseNavigationFocus()
271    {
272        if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
273            HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus();
274    }
275
276    void HumanController::pauseControl()
277    {
278        if (HumanController::localController_s)
279            HumanController::localController_s->doPauseControl();
280    }
281
282    void HumanController::resumeControl()
283    {
284        if (HumanController::localController_s)
285            HumanController::localController_s->doResumeControl();
286    }
287}
Note: See TracBrowser for help on using the repository browser.