[12210] | 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 | /** |
---|
| 30 | @file OrxoBloxBall.cc |
---|
| 31 | @brief Implementation of the OrxoBloxBall class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "OrxoBloxBall.h" |
---|
[12310] | 35 | #include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> |
---|
[12210] | 36 | |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/GameMode.h" |
---|
| 39 | |
---|
| 40 | #include "gametypes/Gametype.h" |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "sound/WorldSound.h" |
---|
| 44 | #include "core/XMLPort.h" |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | RegisterClass(OrxoBloxBall); |
---|
| 49 | |
---|
| 50 | const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Constructor. Registers and initializes the object. |
---|
| 55 | */ |
---|
| 56 | OrxoBloxBall::OrxoBloxBall(Context* context) |
---|
| 57 | : MovableEntity(context) |
---|
| 58 | { |
---|
| 59 | RegisterObject(OrxoBloxBall); |
---|
| 60 | |
---|
| 61 | this->speed_ = 0; |
---|
| 62 | this->accelerationFactor_ = 1.0f; |
---|
[12212] | 63 | this->bDeleteBats_ = false; |
---|
| 64 | this->relMercyOffset_ = 0.05f; |
---|
[12210] | 65 | |
---|
| 66 | this->registerVariables(); |
---|
| 67 | |
---|
| 68 | //initialize sound |
---|
| 69 | if (GameMode::isMaster()) |
---|
| 70 | { |
---|
| 71 | this->defScoreSound_ = new WorldSound(this->getContext()); |
---|
| 72 | this->defScoreSound_->setVolume(1.0f); |
---|
| 73 | this->defBatSound_ = new WorldSound(this->getContext()); |
---|
| 74 | this->defBatSound_->setVolume(0.4f); |
---|
| 75 | this->defBoundarySound_ = new WorldSound(this->getContext()); |
---|
| 76 | this->defBoundarySound_->setVolume(0.5f); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | this->defScoreSound_ = nullptr; |
---|
| 81 | this->defBatSound_ = nullptr; |
---|
| 82 | this->defBoundarySound_ = nullptr; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief |
---|
| 88 | Destructor. |
---|
| 89 | */ |
---|
| 90 | OrxoBloxBall::~OrxoBloxBall() |
---|
| 91 | { |
---|
| 92 | if (this->isInitialized()) |
---|
| 93 | { |
---|
[12212] | 94 | if (this->bDeleteBats_) |
---|
[12210] | 95 | |
---|
[12212] | 96 | delete[] this->batID_; |
---|
[12210] | 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | //xml port for loading sounds |
---|
| 101 | void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 102 | { |
---|
| 103 | SUPER(OrxoBloxBall, XMLPort, xmlelement, mode); |
---|
| 104 | XMLPortParam(OrxoBloxBall, "defScoreSound", setDefScoreSound, getDefScoreSound, xmlelement, mode); |
---|
| 105 | XMLPortParam(OrxoBloxBall, "defBatSound", setDefBatSound, getDefBatSound, xmlelement, mode); |
---|
| 106 | XMLPortParam(OrxoBloxBall, "defBoundarySound", setDefBoundarySound, getDefBoundarySound, xmlelement, mode); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief |
---|
| 111 | Register variables to synchronize over the network. |
---|
| 112 | */ |
---|
| 113 | void OrxoBloxBall::registerVariables() |
---|
| 114 | { |
---|
| 115 | registerVariable( this->fieldWidth_ ); |
---|
| 116 | registerVariable( this->fieldHeight_ ); |
---|
[12212] | 117 | registerVariable( this->batlength_ ); |
---|
[12210] | 118 | registerVariable( this->speed_ ); |
---|
[12212] | 119 | registerVariable( this->relMercyOffset_ ); |
---|
[12210] | 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | @brief |
---|
| 124 | Is called every tick. |
---|
[12212] | 125 | Handles the movement of the ball and its interaction with the boundaries and bats. |
---|
[12210] | 126 | @param dt |
---|
| 127 | The time since the last tick. |
---|
| 128 | */ |
---|
| 129 | void OrxoBloxBall::tick(float dt) |
---|
| 130 | { |
---|
| 131 | SUPER(OrxoBloxBall, tick, dt); |
---|
| 132 | |
---|
| 133 | // Get the current position, velocity and acceleration of the ball. |
---|
| 134 | Vector3 position = this->getPosition(); |
---|
| 135 | Vector3 velocity = this->getVelocity(); |
---|
| 136 | Vector3 acceleration = this->getAcceleration(); |
---|
| 137 | |
---|
[12212] | 138 | // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters). |
---|
| 139 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
[12210] | 140 | { |
---|
| 141 | defBoundarySound_->play(); //play boundary sound |
---|
[12212] | 142 | // Its velocity in z-direction is inverted (i.e. it bounces off). |
---|
| 143 | velocity.z = -velocity.z; |
---|
[12210] | 144 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
[12336] | 145 | if (position.z > this->fieldHeight_ / 2){ |
---|
| 146 | ChatManager::message("Ball hat das Feld verlassen"); |
---|
| 147 | //TODO: ball zerstoeren |
---|
| 148 | } |
---|
| 149 | if (position.z < -this->fieldHeight_ / 2){ |
---|
[12212] | 150 | position.z = -this->fieldHeight_ / 2; |
---|
[12336] | 151 | |
---|
| 152 | } |
---|
[12210] | 153 | |
---|
| 154 | this->fireEvent(); |
---|
| 155 | } |
---|
[12294] | 156 | |
---|
| 157 | //Ball hits the right or left wall and should bounce back. |
---|
| 158 | // If the ball has crossed the left or right boundary of the playing field. |
---|
[12212] | 159 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
[12210] | 160 | { |
---|
[12294] | 161 | //Ball hits the right Wall |
---|
| 162 | if (position.x > this->fieldWidth_ / 2) |
---|
[12212] | 163 | { |
---|
[12294] | 164 | // Set the ball to be exactly at the boundary. |
---|
| 165 | position.x = this->fieldWidth_ / 2; |
---|
| 166 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 167 | velocity.x = -velocity.x; |
---|
| 168 | this->fireEvent(); |
---|
[12212] | 169 | } |
---|
[12278] | 170 | |
---|
[12294] | 171 | //Ball hits the left wall |
---|
| 172 | else if (position.x < -this->fieldWidth_ / 2) |
---|
[12212] | 173 | { |
---|
| 174 | // Set the ball to be exactly at the boundary. |
---|
| 175 | position.x = -this->fieldWidth_ / 2; |
---|
| 176 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 177 | velocity.x = -velocity.x; |
---|
| 178 | this->fireEvent(); |
---|
| 179 | } |
---|
[12210] | 180 | } |
---|
| 181 | |
---|
| 182 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
| 183 | if (acceleration != this->getAcceleration()) |
---|
| 184 | this->setAcceleration(acceleration); |
---|
| 185 | if (velocity != this->getVelocity()) |
---|
| 186 | this->setVelocity(velocity); |
---|
| 187 | if (position != this->getPosition()) |
---|
| 188 | this->setPosition(position); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | @brief |
---|
| 193 | Set the speed of the ball (in x-direction). |
---|
| 194 | @param speed |
---|
| 195 | The speed to be set. |
---|
| 196 | */ |
---|
| 197 | void OrxoBloxBall::setSpeed(float speed) |
---|
| 198 | { |
---|
| 199 | if (speed != this->speed_) // If the speed changes |
---|
| 200 | { |
---|
| 201 | this->speed_ = speed; |
---|
| 202 | |
---|
| 203 | // Set the speed in the direction of the balls current velocity. |
---|
| 204 | Vector3 velocity = this->getVelocity(); |
---|
| 205 | if (velocity.x != 0) |
---|
| 206 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
| 207 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
| 208 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
[12305] | 209 | //velocity.y = this->speed_; |
---|
| 210 | velocity.z = this->speed_; |
---|
[12210] | 211 | |
---|
| 212 | this->setVelocity(velocity); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[12212] | 217 | void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound) |
---|
[12210] | 218 | { |
---|
| 219 | if( defScoreSound_ ) |
---|
[12212] | 220 | defScoreSound_->setSource(OrxoBloxSound); |
---|
[12210] | 221 | else |
---|
| 222 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | const std::string& OrxoBloxBall::getDefScoreSound() |
---|
| 226 | { |
---|
| 227 | if( defScoreSound_ ) |
---|
| 228 | return defScoreSound_->getSource(); |
---|
| 229 | else |
---|
| 230 | assert(0); |
---|
| 231 | return BLANKSTRING; |
---|
| 232 | } |
---|
| 233 | |
---|
[12212] | 234 | void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound) |
---|
[12210] | 235 | { |
---|
| 236 | if( defBatSound_ ) |
---|
[12212] | 237 | defBatSound_->setSource(OrxoBloxSound); |
---|
[12210] | 238 | else |
---|
| 239 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | const std::string& OrxoBloxBall::getDefBatSound() |
---|
| 243 | { |
---|
| 244 | if( defBatSound_ ) |
---|
| 245 | return defBatSound_->getSource(); |
---|
| 246 | else |
---|
| 247 | assert(0); |
---|
| 248 | return BLANKSTRING; |
---|
| 249 | } |
---|
| 250 | |
---|
[12212] | 251 | void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound) |
---|
[12210] | 252 | { |
---|
| 253 | if( defBoundarySound_ ) |
---|
[12212] | 254 | defBoundarySound_->setSource(OrxoBloxSound); |
---|
[12210] | 255 | else |
---|
| 256 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | const std::string& OrxoBloxBall::getDefBoundarySound() |
---|
| 260 | { |
---|
| 261 | if( defBoundarySound_ ) |
---|
| 262 | return defBoundarySound_->getSource(); |
---|
| 263 | else |
---|
| 264 | assert(0); |
---|
| 265 | return BLANKSTRING; |
---|
| 266 | } |
---|
[12310] | 267 | |
---|
| 268 | |
---|
| 269 | void OrxoBloxBall::Bounce(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) { |
---|
| 270 | |
---|
| 271 | Vector3 velocity = this->getVelocity(); |
---|
| 272 | Vector3 myPosition = otherObject->getPosition(); |
---|
| 273 | btVector3 positionOtherObject = contactPoint.getPositionWorldOnA(); |
---|
| 274 | orxout() << "About to Bounce >D" << endl; |
---|
| 275 | //if (positionOtherObject.y < 0) { |
---|
| 276 | //this.destroy() |
---|
| 277 | //}S |
---|
| 278 | //else { |
---|
| 279 | |
---|
| 280 | int distance_X = positionOtherObject.getX() - myPosition.x; |
---|
| 281 | int distance_Z = positionOtherObject.getZ() - myPosition.z; |
---|
| 282 | |
---|
| 283 | if (distance_X < 0) |
---|
| 284 | distance_X = -distance_X; |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | if (distance_Z < 0) |
---|
| 288 | distance_Z = -distance_Z; |
---|
| 289 | |
---|
| 290 | orxout() << distance_X << endl; |
---|
| 291 | orxout() << distance_Z << endl; |
---|
| 292 | |
---|
| 293 | if (distance_X < distance_Z) { |
---|
| 294 | velocity.z = -velocity.z; |
---|
| 295 | orxout() << "z" << endl; |
---|
| 296 | } |
---|
| 297 | if (distance_Z < distance_X) { |
---|
| 298 | velocity.x = -velocity.x; |
---|
| 299 | orxout() << "x" << endl; |
---|
| 300 | } |
---|
| 301 | else { |
---|
| 302 | velocity.x = -velocity.x; |
---|
| 303 | velocity.z = -velocity.z; |
---|
| 304 | orxout() << "both" << endl; |
---|
| 305 | } |
---|
| 306 | this->setVelocity(velocity); |
---|
| 307 | //} |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | bool OrxoBloxBall::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) |
---|
| 312 | { |
---|
| 313 | orxout() << "About to Bounce >D" << endl; |
---|
| 314 | bool result = MovableEntity::collidesAgainst(otherObject, ownCollisionShape, contactPoint); |
---|
| 315 | Bounce(otherObject, ownCollisionShape, contactPoint); |
---|
| 316 | return result; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | |
---|
[12210] | 320 | } |
---|