Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/pawns/Spectator.cc @ 2442

Last change on this file since 2442 was 2442, checked in by rgrieder, 15 years ago

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

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