Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/pawns/Spectator.cc @ 2362

Last change on this file since 2362 was 2362, checked in by landauf, 15 years ago
  • Added Bot (the artifical counterpart to HumanPlayer)
  • Added ArtificialController, the baseclass of all non-human-controllers
  • Added AIController, a simple reimplementation of the old AISpaceShip (but of course without the SpaceShip, just the AI)
  • Added currently empty class ScriptController
  • Some tweaks in PlayerInfo, ControllableEntity and Controller
  • Made Spacetator speed configurable

I don't know where, but there's still an issue in the new code, maybe even caused by the previous commit. The Server seems to crash when a Client disconnects, but only if the Server runs within the debugger.

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