| [6867] | 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 |  *      Cyrill Frei | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "FpsPlayer.h" | 
|---|
 | 30 |  | 
|---|
 | 31 | #include <OgreSceneNode.h> | 
|---|
 | 32 | #include <BulletDynamics/Dynamics/btRigidBody.h> | 
|---|
 | 33 | #include <LinearMath/btVector3.h> | 
|---|
 | 34 | #include <BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> | 
|---|
| [7052] | 35 | #include <OgreSceneManager.h> | 
|---|
 | 36 | #include <OgreSceneNode.h> | 
|---|
 | 37 | #include <OgreEntity.h> | 
|---|
| [6867] | 38 |  | 
|---|
 | 39 | #include "core/CoreIncludes.h" | 
|---|
| [9667] | 40 | #include "core/config/ConfigValueIncludes.h" | 
|---|
| [6867] | 41 | #include "core/Template.h" | 
|---|
 | 42 | #include "core/XMLPort.h" | 
|---|
 | 43 | #include "items/Engine.h" | 
|---|
| [7052] | 44 | #include "Scene.h" | 
|---|
 | 45 | #include "weaponsystem/WeaponPack.h" | 
|---|
 | 46 | #include "weaponsystem/WeaponSlot.h" | 
|---|
 | 47 | #include "weaponsystem/Weapon.h" | 
|---|
| [6867] | 48 |  | 
|---|
 | 49 | #include <cmath> | 
|---|
 | 50 |  | 
|---|
 | 51 | namespace orxonox | 
|---|
 | 52 | { | 
|---|
| [7070] | 53 |     const float orientationGain_ = 100; | 
|---|
 | 54 |     const float jumpValue_ = 300; | 
|---|
| [9667] | 55 |     RegisterClass(FpsPlayer); | 
|---|
| [6867] | 56 |  | 
|---|
| [9667] | 57 |     FpsPlayer::FpsPlayer(Context* context) : Pawn(context) | 
|---|
| [6867] | 58 |     { | 
|---|
 | 59 |         RegisterObject(FpsPlayer); | 
|---|
 | 60 |         this->speed_ = 200; | 
|---|
 | 61 |         this->localVelocity_ = Vector3::ZERO; | 
|---|
| [7075] | 62 | /* | 
|---|
| [6867] | 63 |  *        this->primaryThrust_  = 100; | 
|---|
 | 64 |  *        this->auxilaryThrust_ =  30; | 
|---|
 | 65 |  *        this->rotationThrust_ =  10; | 
|---|
 | 66 |  * | 
|---|
 | 67 |  *        this->localLinearAcceleration_.setValue(0, 0, 0); | 
|---|
 | 68 |  *        this->localAngularAcceleration_.setValue(0, 0, 0); | 
|---|
 | 69 |  *        this->bBoost_ = false; | 
|---|
 | 70 |  *        this->bPermanentBoost_ = false; | 
|---|
 | 71 |  *        this->steering_ = Vector3::ZERO; | 
|---|
| [7075] | 72 | */ | 
|---|
| [6867] | 73 |  | 
|---|
 | 74 |  | 
|---|
 | 75 |         this->bInvertYAxis_ = false; | 
|---|
 | 76 |  | 
|---|
 | 77 |         this->setDestroyWhenPlayerLeft(true); | 
|---|
 | 78 |  | 
|---|
 | 79 |         // FpsPlayer is always a physical object per default | 
|---|
 | 80 |         // Be aware of this call: The collision type legality check will not reach derived classes! | 
|---|
 | 81 |         this->setCollisionType(WorldEntity::Dynamic); | 
|---|
 | 82 |         // Get notification about collisions | 
|---|
 | 83 |         this->enableCollisionCallback(); | 
|---|
 | 84 |  | 
|---|
 | 85 |         this->setConfigValues(); | 
|---|
 | 86 |         this->registerVariables(); | 
|---|
| [6908] | 87 |  | 
|---|
| [7075] | 88 |         //this->weaponNode = this->cameraPositionRootNode_; | 
|---|
 | 89 |         this->weaponNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); | 
|---|
 | 90 |         this->attachNode(this->weaponNode_); | 
|---|
| [6867] | 91 |     } | 
|---|
 | 92 |  | 
|---|
 | 93 |     FpsPlayer::~FpsPlayer() | 
