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