Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 7.5 KB
RevLine 
[2072]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
[7862]31#include "util/Convert.h"
[2072]32#include "core/CoreIncludes.h"
[9667]33#include "core/config/ConfigValueIncludes.h"
[2896]34#include "core/GameMode.h"
[7862]35#include "core/command/CommandExecutor.h"
[10624]36#include "core/command/ConsoleCommandIncludes.h"
[3196]37
38#include "tools/BillboardSet.h"
[5735]39#include "Scene.h"
40#include "infos/PlayerInfo.h"
[2072]41
42namespace orxonox
43{
[7862]44    extern const std::string __CC_fire_name;
[7863]45    extern const std::string __CC_suicide_name;
[7862]46
[9667]47    RegisterClass(Spectator);
[2072]48
[9667]49    Spectator::Spectator(Context* context) : ControllableEntity(context)
[2072]50    {
51        RegisterObject(Spectator);
52
[2662]53        this->speed_ = 200;
[2072]54
55        this->yaw_ = 0;
56        this->pitch_ = 0;
57        this->roll_ = 0;
[2662]58        this->localVelocity_ = Vector3::ZERO;
[2072]59        this->setHudTemplate("spectatorhud");
[2662]60        this->greetingFlare_ = 0;
[2072]61
62        this->setDestroyWhenPlayerLeft(true);
63
[2896]64        if (GameMode::showsGraphics())
[2662]65        {
66            this->greetingFlare_ = new BillboardSet();
[3196]67            this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0f, 1.0f, 0.8f), Vector3(0, 20, 0), 1);
[2662]68            if (this->greetingFlare_->getBillboardSet())
69                this->attachOgreObject(this->greetingFlare_->getBillboardSet());
70            this->greetingFlare_->setVisible(false);
71        }
72
[2072]73        this->bGreetingFlareVisible_ = false;
74        this->bGreeting_ = false;
75
[2662]76        this->setConfigValues();
[2072]77        this->registerVariables();
78    }
79
80    Spectator::~Spectator()
81    {
82        if (this->isInitialized())
83        {
84            if (this->greetingFlare_)
85            {
[2171]86                if (this->greetingFlare_->getBillboardSet())
[2662]87                    this->detachOgreObject(this->greetingFlare_->getBillboardSet());
88
[2072]89                delete this->greetingFlare_;
90            }
91        }
92    }
93
[2662]94    void Spectator::setConfigValues()
95    {
96        SetConfigValue(speed_, 200.0f);
97    }
98
[2072]99    void Spectator::registerVariables()
100    {
[3280]101        registerVariable(this->bGreetingFlareVisible_, VariableDirection::ToClient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility));
102        registerVariable(this->bGreeting_,             VariableDirection::ToServer, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting));
[2072]103    }
104
105    void Spectator::changedGreeting()
106    {
107        this->bGreetingFlareVisible_ = this->bGreeting_;
108        this->changedFlareVisibility();
109    }
110
111    void Spectator::changedFlareVisibility()
112    {
[3034]113        if (this->greetingFlare_)
[2662]114            this->greetingFlare_->setVisible(this->bGreetingFlareVisible_);
[2072]115    }
116
117    void Spectator::tick(float dt)
118    {
[2662]119        if (this->hasLocalController())
[2072]120        {
[2662]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_;
[2072]127
[2662]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;
[2072]135
[2662]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
[2072]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
[5929]153//        this->setSyncMode(ObjectDirection::ToClient);
[2072]154    }
155
[7862]156    /**
[7863]157        @brief Changes the behavior of some console commands.
[7862]158    */
[2662]159    void Spectator::startLocalHumanControl()
[2072]160    {
[2662]161        ControllableEntity::startLocalHumanControl();
[7862]162
163        // change keybind mode of fire command to OnPress to avoid firing after respawn
164        ModifyConsoleCommand(__CC_fire_name).keybindMode(KeybindMode::OnPress);
[7863]165
166        // disable suicide
167        ModifyConsoleCommand(__CC_suicide_name).pushFunction(&prototype::void__void);
[2072]168    }
169
[7862]170    /**
[7863]171        @brief Changes the behavior of some console commands back to the original state.
[7862]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);
[7863]180
181        // enable suicide
182        ModifyConsoleCommand(__CC_suicide_name).popFunction();
[7862]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
[2072]195    void Spectator::moveFrontBack(const Vector2& value)
196    {
[2662]197        this->localVelocity_.z -= value.x;
[2072]198    }
199
200    void Spectator::moveRightLeft(const Vector2& value)
201    {
[2662]202        this->localVelocity_.x += value.x;
[2072]203    }
204
205    void Spectator::moveUpDown(const Vector2& value)
206    {
[2662]207        this->localVelocity_.y += value.x;
[2072]208    }
209
210    void Spectator::rotateYaw(const Vector2& value)
211    {
[3039]212        this->yaw_ += value.y;
[2662]213
214        ControllableEntity::rotateYaw(value);
[2072]215    }
216
217    void Spectator::rotatePitch(const Vector2& value)
218    {
[2662]219        this->pitch_ += value.y;
220
221        ControllableEntity::rotatePitch(value);
[2072]222    }
223
224    void Spectator::rotateRoll(const Vector2& value)
225    {
[2662]226        this->roll_ += value.y;
227
228        ControllableEntity::rotateRoll(value);
[2072]229    }
230
[6417]231    void Spectator::fired(unsigned int firemode)
[2072]232    {
233        if (this->getPlayer())
234            this->getPlayer()->setReadyToSpawn(true);
235    }
236
237    void Spectator::greet()
238    {
239        this->bGreeting_ = !this->bGreeting_;
240
[2896]241        if (GameMode::isMaster())
[2072]242        {
243            this->bGreetingFlareVisible_ = this->bGreeting_;
244            this->changedFlareVisibility();
245        }
246    }
247}
Note: See TracBrowser for help on using the repository browser.