| 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" |
|---|
| 32 | #include "core/GameMode.h" |
|---|
| 33 | #include "gametypes/Gametype.h" |
|---|
| 34 | #include "PongBat.h" |
|---|
| 35 | #include "sound/SoundBase.h" |
|---|
| 36 | |
|---|
| 37 | namespace orxonox |
|---|
| 38 | { |
|---|
| 39 | CreateFactory(PongBall); |
|---|
| 40 | |
|---|
| 41 | const float PongBall::MAX_REL_Z_VELOCITY = 1.5; |
|---|
| 42 | |
|---|
| 43 | PongBall::PongBall(BaseObject* creator) : MovableEntity(creator) |
|---|
| 44 | { |
|---|
| 45 | RegisterObject(PongBall); |
|---|
| 46 | |
|---|
| 47 | this->speed_ = 0; |
|---|
| 48 | this->bat_ = 0; |
|---|
| 49 | this->batID_ = new unsigned int[2]; |
|---|
| 50 | this->batID_[0] = OBJECTID_UNKNOWN; |
|---|
| 51 | this->batID_[1] = OBJECTID_UNKNOWN; |
|---|
| 52 | this->relMercyOffset_ = 0.05f; |
|---|
| 53 | |
|---|
| 54 | this->registerVariables(); |
|---|
| 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"); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 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] ); |
|---|
| 74 | registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void PongBall::tick(float dt) |
|---|
| 78 | { |
|---|
| 79 | SUPER(PongBall, tick, dt); |
|---|
| 80 | |
|---|
| 81 | if (GameMode::isMaster()) |
|---|
| 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; |
|---|
| 89 | this->sidesound_->play(); |
|---|
| 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 | |
|---|
| 101 | if (this->bat_) |
|---|
| 102 | { |
|---|
| 103 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
|---|
| 104 | { |
|---|
| 105 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 106 | if (fabs(distance) <= 1) |
|---|
| 107 | { |
|---|
| 108 | position.x = this->fieldWidth_ / 2; |
|---|
| 109 | velocity.x = -velocity.x; |
|---|
| 110 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 111 | this->batsound_->play(); |
|---|
| 112 | } |
|---|
| 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()); |
|---|
| 118 | this->scoresound_->play(); |
|---|
| 119 | return; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
|---|
| 124 | { |
|---|
| 125 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 126 | if (fabs(distance) <= 1) |
|---|
| 127 | { |
|---|
| 128 | position.x = -this->fieldWidth_ / 2; |
|---|
| 129 | velocity.x = -velocity.x; |
|---|
| 130 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 131 | this->batsound_->play(); |
|---|
| 132 | } |
|---|
| 133 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
|---|
| 134 | { |
|---|
| 135 | if (this->getGametype() && this->bat_[1]) |
|---|
| 136 | { |
|---|
| 137 | this->scoresound_->play(); |
|---|
| 138 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
|---|
| 139 | return; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | if (velocity != this->getVelocity()) |
|---|
| 147 | this->setVelocity(velocity); |
|---|
| 148 | if (position != this->getPosition()) |
|---|
| 149 | this->setPosition(position); |
|---|
| 150 | } |
|---|
| 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; |
|---|
| 159 | this->sidesound_->play(); |
|---|
| 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 | { |
|---|
| 175 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 176 | if (fabs(distance) <= 1) |
|---|
| 177 | { |
|---|
| 178 | position.x = this->fieldWidth_ / 2; |
|---|
| 179 | velocity.x = -velocity.x; |
|---|
| 180 | this->batsound_->play(); |
|---|
| 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 | { |
|---|
| 186 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 187 | if (fabs(distance) <= 1) |
|---|
| 188 | { |
|---|
| 189 | position.x = -this->fieldWidth_ / 2; |
|---|
| 190 | velocity.x = -velocity.x; |
|---|
| 191 | this->batsound_->play(); |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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) |
|---|
| 233 | this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0])); |
|---|
| 234 | if (this->batID_[1] != OBJECTID_UNKNOWN) |
|---|
| 235 | this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1])); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|