Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/Spectator.cc @ 7862

Last change on this file since 7862 was 7862, checked in by landauf, 13 years ago

added feature to Spectator which changes the keybind mode of the fire command to OnPress if the player controls a Spectator. changes mode back to OnHold if the player stops control of the Spectator (e.g. spawns) by using a helper function.

This avoids accidentally firing the weapons right after spawn.

  • Property svn:eol-style set to native
File size: 7.2 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 "Spectator.h"
30
31#include "util/Convert.h"
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "core/GameMode.h"
35#include "core/command/CommandExecutor.h"
36#include "core/command/ConsoleCommand.h"
37
38#include "tools/BillboardSet.h"
39#include "Scene.h"
40#include "infos/PlayerInfo.h"
41
42namespace orxonox
43{
44    extern const std::string __CC_fire_name;
45
46    CreateFactory(Spectator);
47
48    Spectator::Spectator(BaseObject* creator) : ControllableEntity(creator)
49    {
50        RegisterObject(Spectator);
51
52        this->speed_ = 200;
53
54        this->yaw_ = 0;
55        this->pitch_ = 0;
56        this->roll_ = 0;
57        this->localVelocity_ = Vector3::ZERO;
58        this->setHudTemplate("spectatorhud");
59        this->greetingFlare_ = 0;
60
61        this->setDestroyWhenPlayerLeft(true);
62
63        if (GameMode::showsGraphics())
64        {
65            this->greetingFlare_ = new BillboardSet();
66            this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0f, 1.0f, 0.8f), Vector3(0, 20, 0), 1);
67            if (this->greetingFlare_->getBillboardSet())
68                this->attachOgreObject(this->greetingFlare_->getBillboardSet());
69            this->greetingFlare_->setVisible(false);
70        }
71
72        this->bGreetingFlareVisible_ = false;
73        this->bGreeting_ = false;
74
75        this->setConfigValues();
76        this->registerVariables();
77    }
78
79    Spectator::~Spectator()
80    {
81        if (this->isInitialized())
82        {
83            if (this->greetingFlare_)
84            {
85                if (this->greetingFlare_->getBillboardSet())
86                    this->detachOgreObject(this->greetingFlare_->getBillboardSet());
87
88                delete this->greetingFlare_;
89            }
90        }
91    }
92
93    void Spectator::setConfigValues()
94    {
95        SetConfigValue(speed_, 200.0f);
96    }
97
98    void Spectator::registerVariables()
99    {
100        registerVariable(this->bGreetingFlareVisible_, VariableDirection::ToClient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility));
101        registerVariable(this->bGreeting_,             VariableDirection::ToServer, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting));
102    }
103
104    void Spectator::changedGreeting()
105    {
106        this->bGreetingFlareVisible_ = this->bGreeting_;
107        this->changedFlareVisibility();
108    }
109
110    void Spectator::changedFlareVisibility()
111    {
112        if (this->greetingFlare_)
113            this->greetingFlare_->setVisible(this->bGreetingFlareVisible_);
114    }
115
116    void Spectator::tick(float dt)
117    {
118        if (this->hasLocalController())
119        {
120            float localSpeedSquared = this->localVelocity_.squaredLength();
121            float localSpeed;
122            if (localSpeedSquared > 1.0)
123                localSpeed = this->speed_ / sqrtf(localSpeedSquared);
124            else
125                localSpeed = this->speed_;
126
127            this->localVelocity_.x *= localSpeed;
128            this->localVelocity_.y *= localSpeed;
129            this->localVelocity_.z *= localSpeed;
130            this->setVelocity(this->getOrientation() * this->localVelocity_);
131            this->localVelocity_.x = 0;
132            this->localVelocity_.y = 0;
133            this->localVelocity_.z = 0;
134
135            if (!this->isInMouseLook())
136            {
137                this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()));
138                this->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
139                this->roll(Radian(this->roll_ * this->getMouseLookSpeed()));
140            }
141
142            this->yaw_ = this->pitch_ = this->roll_ = 0;
143        }
144
145        SUPER(Spectator, tick, dt);
146    }
147
148    void Spectator::setPlayer(PlayerInfo* player)
149    {
150        ControllableEntity::setPlayer(player);
151
152//        this->setSyncMode(ObjectDirection::ToClient);
153    }
154
155    /**
156        @brief Changes the keybind mode of the fire command to OnPress.
157    */
158    void Spectator::startLocalHumanControl()
159    {
160        ControllableEntity::startLocalHumanControl();
161
162        // change keybind mode of fire command to OnPress to avoid firing after respawn
163        ModifyConsoleCommand(__CC_fire_name).keybindMode(KeybindMode::OnPress);
164    }
165
166    /**
167        @brief Changes the keybind mode of the fire command back to OnHold.
168    */
169    void Spectator::stopLocalHumanControl()
170    {
171        ControllableEntity::stopLocalHumanControl();
172
173        // change fire command to a helper function and change keybind mode to OnPress
174        // as soon as the player releases and presses the button again, the helper function will be called which changes the keybind mode back to OnHold
175        ModifyConsoleCommand(__CC_fire_name).pushFunction(&Spectator::resetFireCommand).keybindMode(KeybindMode::OnPress);
176    }
177
178    /**
179        @brief Helper function which changes the fire command back to the original function and keybind mode to OnHold.
180    */
181    void Spectator::resetFireCommand(unsigned int firemode)
182    {
183        ModifyConsoleCommand(__CC_fire_name).popFunction().keybindMode(KeybindMode::OnHold); // pop this helper function and change keybind mode
184
185        CommandExecutor::execute(__CC_fire_name + " " + multi_cast<std::string>(firemode)); // call the fire command again, this time with the real function
186    }
187
188    void Spectator::moveFrontBack(const Vector2& value)
189    {
190        this->localVelocity_.z -= value.x;
191    }
192
193    void Spectator::moveRightLeft(const Vector2& value)
194    {
195        this->localVelocity_.x += value.x;
196    }
197
198    void Spectator::moveUpDown(const Vector2& value)
199    {
200        this->localVelocity_.y += value.x;
201    }
202
203    void Spectator::rotateYaw(const Vector2& value)
204    {
205        this->yaw_ += value.y;
206
207        ControllableEntity::rotateYaw(value);
208    }
209
210    void Spectator::rotatePitch(const Vector2& value)
211    {
212        this->pitch_ += value.y;
213
214        ControllableEntity::rotatePitch(value);
215    }
216
217    void Spectator::rotateRoll(const Vector2& value)
218    {
219        this->roll_ += value.y;
220
221        ControllableEntity::rotateRoll(value);
222    }
223
224    void Spectator::fired(unsigned int firemode)
225    {
226        if (this->getPlayer())
227            this->getPlayer()->setReadyToSpawn(true);
228    }
229
230    void Spectator::greet()
231    {
232        this->bGreeting_ = !this->bGreeting_;
233
234        if (GameMode::isMaster())
235        {
236            this->bGreetingFlareVisible_ = this->bGreeting_;
237            this->changedFlareVisibility();
238        }
239    }
240}
Note: See TracBrowser for help on using the repository browser.