[11843] | 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 | * Oli Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * Damian 'Mozork' Frick |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "PacmanGhost.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
| 36 | RegisterClass(PacmanGhost); |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | @brief |
---|
| 40 | Constructor. Registers the object and initializes some default values. |
---|
| 41 | @param creator |
---|
| 42 | The creator of this object. |
---|
| 43 | */ |
---|
| 44 | PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context) |
---|
| 45 | { |
---|
| 46 | RegisterObject(PacmanGhost); |
---|
| 47 | |
---|
| 48 | this->localLinearAcceleration_.setValue(1, 1, 1); |
---|
| 49 | this->localAngularAcceleration_.setValue(1, 1, 1); |
---|
| 50 | |
---|
| 51 | this->velocity.setValue(1, 0, 0); |
---|
| 52 | |
---|
| 53 | this->setCollisionType(CollisionType::Dynamic); |
---|
| 54 | |
---|
| 55 | this->resetposition = Vector3(0,20,245); //Set Default start position |
---|
| 56 | |
---|
| 57 | this->actuelposition = this->getPosition(); |
---|
| 58 | |
---|
| 59 | this->target_x = actuelposition.x; |
---|
| 60 | this->target_z = actuelposition.z; |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Destructor. Destroys controller, if present. |
---|
| 67 | */ |
---|
| 68 | PacmanGhost::~PacmanGhost() |
---|
| 69 | { |
---|
| 70 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | @brief |
---|
| 75 | Method for creating a AutonomousDrone through XML. |
---|
| 76 | */ |
---|
| 77 | void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 78 | { |
---|
| 79 | SUPER(PacmanGhost, XMLPort, xmlelement, mode); |
---|
| 80 | |
---|
| 81 | XMLPortParam(PacmanGhost, "primaryThrust", setPrimaryThrust, getPrimaryThrust, xmlelement, mode); |
---|
| 82 | XMLPortParam(PacmanGhost, "auxiliaryThrust", setAuxiliaryThrust, getAuxiliaryThrust, xmlelement, mode); |
---|
| 83 | XMLPortParam(PacmanGhost, "rotationThrust", setRotationThrust, getRotationThrust, xmlelement, mode); |
---|
| 84 | XMLPortParam(PacmanGhost, "resetposition", setResetPosition, getResetPosition, xmlelement, mode); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | Vector3 possibleposition[] = {Vector3(0,10,245),Vector3(215,0,240)}; |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | @brief |
---|
| 93 | Defines which actions the AutonomousDrone has to take in each tick. |
---|
| 94 | @param dt |
---|
| 95 | The length of the tick. |
---|
| 96 | */ |
---|
| 97 | void PacmanGhost::tick(float dt) |
---|
| 98 | { |
---|
| 99 | SUPER(PacmanGhost, tick, dt); |
---|
| 100 | |
---|
| 101 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxiliaryThrust_); |
---|
| 102 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxiliaryThrust_); |
---|
| 103 | if (this->localLinearAcceleration_.z() > 0) |
---|
| 104 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxiliaryThrust_); |
---|
| 105 | else |
---|
| 106 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
| 107 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
| 108 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
| 109 | |
---|
| 110 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
| 111 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
| 112 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | this->actuelposition = this->getPosition(); |
---|
| 116 | |
---|
| 117 | move(); |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | /* |
---|
| 121 | |
---|
| 122 | //Calculate next move |
---|
| 123 | |
---|
| 124 | this->actuelposition = this->getPosition(); |
---|
| 125 | |
---|
| 126 | if(((this->actuelposition.x - this->target_x)<0.1) && ((this->actuelposition.z - this->target_z)<0.1)){ |
---|
| 127 | this->ismoving = false; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if(this->ismoving){ |
---|
| 131 | if(!((this->actuelposition.z-target_z)<0.1)) |
---|
| 132 | moveFrontBack(sgn(this->actuelposition.z-this->target_z)*dt*100); |
---|
| 133 | if(!((this->actuelposition.x-target_x)<0.1)) |
---|
| 134 | moveRightLeft(sgn(this->actuelposition.x-this->target_x)*dt*100); //Assume dt is quite small |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | else{ //Check on which position ghost is |
---|
| 138 | if(((this->actuelposition.x - possibleposition[0].x)<0.1) && ((this->actuelposition.z - possibleposition[0].z)<0.1)){ |
---|
| 139 | this->target_x = possibleposition[1].x; |
---|
| 140 | this->target_z = possibleposition[1].z; |
---|
| 141 | this->ismoving = true; |
---|
| 142 | } |
---|
| 143 | else if(((actuelposition.x - possibleposition[1].x)<0.1) && ((actuelposition.z - possibleposition[1].z)<0.1)){ |
---|
| 144 | this->target_x = possibleposition[0].x; |
---|
| 145 | this->target_z = possibleposition[0].z; |
---|
| 146 | this->ismoving = true; |
---|
| 147 | } |
---|
| 148 | else{ |
---|
| 149 | } //End of Position table |
---|
| 150 | } |
---|
| 151 | */ |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | @brief |
---|
| 159 | Moves the AutonomousDrone in the negative z-direction (Front/Back) by an amount specified by the first component of the input 2-dim vector. |
---|
| 160 | @param value |
---|
| 161 | The vector determining the amount of the movement. |
---|
| 162 | */ |
---|
| 163 | void PacmanGhost::moveFrontBack(const Vector2& value) |
---|
| 164 | { |
---|
| 165 | this->setPosition(dt*(actuelposition + velocity_)); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | @brief |
---|
| 170 | Moves the AutonomousDrone in the x-direction (Right/Left) by an amount specified by the first component of the input 2-dim vector. |
---|
| 171 | @param value |
---|
| 172 | The vector determining the amount of the movement. |
---|
| 173 | */ |
---|
| 174 | void PacmanGhost::moveRightLeft(const Vector2& value) |
---|
| 175 | { |
---|
| 176 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | @brief |
---|
| 181 | Moves the AutonomousDrone in the y-direction (Up/Down) by an amount specified by the first component of the input 2-dim vector. |
---|
| 182 | @param value |
---|
| 183 | The vector determining the amount of the movement. |
---|
| 184 | */ |
---|
| 185 | void PacmanGhost::moveUpDown(const Vector2& value) |
---|
| 186 | { |
---|
| 187 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | @brief |
---|
| 192 | Rotates the AutonomousDrone around the y-axis by the amount specified by the first component of the input 2-dim vector. |
---|
| 193 | @param value |
---|
| 194 | The vector determining the amount of the angular movement. |
---|
| 195 | */ |
---|
| 196 | void PacmanGhost::rotateYaw(const Vector2& value) |
---|
| 197 | { |
---|
| 198 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | @brief |
---|
| 203 | Rotates the AutonomousDrone around the x-axis by the amount specified by the first component of the input 2-dim vector. |
---|
| 204 | @param value |
---|
| 205 | The vector determining the amount of the angular movement. |
---|
| 206 | */ |
---|
| 207 | void PacmanGhost::rotatePitch(const Vector2& value) |
---|
| 208 | { |
---|
| 209 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | @brief |
---|
| 214 | Rotates the AutonomousDrone around the z-axis by the amount specified by the first component of the input 2-dim vector. |
---|
| 215 | @param value |
---|
| 216 | The vector determining the amount of the angular movement. |
---|
| 217 | */ |
---|
| 218 | void PacmanGhost::rotateRoll(const Vector2& value) |
---|
| 219 | { |
---|
| 220 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | } |
---|