[2072] | 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 | |
---|
| 43 | namespace 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); |
---|
[2171] | 64 | if (this->greetingFlare_->getBillboardSet()) |
---|
| 65 | this->getNode()->attachObject(this->greetingFlare_->getBillboardSet()); |
---|
[2072] | 66 | this->greetingFlare_->setVisible(false); |
---|
| 67 | this->bGreetingFlareVisible_ = false; |
---|
| 68 | this->bGreeting_ = false; |
---|
| 69 | |
---|
| 70 | this->registerVariables(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | Spectator::~Spectator() |
---|
| 74 | { |
---|
| 75 | if (this->isInitialized()) |
---|
| 76 | { |
---|
| 77 | if (this->greetingFlare_) |
---|
| 78 | { |
---|
[2171] | 79 | if (this->greetingFlare_->getBillboardSet()) |
---|
| 80 | this->getNode()->detachObject(this->greetingFlare_->getBillboardSet()); |
---|
[2072] | 81 | delete this->greetingFlare_; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void Spectator::registerVariables() |
---|
| 87 | { |
---|
[2171] | 88 | REGISTERDATA(this->bGreetingFlareVisible_, direction::toclient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility)); |
---|
| 89 | REGISTERDATA(this->bGreeting_, direction::toserver, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting)); |
---|
| 90 | REGISTERDATA(this->hudmode_, direction::toclient); |
---|
[2072] | 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(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 | |
---|
[2171] | 133 | // this->setObjectMode(direction::toclient); |
---|
[2072] | 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()) |
---|
[2171] | 207 | this->hudmode_ = 2 + 10*(int)ceil(this->getGametype()->getStartCountdown()); |
---|
[2072] | 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 | } |
---|