|---|
 | 94 |     { | 
|---|
| [7080] | 95 |         if (this->isInitialized()) | 
|---|
 | 96 |         { | 
|---|
 | 97 |             if (this->mesh_.getEntity()) | 
|---|
 | 98 |                 this->detachOgreObject(this->mesh_.getEntity()); | 
|---|
 | 99 |  | 
|---|
 | 100 |             if (this->weaponNode_ && this->getScene()->getSceneManager()) | 
|---|
 | 101 |                 this->getScene()->getSceneManager()->destroySceneNode(this->weaponNode_->getName()); | 
|---|
 | 102 |         } | 
|---|
| [6867] | 103 |     } | 
|---|
 | 104 |  | 
|---|
 | 105 |     void FpsPlayer::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 106 |     { | 
|---|
 | 107 |         SUPER(FpsPlayer, XMLPort, xmlelement, mode); | 
|---|
| [7075] | 108 |  | 
|---|
| [6867] | 109 |         XMLPortParamVariable(FpsPlayer, "primaryThrust",  primaryThrust_,  xmlelement, mode); | 
|---|
 | 110 |         XMLPortParamVariable(FpsPlayer, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); | 
|---|
 | 111 |         XMLPortParamVariable(FpsPlayer, "rotationThrust", rotationThrust_, xmlelement, mode); | 
|---|
| [7075] | 112 |         XMLPortParam(FpsPlayer, "weapon", setMeshSource, getMeshSource, xmlelement, mode); | 
|---|
| [6867] | 113 |     } | 
|---|
 | 114 |  | 
|---|
 | 115 |     void FpsPlayer::registerVariables() | 
|---|
 | 116 |     { | 
|---|
 | 117 |         registerVariable(this->primaryThrust_,  VariableDirection::ToClient); | 
|---|
 | 118 |         registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); | 
|---|
 | 119 |         registerVariable(this->rotationThrust_, VariableDirection::ToClient); | 
|---|
| [7075] | 120 |         registerVariable(this->weaponMashName_, VariableDirection::ToClient); | 
|---|
| [6867] | 121 |     } | 
|---|
 | 122 |  | 
|---|
| [7075] | 123 |  | 
|---|
 | 124 |  | 
|---|
| [6867] | 125 |     void FpsPlayer::setConfigValues() | 
|---|
 | 126 |     { | 
|---|
 | 127 |         SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = targetting down)."); | 
|---|
 | 128 |     } | 
|---|
 | 129 |  | 
|---|
 | 130 |     bool FpsPlayer::isCollisionTypeLegal(WorldEntity::CollisionType type) const | 
|---|
 | 131 |     { | 
|---|
 | 132 |         if (type != WorldEntity::Dynamic) | 
|---|
 | 133 |         { | 
|---|
| [8858] | 134 |             orxout(internal_warning) << "Cannot tell a FpsPlayer not to be dynamic! Ignoring." << endl; | 
|---|
| [6867] | 135 |             assert(false); // Only in debug mode | 
|---|
 | 136 |             return false; | 
|---|
 | 137 |         } | 
|---|
 | 138 |         else | 
|---|
 | 139 |             return true; | 
|---|
 | 140 |     } | 
|---|
 | 141 |  | 
|---|
 | 142 |     void FpsPlayer::tick(float dt) | 
|---|
 | 143 |     { | 
|---|
 | 144 |         if (this->hasLocalController()) | 
|---|
 | 145 |         { | 
|---|
| [6872] | 146 |             this->setOrientation(savedOrientation_); | 
|---|
| [7075] | 147 |  | 
|---|
 | 148 |             this->thisTickBoost_ = false; | 
|---|
 | 149 |  | 
|---|
 | 150 |             float localSpeedSquared = this->localVelocity_.squaredLength(); | 
|---|
| [6867] | 151 |             float localSpeed; | 
|---|
 | 152 |             if (localSpeedSquared > 1.0) | 
|---|
 | 153 |                 localSpeed = this->speed_ / sqrtf(localSpeedSquared); | 
|---|
 | 154 |             else | 
|---|
 | 155 |                 localSpeed = this->speed_; | 
|---|
 | 156 |  | 
|---|
 | 157 |             this->localVelocity_.x *= localSpeed; | 
|---|
 | 158 |             this->localVelocity_.z *= localSpeed; | 
|---|
| [7075] | 159 |             Vector3 temp = this->getOrientation() * this->localVelocity_; | 
|---|
 | 160 |             if (localVelocity_.y == jumpValue_) | 
|---|
 | 161 |                 this->setVelocity(Vector3(temp.x, temp.y + this->getVelocity().y, temp.z)); | 
|---|
 | 162 |             else | 
|---|
 | 163 |                 this->setVelocity(Vector3(temp.x, this->getVelocity().y, temp.z)); | 
|---|
| [6867] | 164 |             this->localVelocity_.x = 0; | 
|---|
 | 165 |             this->localVelocity_.y = 0; | 
|---|
 | 166 |             this->localVelocity_.z = 0; | 
|---|
 | 167 |  | 
|---|
 | 168 |             if (!this->isInMouseLook()) | 
|---|
 | 169 |             { | 
|---|
| [7052] | 170 |                 this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()), WorldEntity::Parent); | 
|---|
| [6867] | 171 |  | 
|---|
| [7075] | 172 |                 Radian pitch = this->cameraPositionRootNode_->getOrientation().getPitch(); | 
|---|
| [7184] | 173 |                 if (pitch < Radian(math::pi_2) && pitch > Radian(-math::pi_2)) | 
|---|
| [7075] | 174 |                 { | 
|---|
 | 175 |                     this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); | 
|---|
 | 176 |                 } | 
