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