[2825] | 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 "PongBall.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
[2896] | 32 | #include "core/GameMode.h" |
---|
[5735] | 33 | #include "gametypes/Gametype.h" |
---|
[5725] | 34 | #include "PongBat.h" |
---|
[3108] | 35 | #include "sound/SoundBase.h" |
---|
[2825] | 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | CreateFactory(PongBall); |
---|
| 40 | |
---|
[2885] | 41 | const float PongBall::MAX_REL_Z_VELOCITY = 1.5; |
---|
| 42 | |
---|
[5881] | 43 | PongBall::PongBall(BaseObject* creator) |
---|
| 44 | : MovableEntity(creator) |
---|
| 45 | , sidesound_(NULL) |
---|
| 46 | , batsound_(NULL) |
---|
| 47 | , scoresound_(NULL) |
---|
[2825] | 48 | { |
---|
| 49 | RegisterObject(PongBall); |
---|
| 50 | |
---|
| 51 | this->speed_ = 0; |
---|
| 52 | this->bat_ = 0; |
---|
[3084] | 53 | this->batID_ = new unsigned int[2]; |
---|
| 54 | this->batID_[0] = OBJECTID_UNKNOWN; |
---|
| 55 | this->batID_[1] = OBJECTID_UNKNOWN; |
---|
[3196] | 56 | this->relMercyOffset_ = 0.05f; |
---|
[3108] | 57 | |
---|
[3084] | 58 | this->registerVariables(); |
---|
[3108] | 59 | |
---|
[5881] | 60 | if (GameMode::playsSound()) |
---|
| 61 | { |
---|
| 62 | this->sidesound_ = new SoundBase(this); |
---|
| 63 | this->sidesound_->loadFile("sounds/pong_side.wav"); |
---|
[3108] | 64 | |
---|
[5881] | 65 | this->batsound_ = new SoundBase(this); |
---|
| 66 | this->batsound_->loadFile("sounds/pong_bat.wav"); |
---|
[3108] | 67 | |
---|
[5881] | 68 | this->scoresound_ = new SoundBase(this); |
---|
| 69 | this->scoresound_->loadFile("sounds/pong_score.wav"); |
---|
| 70 | } |
---|
[2825] | 71 | } |
---|
[3108] | 72 | |
---|
[5881] | 73 | PongBall::~PongBall() |
---|
| 74 | { |
---|
| 75 | if (this->sidesound_) |
---|
| 76 | delete this->sidesound_; |
---|
| 77 | if (this->batsound_) |
---|
| 78 | delete this->batsound_; |
---|
| 79 | if (this->scoresound_) |
---|
| 80 | delete this->scoresound_; |
---|
| 81 | } |
---|
| 82 | |
---|
[3084] | 83 | void PongBall::registerVariables() |
---|
| 84 | { |
---|
| 85 | registerVariable( this->fieldWidth_ ); |
---|
| 86 | registerVariable( this->fieldHeight_ ); |
---|
| 87 | registerVariable( this->batlength_ ); |
---|
| 88 | registerVariable( this->speed_ ); |
---|
| 89 | registerVariable( this->relMercyOffset_ ); |
---|
| 90 | registerVariable( this->batID_[0] ); |
---|
[3280] | 91 | registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) ); |
---|
[3084] | 92 | } |
---|
[2825] | 93 | |
---|
| 94 | void PongBall::tick(float dt) |
---|
| 95 | { |
---|
| 96 | SUPER(PongBall, tick, dt); |
---|
| 97 | |
---|
[2896] | 98 | if (GameMode::isMaster()) |
---|
[2825] | 99 | { |
---|
| 100 | Vector3 position = this->getPosition(); |
---|
| 101 | Vector3 velocity = this->getVelocity(); |
---|
| 102 | |
---|
| 103 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
| 104 | { |
---|
| 105 | velocity.z = -velocity.z; |
---|
[5881] | 106 | if (GameMode::playsSound()) |
---|
| 107 | this->sidesound_->play(); |
---|
[2825] | 108 | |
---|
| 109 | if (position.z > this->fieldHeight_ / 2) |
---|
| 110 | position.z = this->fieldHeight_ / 2; |
---|
| 111 | if (position.z < -this->fieldHeight_ / 2) |
---|
| 112 | position.z = -this->fieldHeight_ / 2; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
| 116 | { |
---|
| 117 | float distance = 0; |
---|
| 118 | |
---|
[2839] | 119 | if (this->bat_) |
---|
[2825] | 120 | { |
---|
[2839] | 121 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
[2825] | 122 | { |
---|
[3196] | 123 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
[2839] | 124 | if (fabs(distance) <= 1) |
---|
[2825] | 125 | { |
---|
[2839] | 126 | position.x = this->fieldWidth_ / 2; |
---|
| 127 | velocity.x = -velocity.x; |
---|
[2885] | 128 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
[5881] | 129 | if (GameMode::playsSound()) |
---|
| 130 | this->batsound_->play(); |
---|
[2825] | 131 | } |
---|
[2839] | 132 | else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 133 | { |
---|
| 134 | if (this->getGametype() && this->bat_[0]) |
---|
| 135 | { |
---|
| 136 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
[5881] | 137 | if (GameMode::playsSound()) |
---|
| 138 | this->scoresound_->play(); |
---|
[2839] | 139 | return; |
---|
| 140 | } |
---|
| 141 | } |
---|
[2825] | 142 | } |
---|
[2839] | 143 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
[2825] | 144 | { |
---|
[3196] | 145 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
[2839] | 146 | if (fabs(distance) <= 1) |
---|
[2825] | 147 | { |
---|
[2839] | 148 | position.x = -this->fieldWidth_ / 2; |
---|
| 149 | velocity.x = -velocity.x; |
---|
[2885] | 150 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
[5881] | 151 | if (GameMode::playsSound()) |
---|
| 152 | this->batsound_->play(); |
---|
[2825] | 153 | } |
---|
[2839] | 154 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 155 | { |
---|
| 156 | if (this->getGametype() && this->bat_[1]) |
---|
| 157 | { |
---|
[5881] | 158 | if (GameMode::playsSound()) |
---|
| 159 | this->scoresound_->play(); |
---|
[2839] | 160 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | } |
---|
[2825] | 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if (velocity != this->getVelocity()) |
---|
| 169 | this->setVelocity(velocity); |
---|
| 170 | if (position != this->getPosition()) |
---|
| 171 | this->setPosition(position); |
---|
| 172 | } |
---|
[3084] | 173 | else |
---|
| 174 | { |
---|
| 175 | Vector3 position = this->getPosition(); |
---|
| 176 | Vector3 velocity = this->getVelocity(); |
---|
| 177 | |
---|
| 178 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
| 179 | { |
---|
| 180 | velocity.z = -velocity.z; |
---|
[5881] | 181 | if (GameMode::playsSound()) |
---|
| 182 | this->sidesound_->play(); |
---|
[3084] | 183 | |
---|
| 184 | if (position.z > this->fieldHeight_ / 2) |
---|
| 185 | position.z = this->fieldHeight_ / 2; |
---|
| 186 | if (position.z < -this->fieldHeight_ / 2) |
---|
| 187 | position.z = -this->fieldHeight_ / 2; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
| 191 | { |
---|
| 192 | float distance = 0; |
---|
| 193 | |
---|
| 194 | if (this->bat_) |
---|
| 195 | { |
---|
| 196 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
| 197 | { |
---|
[3196] | 198 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
[3084] | 199 | if (fabs(distance) <= 1) |
---|
| 200 | { |
---|
| 201 | position.x = this->fieldWidth_ / 2; |
---|
| 202 | velocity.x = -velocity.x; |
---|
[5881] | 203 | if (GameMode::playsSound()) |
---|
| 204 | this->batsound_->play(); |
---|
[3084] | 205 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
| 209 | { |
---|
[3196] | 210 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
[3084] | 211 | if (fabs(distance) <= 1) |
---|
| 212 | { |
---|
| 213 | position.x = -this->fieldWidth_ / 2; |
---|
| 214 | velocity.x = -velocity.x; |
---|
[5881] | 215 | if (GameMode::playsSound()) |
---|
| 216 | this->batsound_->play(); |
---|
[3084] | 217 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | if (velocity != this->getVelocity()) |
---|
| 224 | this->setVelocity(velocity); |
---|
| 225 | if (position != this->getPosition()) |
---|
| 226 | this->setPosition(position); |
---|
| 227 | } |
---|
[2825] | 228 | } |
---|
| 229 | |
---|
| 230 | void PongBall::setSpeed(float speed) |
---|
| 231 | { |
---|
| 232 | if (speed != this->speed_) |
---|
| 233 | { |
---|
| 234 | this->speed_ = speed; |
---|
| 235 | |
---|
| 236 | Vector3 velocity = this->getVelocity(); |
---|
| 237 | if (velocity.x != 0) |
---|
| 238 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
| 239 | else |
---|
| 240 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
| 241 | |
---|
| 242 | this->setVelocity(velocity); |
---|
| 243 | } |
---|
| 244 | } |
---|
[3196] | 245 | |
---|
| 246 | void PongBall::setBats(PongBat** bats) |
---|
| 247 | { |
---|
| 248 | this->bat_ = bats; |
---|
| 249 | this->batID_[0] = this->bat_[0]->getObjectID(); |
---|
| 250 | this->batID_[1] = this->bat_[1]->getObjectID(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | void PongBall::applyBats() |
---|
| 254 | { |
---|
| 255 | if (!this->bat_) |
---|
| 256 | this->bat_ = new PongBat*[2]; |
---|
| 257 | if (this->batID_[0] != OBJECTID_UNKNOWN) |
---|
[3325] | 258 | this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0])); |
---|
[3196] | 259 | if (this->batID_[1] != OBJECTID_UNKNOWN) |
---|
[3325] | 260 | this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1])); |
---|
[3196] | 261 | } |
---|
[2825] | 262 | } |
---|