[10017] | 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: |
---|
[10040] | 23 | * Fabian 'x3n' Landau |
---|
[10017] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[10040] | 29 | /** |
---|
| 30 | @file JumpPlatform.cc |
---|
| 31 | @brief Implementation of the JumpPlatform class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[10017] | 34 | #include "JumpPlatform.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/GameMode.h" |
---|
| 38 | |
---|
[10040] | 39 | #include "gametypes/Gametype.h" |
---|
| 40 | |
---|
| 41 | #include "JumpFigure.h" |
---|
| 42 | |
---|
| 43 | #include "sound/WorldSound.h" |
---|
| 44 | #include "core/XMLPort.h" |
---|
| 45 | |
---|
[10017] | 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | RegisterClass(JumpPlatform); |
---|
| 49 | |
---|
[10040] | 50 | const float JumpPlatform::MAX_REL_Z_VELOCITY = 1.5; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Constructor. Registers and initializes the object. |
---|
| 55 | */ |
---|
| 56 | JumpPlatform::JumpPlatform(Context* context) : MovableEntity(context) |
---|
[10017] | 57 | { |
---|
| 58 | RegisterObject(JumpPlatform); |
---|
| 59 | |
---|
[10040] | 60 | this->figure_ = 0; |
---|
| 61 | this->bDeleteBats_ = false; |
---|
| 62 | this->batID_ = new unsigned int[1]; |
---|
| 63 | this->batID_[0] = OBJECTID_UNKNOWN; |
---|
| 64 | this->relMercyOffset_ = 0.05f; |
---|
[10017] | 65 | |
---|
[10040] | 66 | this->registerVariables(); |
---|
[10017] | 67 | |
---|
[10040] | 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_ = 0; |
---|
| 81 | this->defBatSound_ = 0; |
---|
| 82 | this->defBoundarySound_ = 0; |
---|
| 83 | } |
---|
[10017] | 84 | |
---|
[10040] | 85 | this->setPosition(Vector3(0,0,0)); |
---|
| 86 | this->setVelocity(Vector3(0,0,0)); |
---|
| 87 | this->setAcceleration(Vector3(0,0,0)); |
---|
[10032] | 88 | } |
---|
[10017] | 89 | |
---|
[10040] | 90 | /** |
---|
| 91 | @brief |
---|
| 92 | Destructor. |
---|
| 93 | */ |
---|
| 94 | JumpPlatform::~JumpPlatform() |
---|
| 95 | { |
---|
| 96 | if (this->isInitialized()) |
---|
| 97 | { |
---|
| 98 | if (this->bDeleteBats_) |
---|
| 99 | delete this->figure_; |
---|
| 100 | |
---|
| 101 | delete[] this->batID_; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | //xml port for loading sounds |
---|
| 106 | void JumpPlatform::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 107 | { |
---|
| 108 | SUPER(JumpPlatform, XMLPort, xmlelement, mode); |
---|
| 109 | XMLPortParam(JumpPlatform, "defScoreSound", setDefScoreSound, getDefScoreSound, xmlelement, mode); |
---|
| 110 | XMLPortParam(JumpPlatform, "defBatSound", setDefBatSound, getDefBatSound, xmlelement, mode); |
---|
| 111 | XMLPortParam(JumpPlatform, "defBoundarySound", setDefBoundarySound, getDefBoundarySound, xmlelement, mode); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | @brief |
---|
| 116 | Register variables to synchronize over the network. |
---|
| 117 | */ |
---|
| 118 | void JumpPlatform::registerVariables() |
---|
| 119 | { |
---|
| 120 | registerVariable( this->fieldWidth_ ); |
---|
| 121 | registerVariable( this->fieldHeight_ ); |
---|
| 122 | registerVariable( this->relMercyOffset_ ); |
---|
| 123 | registerVariable( this->batID_[0] ); |
---|
| 124 | //registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<JumpPlatform>( this, &JumpPlatform::applyBats) ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | @brief |
---|
| 129 | Is called every tick. |
---|
| 130 | Handles the movement of the ball and its interaction with the boundaries and bats. |
---|
| 131 | @param dt |
---|
| 132 | The time since the last tick. |
---|
| 133 | */ |
---|
[10032] | 134 | void JumpPlatform::tick(float dt) |
---|
| 135 | { |
---|
[10040] | 136 | SUPER(JumpPlatform, tick, dt); |
---|
[10017] | 137 | |
---|
[10040] | 138 | Vector3 platformPosition = this->getPosition(); |
---|
| 139 | Vector3 platformVelocity = this->getVelocity(); |
---|
[10017] | 140 | |
---|
[10040] | 141 | if (figure_ != NULL) |
---|
| 142 | { |
---|
| 143 | Vector3 figurePosition = figure_->getPosition(); |
---|
| 144 | Vector3 figureVelocity = figure_->getVelocity(); |
---|
[10017] | 145 | |
---|
[10040] | 146 | if(figureVelocity.z < 0 && figurePosition.x > platformPosition.x-10 && figurePosition.x < platformPosition.x+10 && figurePosition.z > platformPosition.z-4 && figurePosition.z < platformPosition.z+4) |
---|
| 147 | { |
---|
| 148 | figure_->JumpFromPlatform(200.0f); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | /* |
---|
| 155 | // 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). |
---|
| 156 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
| 157 | { |
---|
| 158 | defBoundarySound_->play(); //play boundary sound |
---|
| 159 | // Its velocity in z-direction is inverted (i.e. it bounces off). |
---|
| 160 | velocity.z = -velocity.z; |
---|
| 161 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
| 162 | if (position.z > this->fieldHeight_ / 2) |
---|
| 163 | position.z = this->fieldHeight_ / 2; |
---|
| 164 | if (position.z < -this->fieldHeight_ / 2) |
---|
| 165 | position.z = -this->fieldHeight_ / 2; |
---|
| 166 | |
---|
| 167 | this->fireEvent(); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry). |
---|
| 171 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
| 172 | { |
---|
| 173 | float distance = 0; |
---|
| 174 | |
---|
| 175 | if (this->bat_ != NULL) // If there are bats. |
---|
| 176 | { |
---|
| 177 | // If the right boundary has been crossed. |
---|
| 178 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL) |
---|
| 179 | { |
---|
| 180 | // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) |
---|
| 181 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
| 182 | if (fabs(distance) <= 1) // If the bat is there to parry. |
---|
| 183 | { |
---|
| 184 | defBatSound_->play(); //play bat sound |
---|
| 185 | // Set the ball to be exactly at the boundary. |
---|
| 186 | position.x = this->fieldWidth_ / 2; |
---|
| 187 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 188 | velocity.x = -velocity.x; |
---|
| 189 | // Adjust the velocity in the z-direction, depending on where the ball hit the bat. |
---|
| 190 | velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 191 | acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; |
---|
| 192 | |
---|
| 193 | this->fireEvent(); |
---|
| 194 | } |
---|
| 195 | // If the left player scores. |
---|
| 196 | else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 197 | { |
---|
| 198 | defScoreSound_->play();//play score sound |
---|
| 199 | if (this->getGametype() && this->bat_[0]) |
---|
| 200 | { |
---|
| 201 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | // If the left boundary has been crossed. |
---|
| 207 | else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL) |
---|
| 208 | { |
---|
| 209 | // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) |
---|
| 210 | distance = (position.z - this->figure_->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
| 211 | if (fabs(distance) <= 1) // If the bat is there to parry. |
---|
| 212 | { |
---|
| 213 | defBatSound_->play(); //play bat sound |
---|
| 214 | // Set the ball to be exactly at the boundary. |
---|
| 215 | position.x = -this->fieldWidth_ / 2; |
---|
| 216 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 217 | velocity.x = -velocity.x; |
---|
| 218 | // Adjust the velocity in the z-direction, depending on where the ball hit the bat. |
---|
| 219 | velocity.z = distance * distance * sgn(distance) * JumpPlatform::MAX_REL_Z_VELOCITY * this->speed_; |
---|
| 220 | acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; |
---|
| 221 | |
---|
| 222 | this->fireEvent(); |
---|
| 223 | } |
---|
| 224 | // If the right player scores. |
---|
| 225 | else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
| 226 | { |
---|
| 227 | defScoreSound_->play();//play score sound |
---|
| 228 | if (this->getGametype() && this->bat_[1]) |
---|
| 229 | { |
---|
| 230 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
| 231 | return; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | */ |
---|
[10032] | 238 | } |
---|
[10017] | 239 | |
---|
[10040] | 240 | /** |
---|
| 241 | @brief |
---|
| 242 | Set the bats for the ball. |
---|
| 243 | @param bats |
---|
| 244 | An array (of size 2) of weak pointers, to be set as the new bats. |
---|
| 245 | */ |
---|
| 246 | void JumpPlatform::setFigure(WeakPtr<JumpFigure> newFigure) |
---|
| 247 | { |
---|
| 248 | if (this->bDeleteBats_) // If there are already some bats, delete them. |
---|
| 249 | { |
---|
| 250 | delete this->figure_; |
---|
| 251 | this->bDeleteBats_ = false; |
---|
| 252 | } |
---|
[10017] | 253 | |
---|
[10040] | 254 | this->figure_ = newFigure; |
---|
| 255 | // Also store their object IDs, for synchronization. |
---|
| 256 | this->batID_[0] = this->figure_->getObjectID(); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | @brief |
---|
| 261 | Get the bats over the network. |
---|
| 262 | */ |
---|
| 263 | void JumpPlatform::applyBats() |
---|
| 264 | { |
---|
| 265 | // Make space for the bats, if they don't exist, yet. |
---|
| 266 | if (this->figure_ == NULL) |
---|
| 267 | { |
---|
| 268 | this->figure_ = *(new WeakPtr<JumpFigure>); |
---|
| 269 | this->bDeleteBats_ = true; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | if (this->batID_[0] != OBJECTID_UNKNOWN) |
---|
| 273 | { |
---|
| 274 | // WAR IM PONG NICHT AUSKOMMENTIERT!!! |
---|
| 275 | //this->figure_ = orxonox_cast<JumpFigure>(Synchronisable::getSynchronisable(this->batID_[0])); |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | void JumpPlatform::setDefScoreSound(const std::string &jumpSound) |
---|
| 280 | { |
---|
| 281 | if( defScoreSound_ ) |
---|
| 282 | defScoreSound_->setSource(jumpSound); |
---|
| 283 | else |
---|
| 284 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | const std::string& JumpPlatform::getDefScoreSound() |
---|
| 288 | { |
---|
| 289 | if( defScoreSound_ ) |
---|
| 290 | return defScoreSound_->getSource(); |
---|
| 291 | else |
---|
| 292 | assert(0); |
---|
| 293 | return BLANKSTRING; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | void JumpPlatform::setDefBatSound(const std::string &jumpSound) |
---|
| 297 | { |
---|
| 298 | if( defBatSound_ ) |
---|
| 299 | defBatSound_->setSource(jumpSound); |
---|
| 300 | else |
---|
| 301 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | const std::string& JumpPlatform::getDefBatSound() |
---|
| 305 | { |
---|
| 306 | if( defBatSound_ ) |
---|
| 307 | return defBatSound_->getSource(); |
---|
| 308 | else |
---|
| 309 | assert(0); |
---|
| 310 | return BLANKSTRING; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | void JumpPlatform::setDefBoundarySound(const std::string &jumpSound) |
---|
| 314 | { |
---|
| 315 | if( defBoundarySound_ ) |
---|
| 316 | defBoundarySound_->setSource(jumpSound); |
---|
| 317 | else |
---|
| 318 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | const std::string& JumpPlatform::getDefBoundarySound() |
---|
| 322 | { |
---|
| 323 | if( defBoundarySound_ ) |
---|
| 324 | return defBoundarySound_->getSource(); |
---|
| 325 | else |
---|
| 326 | assert(0); |
---|
| 327 | return BLANKSTRING; |
---|
| 328 | } |
---|
[10017] | 329 | } |
---|