Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2024 was 2024, checked in by landauf, 16 years ago

added spaceship

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