| 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" |
|---|
| 33 | #include "core/GameMode.h" |
|---|
| 34 | #include "objects/worldentities/PongBat.h" |
|---|
| 35 | #include "objects/gametypes/Gametype.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->relMercyOffset_ = 0.05; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void PongBall::tick(float dt) |
|---|
| 53 | { |
|---|
| 54 | SUPER(PongBall, tick, dt); |
|---|
| 55 | |
|---|
| 56 | if (GameMode::isMaster()) |
|---|
| 57 | { |
|---|
| 58 | Vector3 position = this->getPosition(); |
|---|
| 59 | Vector3 velocity = this->getVelocity(); |
|---|
| 60 | |
|---|
| 61 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
|---|
| 62 | { |
|---|
| 63 | velocity.z = -velocity.z; |
|---|
| 64 | |
|---|
| 65 | if (position.z > this->fieldHeight_ / 2) |
|---|
| 66 | position.z = this->fieldHeight_ / 2; |
|---|
| 67 | if (position.z < -this->fieldHeight_ / 2) |
|---|
| 68 | position.z = -this->fieldHeight_ / 2; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
|---|
| 72 | { |
|---|
| 73 | float distance = 0; |
|---|
| 74 | |
|---|
| 75 | if (this->bat_) |
|---|
| 76 | { |
|---|
| 77 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
|---|
| 78 | { |
|---|
| 79 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
|---|
| 80 | if (fabs(distance) <= 1) |
|---|
| 81 | { |
|---|
| 82 | position.x = this->fieldWidth_ / 2; |
|---|
| 83 | velocity.x = -velocity.x; |
|---|
| 84 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 85 | } |
|---|
| 86 | else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
|---|
| 87 | { |
|---|
| 88 | if (this->getGametype() && this->bat_[0]) |
|---|
| 89 | { |
|---|
| 90 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
|---|
| 91 | return; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
|---|
| 96 | { |
|---|
| 97 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2); |
|---|
| 98 | if (fabs(distance) <= 1) |
|---|
| 99 | { |
|---|
| 100 | position.x = -this->fieldWidth_ / 2; |
|---|
| 101 | velocity.x = -velocity.x; |
|---|
| 102 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
|---|
| 103 | } |
|---|
| 104 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
|---|
| 105 | { |
|---|
| 106 | if (this->getGametype() && this->bat_[1]) |
|---|
| 107 | { |
|---|
| 108 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if (velocity != this->getVelocity()) |
|---|
| 117 | this->setVelocity(velocity); |
|---|
| 118 | if (position != this->getPosition()) |
|---|
| 119 | this->setPosition(position); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void PongBall::setSpeed(float speed) |
|---|
| 124 | { |
|---|
| 125 | if (speed != this->speed_) |
|---|
| 126 | { |
|---|
| 127 | this->speed_ = speed; |
|---|
| 128 | |
|---|
| 129 | Vector3 velocity = this->getVelocity(); |
|---|
| 130 | if (velocity.x != 0) |
|---|
| 131 | velocity.x = sgn(velocity.x) * this->speed_; |
|---|
| 132 | else |
|---|
| 133 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
|---|
| 134 | |
|---|
| 135 | this->setVelocity(velocity); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|