|---|
| [7184] | 177 |                 else if (pitch < Radian(-math::pi_2)) | 
|---|
| [7075] | 178 |                 { | 
|---|
| [7183] | 179 |                     if (this->pitch_ > 0.0f) | 
|---|
| [7075] | 180 |                         this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); | 
|---|
| [7184] | 181 |                     else if (pitch < Radian(-math::pi_2)) | 
|---|
 | 182 |                         this->cameraPositionRootNode_->pitch(-pitch + Radian(-math::pi_2)); | 
|---|
| [7075] | 183 |                 } | 
|---|
| [7184] | 184 |                 else if (pitch > Radian(math::pi_2)) | 
|---|
| [7075] | 185 |                 { | 
|---|
| [7183] | 186 |                     if (this->pitch_ < 0.0f) | 
|---|
| [7075] | 187 |                         this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); | 
|---|
| [7184] | 188 |                     else if (pitch > Radian(math::pi_2)) | 
|---|
 | 189 |                         this->cameraPositionRootNode_->pitch(-pitch + Radian(math::pi_2)); | 
|---|
| [7075] | 190 |                 } | 
|---|
 | 191 |                 this->weaponNode_->setOrientation(this->cameraPositionRootNode_->getOrientation()); | 
|---|
 | 192 |             } | 
|---|
 | 193 |  | 
|---|
| [6867] | 194 |             this->yaw_ = this->pitch_ = this->roll_ = 0; | 
|---|
| [7075] | 195 |  | 
|---|
 | 196 |             this->setAngularVelocity(0.0, 0.0, 0.0); | 
|---|
 | 197 |             this->savedOrientation_ = this->getOrientation(); | 
|---|
| [6867] | 198 |         } | 
|---|
 | 199 |  | 
|---|
 | 200 |         SUPER(FpsPlayer, tick, dt); | 
|---|
 | 201 |     } | 
|---|
| [7075] | 202 |  | 
|---|
| [7052] | 203 |     void FpsPlayer::changedMesh() | 
|---|
 | 204 |     { | 
|---|
 | 205 |         if (GameMode::showsGraphics()) | 
|---|
 | 206 |         { | 
|---|
 | 207 |             if (this->mesh_.getEntity()) | 
|---|
| [7070] | 208 |                 this->weaponNode_->detachObject(this->mesh_.getEntity()); | 
|---|
| [6867] | 209 |  | 
|---|
| [7052] | 210 |             this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); | 
|---|
 | 211 |  | 
|---|
 | 212 |             if (this->mesh_.getEntity()) | 
|---|
 | 213 |             { | 
|---|
| [7070] | 214 |                 this->weaponNode_->attachObject(this->mesh_.getEntity()); | 
|---|
| [7052] | 215 |             } | 
|---|
 | 216 |         } | 
|---|
 | 217 |     } | 
|---|
 | 218 |  | 
|---|
| [6867] | 219 |     void FpsPlayer::setPlayer(PlayerInfo* player) | 
|---|
 | 220 |     { | 
|---|
 | 221 |         ControllableEntity::setPlayer(player); | 
|---|
 | 222 |  | 
|---|
 | 223 | //        this->setSyncMode(ObjectDirection::ToClient); | 
|---|
 | 224 |     } | 
|---|
 | 225 |  | 
|---|
 | 226 |     void FpsPlayer::startLocalHumanControl() | 
|---|
 | 227 |     { | 
|---|
 | 228 |         ControllableEntity::startLocalHumanControl(); | 
|---|
 | 229 |     } | 
