Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11579 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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