[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "PongBall.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
[2896] | 33 | #include "core/GameMode.h" |
---|
[2825] | 34 | #include "objects/gametypes/Gametype.h" |
---|
| 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; |
---|
[3084] | 52 | |
---|
| 53 | this->registerVariables(); |
---|
[2825] | 54 | } |
---|
[3084] | 55 | |
---|
| 56 | void PongBall::registerVariables() |
---|
| 57 | { |
---|
| 58 | registerVariable( this->fieldWidth_ ); |
---|
| 59 | registerVariable( this->fieldHeight_ ); |
---|
| 60 | registerVariable( this->batlength_ ); |
---|
| 61 | registerVariable( this->speed_ ); |
---|
| 62 | registerVariable( this->relMercyOffset_ ); |
---|
| 63 | registerVariable( this->batID_[0] ); |
---|
| 64 | registerVariable( this->batID_[1], variableDirection::toclient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) ); |
---|
| 65 | } |
---|
[2825] | 66 | |
---|
| 67 | void PongBall::tick(float dt) |
---|
| 68 | { |
---|
| 69 | SUPER(PongBall, tick, dt); |
---|
| 70 | |
---|
[2896] | 71 | if (GameMode::isMaster()) |
---|
[2825] | 72 | { |
---|
| 73 | Vector3 position = this->getPosition(); |
---|
| 74 | Vector3 velocity = this->getVelocity(); |
---|
| 75 | |
---|
| 76 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
| 77 | { |
---|
| 78 | velocity.z = -velocity.z; |
---|
| 79 | |
---|
| 80 | if (position.z > this->fieldHeight_ / 2) |
---|
| 81 | position.z = this->fieldHeight_ / 2; |
---|
| 82 | if (position.z < -this->fieldHeight_ / 2) |
---|
| 83 | position.z = -this->fieldHeight_ / 2; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
| 87 | { |
---|
| 88 | float distance = 0; |
---|
| 89 | |
---|
[2839] | 90 | if (this->bat_) |
---|
[2825] | 91 | { |
---|
[2839] | 92 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
[2825] | 93 | { |
---|
[2839] | 94 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
| 95 | if (fabs(distance) <= 1) |
---|
[2825] | 96 | { |
---|
[2839] | 97 | position.x = this->fieldWidth_ / 2; |
---|
| 98 | velocity.x = -velocity.x; |
---|
[2885] | 99 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
[2825] | 100 | } |
---|
[2839] | 101 | else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 102 | { |
---|
| 103 | if (this->getGametype() && this->bat_[0]) |
---|
| 104 | { |
---|
| 105 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
| 106 | return; |
---|
| 107 | } |
---|
| 108 | } |
---|
[2825] | 109 | } |
---|
[2839] | 110 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
[2825] | 111 | { |
---|
[2839] | 112 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
| 113 | if (fabs(distance) <= 1) |
---|
[2825] | 114 | { |
---|
[2839] | 115 | position.x = -this->fieldWidth_ / 2; |
---|
| 116 | velocity.x = -velocity.x; |
---|
[2885] | 117 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
[2825] | 118 | } |
---|
[2839] | 119 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 120 | { |
---|
| 121 | if (this->getGametype() && this->bat_[1]) |
---|
| 122 | { |
---|
| 123 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
| 124 | return; |
---|
| 125 | } |
---|
| 126 | } |
---|
[2825] | 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (velocity != this->getVelocity()) |
---|
| 132 | this->setVelocity(velocity); |
---|
| 133 | if (position != this->getPosition()) |
---|
| 134 | this->setPosition(position); |
---|
| 135 | } |
---|
[3084] | 136 | else |
---|
| 137 | { |
---|
| 138 | Vector3 position = this->getPosition(); |
---|
| 139 | Vector3 velocity = this->getVelocity(); |
---|
| 140 | |
---|
| 141 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
| 142 | { |
---|
| 143 | velocity.z = -velocity.z; |
---|
| 144 | |
---|
| 145 | if (position.z > this->fieldHeight_ / 2) |
---|
| 146 | position.z = this->fieldHeight_ / 2; |
---|
| 147 | if (position.z < -this->fieldHeight_ / 2) |
---|
| 148 | position.z = -this->fieldHeight_ / 2; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
| 152 | { |
---|
| 153 | float distance = 0; |
---|
| 154 | |
---|
| 155 | if (this->bat_) |
---|
| 156 | { |
---|
| 157 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
| 158 | { |
---|
| 159 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
| 160 | if (fabs(distance) <= 1) |
---|
| 161 | { |
---|
| 162 | position.x = this->fieldWidth_ / 2; |
---|
| 163 | velocity.x = -velocity.x; |
---|
| 164 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
| 168 | { |
---|
| 169 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
---|
| 170 | if (fabs(distance) <= 1) |
---|
| 171 | { |
---|
| 172 | position.x = -this->fieldWidth_ / 2; |
---|
| 173 | velocity.x = -velocity.x; |
---|
| 174 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if (velocity != this->getVelocity()) |
---|
| 181 | this->setVelocity(velocity); |
---|
| 182 | if (position != this->getPosition()) |
---|
| 183 | this->setPosition(position); |
---|
| 184 | } |
---|
[2825] | 185 | } |
---|
| 186 | |
---|
| 187 | void PongBall::setSpeed(float speed) |
---|
| 188 | { |
---|
| 189 | if (speed != this->speed_) |
---|
| 190 | { |
---|
| 191 | this->speed_ = speed; |
---|
| 192 | |
---|
| 193 | Vector3 velocity = this->getVelocity(); |
---|
| 194 | if (velocity.x != 0) |
---|
| 195 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
| 196 | else |
---|
| 197 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
| 198 | |
---|
| 199 | this->setVelocity(velocity); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|