|---|
| [7075] | 230 |  | 
|---|
| [6867] | 231 |     void FpsPlayer::moveFrontBack(const Vector2& value) | 
|---|
 | 232 |     { | 
|---|
 | 233 |         this->localVelocity_.z -= value.x; | 
|---|
 | 234 |     } | 
|---|
 | 235 |  | 
|---|
 | 236 |  | 
|---|
 | 237 |     void FpsPlayer::moveRightLeft(const Vector2& value) | 
|---|
 | 238 |     { | 
|---|
 | 239 |         this->localVelocity_.x += value.x; | 
|---|
 | 240 |     } | 
|---|
 | 241 |  | 
|---|
 | 242 |     void FpsPlayer::moveUpDown(const Vector2& value) | 
|---|
 | 243 |     { | 
|---|
 | 244 |         //this->localVelocity_.y += value.x; | 
|---|
 | 245 |     } | 
|---|
 | 246 |  | 
|---|
 | 247 |     void FpsPlayer::rotateYaw(const Vector2& value) | 
|---|
 | 248 |     { | 
|---|
 | 249 |         this->yaw_ += value.y; | 
|---|
 | 250 |  | 
|---|
 | 251 |         ControllableEntity::rotateYaw(value); | 
|---|
 | 252 |     } | 
|---|
 | 253 |  | 
|---|
 | 254 |     void FpsPlayer::rotatePitch(const Vector2& value) | 
|---|
 | 255 |     { | 
|---|
 | 256 |         this->pitch_ += value.y; | 
|---|
 | 257 |  | 
|---|
 | 258 |         ControllableEntity::rotatePitch(value); | 
|---|
 | 259 |     } | 
|---|
 | 260 |  | 
|---|
 | 261 |     void FpsPlayer::rotateRoll(const Vector2& value) | 
|---|
 | 262 |     { | 
|---|
 | 263 |         this->roll_ += value.y; | 
|---|
 | 264 |  | 
|---|
 | 265 |         ControllableEntity::rotateRoll(value); | 
|---|
 | 266 |     } | 
|---|
 | 267 |  | 
|---|
 | 268 |     void FpsPlayer::fire() | 
|---|
 | 269 |     { | 
|---|
 | 270 |     } | 
|---|
| [7075] | 271 |  | 
|---|
| [9016] | 272 |     void FpsPlayer::boost(bool bBoost) //acctually jump | 
|---|
| [6867] | 273 |     { | 
|---|
| [7075] | 274 |         if (this->isFloor_) | 
|---|
 | 275 |         { | 
|---|
 | 276 |             if (!this->thisTickBoost_) | 
|---|
 | 277 |                 this->localVelocity_.y = jumpValue_; | 
|---|
 | 278 |             //this->physicalBody_->applyCentralImpulse(btVector3(0, jumpvalue, 0)); | 
|---|
 | 279 |             this->thisTickBoost_ = true; | 
|---|
 | 280 |             this->isFloor_ = false; | 
|---|
 | 281 |         } | 
|---|
| [6867] | 282 |     } | 
|---|
 | 283 |  | 
|---|
 | 284 |     bool FpsPlayer::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) | 
|---|
 | 285 |     { | 
|---|
| [7075] | 286 |         if (contactPoint.m_normalWorldOnB.y() > 0.6) | 
|---|
 | 287 |             this->isFloor_ = true; | 
|---|
 | 288 |         else | 
|---|
 | 289 |             this->isFloor_ = false; | 
|---|
 | 290 |  | 
|---|
 | 291 |         return false; | 
|---|
| [6867] | 292 |     } | 
|---|
| [7075] | 293 |  | 
|---|
| [7052] | 294 |     void FpsPlayer::addedWeaponPack(WeaponPack* wPack) | 
|---|
 | 295 |     { | 
|---|
 | 296 |         for (size_t i = 0; i < wPack->getNumWeapons(); ++i) | 
|---|
| [7075] | 297 |         { | 
|---|
 | 298 |             Weapon* weapon = wPack->getWeapon(i); | 
|---|
 | 299 |             if (weapon->getWeaponSlot()) | 
|---|
 | 300 |             { | 
|---|
 | 301 |                 weapon->getWeaponSlot()->removeWeapon(); | 
|---|
 | 302 |                 weapon->detachFromParent(); | 
|---|
 | 303 |                 weapon->attachToNode(this->weaponNode_); | 
|---|
 | 304 |             } | 
|---|
 | 305 |         } | 
|---|
| [7052] | 306 |     } | 
|---|
| [6867] | 307 | } | 
|---|