[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" |
---|
| 40 | #include "core/ConfigValueIncludes.h" |
---|
| 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; |
---|
[6867] | 55 | CreateFactory(FpsPlayer); |
---|
| 56 | |
---|
| 57 | FpsPlayer::FpsPlayer(BaseObject* creator) : Pawn(creator) |
---|
| 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 | { |
---|
[7075] | 95 | if (this->isInitialized() && this->mesh_.getEntity()) |
---|
[7052] | 96 | this->detachOgreObject(this->mesh_.getEntity()); |
---|
[6867] | 97 | } |
---|
| 98 | |
---|
| 99 | void FpsPlayer::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 100 | { |
---|
| 101 | SUPER(FpsPlayer, XMLPort, xmlelement, mode); |
---|
[7075] | 102 | |
---|
[6867] | 103 | XMLPortParamVariable(FpsPlayer, "primaryThrust", primaryThrust_, xmlelement, mode); |
---|
| 104 | XMLPortParamVariable(FpsPlayer, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); |
---|
| 105 | XMLPortParamVariable(FpsPlayer, "rotationThrust", rotationThrust_, xmlelement, mode); |
---|
[7075] | 106 | XMLPortParam(FpsPlayer, "weapon", setMeshSource, getMeshSource, xmlelement, mode); |
---|
[6867] | 107 | } |
---|
| 108 | |
---|
| 109 | void FpsPlayer::registerVariables() |
---|
| 110 | { |
---|
| 111 | registerVariable(this->primaryThrust_, VariableDirection::ToClient); |
---|
| 112 | registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); |
---|
| 113 | registerVariable(this->rotationThrust_, VariableDirection::ToClient); |
---|
[7075] | 114 | registerVariable(this->weaponMashName_, VariableDirection::ToClient); |
---|
[6867] | 115 | } |
---|
| 116 | |
---|
[7075] | 117 | |
---|
| 118 | |
---|
[6867] | 119 | void FpsPlayer::setConfigValues() |
---|
| 120 | { |
---|
| 121 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = targetting down)."); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | bool FpsPlayer::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
| 125 | { |
---|
| 126 | if (type != WorldEntity::Dynamic) |
---|
| 127 | { |
---|
| 128 | CCOUT(1) << "Error: Cannot tell a FpsPlayer not to be dynamic! Ignoring." << std::endl; |
---|
| 129 | assert(false); // Only in debug mode |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void FpsPlayer::tick(float dt) |
---|
| 137 | { |
---|
| 138 | if (this->hasLocalController()) |
---|
| 139 | { |
---|
[6872] | 140 | this->setOrientation(savedOrientation_); |
---|
[7075] | 141 | |
---|
| 142 | this->thisTickBoost_ = false; |
---|
| 143 | |
---|
| 144 | float localSpeedSquared = this->localVelocity_.squaredLength(); |
---|
[6867] | 145 | float localSpeed; |
---|
| 146 | if (localSpeedSquared > 1.0) |
---|
| 147 | localSpeed = this->speed_ / sqrtf(localSpeedSquared); |
---|
| 148 | else |
---|
| 149 | localSpeed = this->speed_; |
---|
| 150 | |
---|
| 151 | this->localVelocity_.x *= localSpeed; |
---|
| 152 | this->localVelocity_.z *= localSpeed; |
---|
[7075] | 153 | Vector3 temp = this->getOrientation() * this->localVelocity_; |
---|
| 154 | if (localVelocity_.y == jumpValue_) |
---|
| 155 | this->setVelocity(Vector3(temp.x, temp.y + this->getVelocity().y, temp.z)); |
---|
| 156 | else |
---|
| 157 | this->setVelocity(Vector3(temp.x, this->getVelocity().y, temp.z)); |
---|
[6867] | 158 | this->localVelocity_.x = 0; |
---|
| 159 | this->localVelocity_.y = 0; |
---|
| 160 | this->localVelocity_.z = 0; |
---|
| 161 | |
---|
| 162 | if (!this->isInMouseLook()) |
---|
| 163 | { |
---|
[7052] | 164 | this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()), WorldEntity::Parent); |
---|
[6867] | 165 | |
---|
[7075] | 166 | Radian pitch = this->cameraPositionRootNode_->getOrientation().getPitch(); |
---|
| 167 | if (pitch < Radian(1.5707) && pitch > Radian(-1.5707)) |
---|
| 168 | { |
---|
| 169 | this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); |
---|
| 170 | } |
---|
| 171 | else if (pitch < Radian(-1.5707)) |
---|
| 172 | { |
---|
| 173 | if (this->pitch_ > 0.0) |
---|
| 174 | this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); |
---|
| 175 | else if (pitch < Radian(-1.571)) |
---|
| 176 | this->cameraPositionRootNode_->pitch(-pitch + Radian(-1.570796)); |
---|
| 177 | } |
---|
| 178 | else if (pitch > Radian(1.5707)) |
---|
| 179 | { |
---|
| 180 | if (this->pitch_ < 0.0) |
---|
| 181 | this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); |
---|
| 182 | else if (pitch > Radian(1.571)) |
---|
| 183 | this->cameraPositionRootNode_->pitch(-pitch + Radian(1.570796)); |
---|
| 184 | } |
---|
| 185 | this->weaponNode_->setOrientation(this->cameraPositionRootNode_->getOrientation()); |
---|
| 186 | } |
---|
| 187 | |
---|
[6867] | 188 | this->yaw_ = this->pitch_ = this->roll_ = 0; |
---|
[7075] | 189 | |
---|
| 190 | this->setAngularVelocity(0.0, 0.0, 0.0); |
---|
| 191 | this->savedOrientation_ = this->getOrientation(); |
---|
[6867] | 192 | } |
---|
| 193 | |
---|
| 194 | SUPER(FpsPlayer, tick, dt); |
---|
| 195 | } |
---|
[7075] | 196 | |
---|
[7052] | 197 | void FpsPlayer::changedMesh() |
---|
| 198 | { |
---|
| 199 | if (GameMode::showsGraphics()) |
---|
| 200 | { |
---|
| 201 | if (this->mesh_.getEntity()) |
---|
[7070] | 202 | this->weaponNode_->detachObject(this->mesh_.getEntity()); |
---|
[6867] | 203 | |
---|
[7052] | 204 | this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); |
---|
| 205 | |
---|
| 206 | if (this->mesh_.getEntity()) |
---|
| 207 | { |
---|
[7070] | 208 | this->weaponNode_->attachObject(this->mesh_.getEntity()); |
---|
[7052] | 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
[6867] | 213 | void FpsPlayer::setPlayer(PlayerInfo* player) |
---|
| 214 | { |
---|
| 215 | ControllableEntity::setPlayer(player); |
---|
| 216 | |
---|
| 217 | // this->setSyncMode(ObjectDirection::ToClient); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | void FpsPlayer::startLocalHumanControl() |
---|
| 221 | { |
---|
| 222 | ControllableEntity::startLocalHumanControl(); |
---|
| 223 | } |
---|
[7075] | 224 | |
---|
[6867] | 225 | void FpsPlayer::moveFrontBack(const Vector2& value) |
---|
| 226 | { |
---|
| 227 | this->localVelocity_.z -= value.x; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | void FpsPlayer::moveRightLeft(const Vector2& value) |
---|
| 232 | { |
---|
| 233 | this->localVelocity_.x += value.x; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | void FpsPlayer::moveUpDown(const Vector2& value) |
---|
| 237 | { |
---|
| 238 | //this->localVelocity_.y += value.x; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | void FpsPlayer::rotateYaw(const Vector2& value) |
---|
| 242 | { |
---|
| 243 | this->yaw_ += value.y; |
---|
| 244 | |
---|
| 245 | ControllableEntity::rotateYaw(value); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | void FpsPlayer::rotatePitch(const Vector2& value) |
---|
| 249 | { |
---|
| 250 | this->pitch_ += value.y; |
---|
| 251 | |
---|
| 252 | ControllableEntity::rotatePitch(value); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | void FpsPlayer::rotateRoll(const Vector2& value) |
---|
| 256 | { |
---|
| 257 | this->roll_ += value.y; |
---|
| 258 | |
---|
| 259 | ControllableEntity::rotateRoll(value); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | void FpsPlayer::fire() |
---|
| 263 | { |
---|
| 264 | } |
---|
[7075] | 265 | |
---|
| 266 | void FpsPlayer::boost() //acctually jump |
---|
[6867] | 267 | { |
---|
[7075] | 268 | if (this->isFloor_) |
---|
| 269 | { |
---|
| 270 | if (!this->thisTickBoost_) |
---|
| 271 | this->localVelocity_.y = jumpValue_; |
---|
| 272 | //this->physicalBody_->applyCentralImpulse(btVector3(0, jumpvalue, 0)); |
---|
| 273 | this->thisTickBoost_ = true; |
---|
| 274 | this->isFloor_ = false; |
---|
| 275 | } |
---|
[6867] | 276 | } |
---|
| 277 | |
---|
| 278 | bool FpsPlayer::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) |
---|
| 279 | { |
---|
[7075] | 280 | if (contactPoint.m_normalWorldOnB.y() > 0.6) |
---|
| 281 | this->isFloor_ = true; |
---|
| 282 | else |
---|
| 283 | this->isFloor_ = false; |
---|
| 284 | |
---|
| 285 | return false; |
---|
[6867] | 286 | } |
---|
[7075] | 287 | |
---|
[7052] | 288 | void FpsPlayer::addedWeaponPack(WeaponPack* wPack) |
---|
| 289 | { |
---|
| 290 | for (size_t i = 0; i < wPack->getNumWeapons(); ++i) |
---|
[7075] | 291 | { |
---|
| 292 | Weapon* weapon = wPack->getWeapon(i); |
---|
| 293 | if (weapon->getWeaponSlot()) |
---|
| 294 | { |
---|
| 295 | weapon->getWeaponSlot()->removeWeapon(); |
---|
| 296 | weapon->detachFromParent(); |
---|
| 297 | weapon->attachToNode(this->weaponNode_); |
---|
| 298 | } |
---|
| 299 | } |
---|
[7052] | 300 | } |
---|
[6867] | 301 | } |
---|