| [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" |
|---|
| [2825] | 35 | |
|---|
| 36 | namespace orxonox |
|---|
| 37 | { |
|---|
| 38 | CreateFactory(PongBall); |
|---|
| 39 | |
|---|
| [2885] | 40 | const float PongBall::MAX_REL_Z_VELOCITY = 1.5; |
|---|
| 41 | |
|---|
| [5929] | 42 | PongBall::PongBall(BaseObject* creator) |
|---|
| 43 | : MovableEntity(creator) |
|---|
| [2825] | 44 | { |
|---|
| 45 | RegisterObject(PongBall); |
|---|
| 46 | |
|---|
| 47 | this->speed_ = 0; |
|---|
| [5929] | 48 | this->accelerationFactor_ = 1.0f; |
|---|
| [2825] | 49 | this->bat_ = 0; |
|---|
| [3084] | 50 | this->batID_ = new unsigned int[2]; |
|---|
| 51 | this->batID_[0] = OBJECTID_UNKNOWN; |
|---|
| 52 | this->batID_[1] = OBJECTID_UNKNOWN; |
|---|
| [3196] | 53 | this->relMercyOffset_ = 0.05f; |
|---|
| [3108] | 54 | |
|---|
| [3084] | 55 | this->registerVariables(); |
|---|
| [5929] | 56 | } |
|---|
| [3108] | 57 | |
|---|
| [5929] | 58 | PongBall::~PongBall() |
|---|
| 59 | { |
|---|
| [2825] | 60 | } |
|---|
| [3108] | 61 | |
|---|
| [3084] | 62 | void PongBall::registerVariables() |
|---|
| 63 | { |
|---|
| 64 | registerVariable( this->fieldWidth_ ); |
|---|
| 65 | registerVariable( this->fieldHeight_ ); |
|---|
| 66 | registerVariable( this->batlength_ ); |
|---|
| 67 | registerVariable( this->speed_ ); |
|---|
| 68 | registerVariable( this->relMercyOffset_ ); |
|---|
| 69 | registerVariable( this->batID_[0] ); |
|---|
| [3280] | 70 | registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) ); |
|---|
| [3084] | 71 | } |
|---|
| [2825] | 72 | |
|---|
| 73 | void PongBall::tick(float dt) |
|---|
| 74 | { |
|---|
| 75 | SUPER(PongBall, tick, dt); |
|---|
| 76 | |
|---|
| [5929] | 77 | Vector3 position = this->getPosition(); |
|---|
| 78 | Vector3 velocity = this->getVelocity(); |
|---|
| 79 | Vector3 acceleration = this->getAcceleration(); |
|---|
| 80 | |
|---|
| 81 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
|---|
| [2825] | 82 | { |
|---|
| [5929] | 83 | velocity.z = -velocity.z; |
|---|
| 84 | if (position.z > this->fieldHeight_ / 2) |
|---|
| 85 | position.z = this->fieldHeight_ / 2; |
|---|
| 86 | if (position.z < -this->fieldHeight_ / 2) |
|---|
| 87 | position.z = -this->fieldHeight_ / 2; |
|---|
| [2825] | 88 | |
|---|
| [5929] | 89 | this->fireEvent(); |
|---|
| 90 | } |
|---|
| [2825] | 91 | |
|---|
| [5929] | 92 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
|---|
| 93 | { |
|---|
| 94 | float distance = 0; |
|---|
| [2825] | 95 | |
|---|
| [5929] | 96 | if (this->bat_) |
|---|
| [2825] | 97 | { |
|---|
| [5929] | 98 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
|---|
| [2825] | 99 | { |
|---|
| [5929] | 100 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 101 | if (fabs(distance) <= 1) |
|---|
| [2825] | 102 | { |
|---|
| [5929] | 103 | position.x = this->fieldWidth_ / 2; |
|---|
| 104 | velocity.x = -velocity.x; |
|---|
| 105 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 106 | acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; |
|---|
| 107 | |
|---|
| 108 | this->fireEvent(); |
|---|
| 109 | } |
|---|
| 110 | else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
|---|
| 111 | { |
|---|
| 112 | if (this->getGametype() && this->bat_[0]) |
|---|
| [2825] | 113 | { |
|---|
| [5929] | 114 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
|---|
| 115 | return; |
|---|
| [2825] | 116 | } |
|---|
| 117 | } |
|---|
| [5929] | 118 | } |
|---|
| 119 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
|---|
| 120 | { |
|---|
| 121 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
|---|
| 122 | if (fabs(distance) <= 1) |
|---|
| [2825] | 123 | { |
|---|
| [5929] | 124 | position.x = -this->fieldWidth_ / 2; |
|---|
| 125 | velocity.x = -velocity.x; |
|---|
| 126 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 127 | acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; |
|---|
| 128 | |
|---|
| 129 | this->fireEvent(); |
|---|
| 130 | } |
|---|
| 131 | else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
|---|
| 132 | { |
|---|
| 133 | if (this->getGametype() && this->bat_[1]) |
|---|
| [2825] | 134 | { |
|---|
| [5929] | 135 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
|---|
| 136 | return; |
|---|
| [2825] | 137 | } |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| [3084] | 142 | |
|---|
| [5929] | 143 | if (acceleration != this->getAcceleration()) |
|---|
| 144 | this->setAcceleration(acceleration); |
|---|
| 145 | if (velocity != this->getVelocity()) |
|---|
| [3084] | 146 | this->setVelocity(velocity); |
|---|
| [5929] | 147 | if (position != this->getPosition()) |
|---|
| [3084] | 148 | this->setPosition(position); |
|---|
| [2825] | 149 | } |
|---|
| 150 | |
|---|
| 151 | void PongBall::setSpeed(float speed) |
|---|
| 152 | { |
|---|
| 153 | if (speed != this->speed_) |
|---|
| 154 | { |
|---|
| 155 | this->speed_ = speed; |
|---|
| 156 | |
|---|
| 157 | Vector3 velocity = this->getVelocity(); |
|---|
| 158 | if (velocity.x != 0) |
|---|
| 159 | velocity.x = sgn(velocity.x) * this->speed_; |
|---|
| 160 | else |
|---|
| 161 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
|---|
| 162 | |
|---|
| 163 | this->setVelocity(velocity); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| [3196] | 166 | |
|---|
| 167 | void PongBall::setBats(PongBat** bats) |
|---|
| 168 | { |
|---|
| 169 | this->bat_ = bats; |
|---|
| 170 | this->batID_[0] = this->bat_[0]->getObjectID(); |
|---|
| 171 | this->batID_[1] = this->bat_[1]->getObjectID(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void PongBall::applyBats() |
|---|
| 175 | { |
|---|
| 176 | if (!this->bat_) |
|---|
| 177 | this->bat_ = new PongBat*[2]; |
|---|
| 178 | if (this->batID_[0] != OBJECTID_UNKNOWN) |
|---|
| [3325] | 179 | this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0])); |
|---|
| [3196] | 180 | if (this->batID_[1] != OBJECTID_UNKNOWN) |
|---|
| [3325] | 181 | this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1])); |
|---|
| [3196] | 182 | } |
|---|
| [2825] | 183 | } |
|---|