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