| [2825] | 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 |  | 
|---|
| [8108] | 29 | /** | 
|---|
 | 30 |     @file PongBat.cc | 
|---|
 | 31 |     @brief Implementation of the PongBat class. | 
|---|
 | 32 | */ | 
|---|
 | 33 |  | 
|---|
| [2825] | 34 | #include "PongBat.h" | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "core/CoreIncludes.h" | 
|---|
 | 37 | #include "core/XMLPort.h" | 
|---|
 | 38 |  | 
|---|
 | 39 | namespace orxonox | 
|---|
 | 40 | { | 
|---|
 | 41 |     CreateFactory(PongBat); | 
|---|
 | 42 |  | 
|---|
| [8108] | 43 |     /** | 
|---|
 | 44 |     @brief | 
|---|
 | 45 |         Constructor. Registers and initializes the object. | 
|---|
 | 46 |     */ | 
|---|
| [2825] | 47 |     PongBat::PongBat(BaseObject* creator) : ControllableEntity(creator) | 
|---|
 | 48 |     { | 
|---|
 | 49 |         RegisterObject(PongBat); | 
|---|
 | 50 |  | 
|---|
 | 51 |         this->movement_ = 0; | 
|---|
 | 52 |         this->bMoveLocal_ = false; | 
|---|
 | 53 |         this->speed_ = 60; | 
|---|
 | 54 |         this->length_ = 0.25; | 
|---|
 | 55 |         this->fieldHeight_ = 100; | 
|---|
| [2839] | 56 |         this->bSteadiedPosition_ = false; | 
|---|
| [2825] | 57 |  | 
|---|
 | 58 |         this->registerVariables(); | 
|---|
 | 59 |     } | 
|---|
 | 60 |  | 
|---|
| [8108] | 61 |     /** | 
|---|
 | 62 |     @brief | 
|---|
 | 63 |         Registers variables to be synchronized over the network. | 
|---|
 | 64 |     */ | 
|---|
| [2825] | 65 |     void PongBat::registerVariables() | 
|---|
 | 66 |     { | 
|---|
 | 67 |         registerVariable(this->speed_); | 
|---|
| [3084] | 68 |         registerVariable(this->fieldHeight_); | 
|---|
 | 69 |         registerVariable(this->length_); | 
|---|
| [2825] | 70 |     } | 
|---|
 | 71 |  | 
|---|
| [8108] | 72 |     /** | 
|---|
 | 73 |     @brief | 
|---|
 | 74 |         Is called each tick. | 
|---|
 | 75 |         Moves the bat. | 
|---|
 | 76 |     @param dt | 
|---|
 | 77 |         The time since last tick. | 
|---|
 | 78 |     */ | 
|---|
| [2825] | 79 |     void PongBat::tick(float dt) | 
|---|
 | 80 |     { | 
|---|
| [8108] | 81 |         // If the bat is controlled (but not over the network). | 
|---|
| [2825] | 82 |         if (this->hasLocalController()) | 
|---|
 | 83 |         { | 
|---|
| [2839] | 84 |             if (this->movement_ != 0) | 
|---|
 | 85 |             { | 
|---|
| [8108] | 86 |                 // The absolute value of the movement is restricted to be lesser or equal than the speed of the bat. | 
|---|
| [2839] | 87 |                 this->movement_ = clamp(this->movement_, -1.0f, 1.0f) * this->speed_; | 
|---|
| [2825] | 88 |  | 
|---|
| [8108] | 89 |                 // If moveRightLeft() is used the movement is dependento on wehther it is the right or the left bat, so, it is i.e. dependent on the orientation of the bat. | 
|---|
| [2839] | 90 |                 if (this->bMoveLocal_) | 
|---|
 | 91 |                     this->setVelocity(this->getOrientation() * Vector3(this->movement_, 0, 0)); | 
|---|
 | 92 |                 else | 
|---|
 | 93 |                     this->setVelocity(0, 0, this->movement_); | 
|---|
| [2825] | 94 |  | 
|---|
| [2839] | 95 |                 this->movement_ = 0; | 
|---|
 | 96 |                 this->bSteadiedPosition_ = false; | 
|---|
 | 97 |             } | 
|---|
| [8108] | 98 |             // If there is no movement but the position has not been steadied, the velocity is set to zero and the position is reaffirmed. | 
|---|
| [2839] | 99 |             else if (!this->bSteadiedPosition_) | 
|---|
 | 100 |             { | 
|---|
 | 101 |                 // To ensure network synchronicity | 
|---|
 | 102 |                 this->setVelocity(0, 0, 0); | 
|---|
 | 103 |                 this->setPosition(this->getPosition()); | 
|---|
 | 104 |                 this->bSteadiedPosition_ = true; | 
|---|
 | 105 |             } | 
|---|
| [2825] | 106 |         } | 
|---|
 | 107 |  | 
|---|
 | 108 |         SUPER(PongBat, tick, dt); | 
|---|
 | 109 |  | 
|---|
| [8108] | 110 |         // Restrict the position of the bats, for them to always be between the upper and lower delimiters. i.e. the bats stall if they reach the upper or lower boundary. | 
|---|
| [2839] | 111 |         Vector3 position = this->getPosition(); | 
|---|
 | 112 |         if (position.z > this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2) | 
|---|
 | 113 |             position.z = this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2; | 
|---|
 | 114 |         if (position.z < -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2) | 
|---|
 | 115 |             position.z = -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2; | 
|---|
 | 116 |         if (position != this->getPosition()) | 
|---|
| [3084] | 117 |         { | 
|---|
| [2839] | 118 |             this->setPosition(position); | 
|---|
| [3084] | 119 |             this->setVelocity( Vector3::ZERO ); | 
|---|
 | 120 |         } | 
|---|
| [2825] | 121 |     } | 
|---|
 | 122 |  | 
|---|
| [8108] | 123 |     /** | 
|---|
 | 124 |     @brief | 
|---|
 | 125 |         Overloaded the function to steer the bat up and down. | 
|---|
 | 126 |     @param value | 
|---|
 | 127 |         A vector whose first component is the inverse direction in which we want to steer the bat. | 
|---|
 | 128 |     */ | 
|---|
| [2825] | 129 |     void PongBat::moveFrontBack(const Vector2& value) | 
|---|
 | 130 |     { | 
|---|
 | 131 |         this->bMoveLocal_ = false; | 
|---|
| [3084] | 132 |         this->movement_ = -value.x; | 
|---|
| [2825] | 133 |     } | 
|---|
 | 134 |  | 
|---|
| [8108] | 135 |     /** | 
|---|
 | 136 |     @brief | 
|---|
 | 137 |         Overloaded the function to steer the bat up and down. | 
|---|
 | 138 |     @param value | 
|---|
 | 139 |         A vector whose first component is the direction in which we wnat to steer the bat. | 
|---|
 | 140 |     */ | 
|---|
| [2825] | 141 |     void PongBat::moveRightLeft(const Vector2& value) | 
|---|
 | 142 |     { | 
|---|
 | 143 |         this->bMoveLocal_ = true; | 
|---|
 | 144 |         this->movement_ = value.x; | 
|---|
 | 145 |     } | 
|---|
| [2839] | 146 |  | 
|---|
| [8108] | 147 |     /** | 
|---|
 | 148 |     @brief | 
|---|
 | 149 |         Is called when the player changed. | 
|---|
 | 150 |     */ | 
|---|
| [2839] | 151 |     void PongBat::changedPlayer() | 
|---|
 | 152 |     { | 
|---|
 | 153 |         this->setVelocity(0, 0, 0); | 
|---|
 | 154 |     } | 
|---|
| [2825] | 155 | } | 
|---|