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