Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/pawns/Spectator.cc @ 2112

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

Test: replacing namespace network with namespace orxonox. network::packet —> orxonox::packet

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