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