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