Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/pawns/Spectator.cc @ 2425

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