Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/pawns/Spectator.cc @ 2480

Last change on this file since 2480 was 2480, checked in by scheusso, 15 years ago

another small fix according to dedicated server

  • Property svn:eol-style set to native
File size: 8.1 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 "OrxonoxStableHeaders.h"
30#include "Spectator.h"
31
32#include <OgreBillboardSet.h>
33
34#include "core/CoreIncludes.h"
35#include "core/Core.h"
36#include "objects/worldentities/Model.h"
37#include "objects/Scene.h"
38#include "objects/infos/PlayerInfo.h"
39#include "objects/gametypes/Gametype.h"
40#include "tools/BillboardSet.h"
41#include "overlays/OverlayText.h"
42#include "overlays/OverlayGroup.h"
43#include "util/Convert.h"
44
45namespace orxonox
46{
47    CreateFactory(Spectator);
48
49    Spectator::Spectator(BaseObject* creator) : ControllableEntity(creator)
50    {
51        RegisterObject(Spectator);
52
53        this->speed_ = 100;
54        this->rotationGain_ = 3;
55
56        this->yaw_ = 0;
57        this->pitch_ = 0;
58        this->roll_ = 0;
59        this->localVelocity_ = Vector3::ZERO;
60        this->setHudTemplate("spectatorhud");
61        this->hudmode_ = 0;
62
63        this->setDestroyWhenPlayerLeft(true);
64
65        if ( Core::showsGraphics() )
66        {
67            this->greetingFlare_ = new BillboardSet();
68            this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 1.0, 0.8), Vector3(0, 20, 0), 1);
69            if (this->greetingFlare_->getBillboardSet())
70                this->attachOgreObject(this->greetingFlare_->getBillboardSet());
71            this->greetingFlare_->setVisible(false);
72        }
73        else
74            this->greetingFlare_ = 0;
75        this->bGreetingFlareVisible_ = false;
76        this->bGreeting_ = false;
77
78        this->registerVariables();
79    }
80
81    Spectator::~Spectator()
82    {
83        if (this->isInitialized())
84        {
85            if (this->greetingFlare_)
86            {
87                if (this->greetingFlare_->getBillboardSet())
88                    this->detachOgreObject(this->greetingFlare_->getBillboardSet());
89                delete this->greetingFlare_;
90            }
91        }
92    }
93
94    void Spectator::registerVariables()
95    {
96        registerVariable(this->bGreetingFlareVisible_, variableDirection::toclient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility));
97        registerVariable(this->bGreeting_,             variableDirection::toserver, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting));
98        registerVariable(this->hudmode_,               variableDirection::toclient);
99    }
100
101    void Spectator::changedGreeting()
102    {
103        this->bGreetingFlareVisible_ = this->bGreeting_;
104        this->changedFlareVisibility();
105    }
106
107    void Spectator::changedFlareVisibility()
108    {
109        if ( this->greetingFlare_ )             
110            this->greetingFlare_->setVisible(this->bGreetingFlareVisible_);
111    }
112
113    void Spectator::tick(float dt)
114    {
115        this->updateHUD();
116
117        if (this->isLocallyControlled())
118        {
119            float localSpeedSquared = this->localVelocity_.squaredLength();
120            float localSpeed;
121            if (localSpeedSquared > 1.0)
122                localSpeed = this->speed_ / sqrtf(localSpeedSquared);
123            else
124                localSpeed = this->speed_;
125
126            this->localVelocity_.x *= localSpeed;
127            this->localVelocity_.y *= localSpeed;
128            this->localVelocity_.z *= localSpeed;
129            this->setVelocity(this->getOrientation() * this->localVelocity_);
130            this->localVelocity_.x = 0;
131            this->localVelocity_.y = 0;
132            this->localVelocity_.z = 0;
133
134            this->yaw  (Radian(this->yaw_   * this->rotationGain_));
135            this->pitch(Radian(this->pitch_ * this->rotationGain_));
136            this->roll (Radian(this->roll_  * this->rotationGain_));
137
138            this->yaw_ = this->pitch_ = this->roll_ = 0;
139        }
140
141        SUPER(Spectator, tick, dt);
142    }
143
144    void Spectator::setPlayer(PlayerInfo* player)
145    {
146        ControllableEntity::setPlayer(player);
147
148//        this->setObjectMode(direction::toclient);
149    }
150
151    void Spectator::startLocalControl()
152    {
153        ControllableEntity::startLocalControl();
154//        if (this->isLocallyControlled())
155//            this->testmesh_->setVisible(false);
156    }
157
158    void Spectator::moveFrontBack(const Vector2& value)
159    {
160        this->localVelocity_.z -= value.x;
161    }
162
163    void Spectator::moveRightLeft(const Vector2& value)
164    {
165        this->localVelocity_.x += value.x;
166    }
167
168    void Spectator::moveUpDown(const Vector2& value)
169    {
170        this->localVelocity_.y += value.x;
171    }
172
173    void Spectator::rotateYaw(const Vector2& value)
174    {
175        this->yaw_ += value.y;
176    }
177
178    void Spectator::rotatePitch(const Vector2& value)
179    {
180        this->pitch_ += value.y;
181    }
182
183    void Spectator::rotateRoll(const Vector2& value)
184    {
185        this->roll_ += value.y;
186    }
187
188    void Spectator::fire()
189    {
190        if (this->getPlayer())
191            this->getPlayer()->setReadyToSpawn(true);
192    }
193
194    void Spectator::greet()
195    {
196        this->bGreeting_ = !this->bGreeting_;
197
198        if (Core::isMaster())
199        {
200            this->bGreetingFlareVisible_ = this->bGreeting_;
201            this->changedFlareVisibility();
202        }
203    }
204
205    void Spectator::updateHUD()
206    {
207        // <hack>
208        if (Core::isMaster())
209        {
210            if (this->getPlayer() && this->getGametype())
211            {
212                if (!this->getGametype()->hasStarted() && !this->getGametype()->isStartCountdownRunning())
213                {
214                    if (!this->getPlayer()->isReadyToSpawn())
215                        this->hudmode_ = 0;
216                    else
217                        this->hudmode_ = 1;
218                }
219                else if (!this->getGametype()->hasEnded())
220                {
221                    if (this->getGametype()->isStartCountdownRunning())
222                        this->hudmode_ = 2 + 10*(int)ceil(this->getGametype()->getStartCountdown());
223                    else
224                        this->hudmode_ = 3;
225                }
226                else
227                    this->hudmode_ = 4;
228            }
229            else
230                return;
231        }
232
233        if (this->getHUD())
234        {
235            std::string text;
236            int hudmode = this->hudmode_ % 10;
237
238            switch (hudmode)
239            {
240                case 0:
241                    text = "Press [Fire] to start the match";
242                    break;
243                case 1:
244                    text = "Waiting for other players";
245                    break;
246                case 2:
247                    text = convertToString((this->hudmode_ - 2) / 10);
248                    break;
249                case 3:
250                    text = "Press [Fire] to respawn";
251                    break;
252                case 4:
253                    text = "Game has ended";
254                    break;
255                default:;
256            }
257
258            std::map<std::string, OrxonoxOverlay*>::const_iterator it = this->getHUD()->getOverlays().begin();
259            for (; it != this->getHUD()->getOverlays().end(); ++it)
260            {
261                if (it->second->isA(Class(OverlayText)) && it->second->getName() == "state")
262                {
263                    OverlayText* overlay = dynamic_cast<OverlayText*>(it->second);
264                    if (overlay)
265                        overlay->setCaption(text);
266                    break;
267                }
268            }
269        }
270        // </hack>
271    }
272}
Note: See TracBrowser for help on using the repository browser.