2014-01-15 11:03:25 +01:00
|
|
|
|
2014-01-09 12:51:17 +01:00
|
|
|
#include "Player.h"
|
|
|
|
#include "Weapon.h"
|
2014-01-20 15:47:52 +01:00
|
|
|
#include "CollisionManager.h"
|
|
|
|
#include "Game.h"
|
2014-01-09 12:51:17 +01:00
|
|
|
|
|
|
|
using namespace GameLogic;
|
|
|
|
using namespace Oyster::Physics;
|
2014-02-14 10:09:03 +01:00
|
|
|
const float MOVE_FORCE = 30;
|
2014-02-12 13:11:35 +01:00
|
|
|
const float KEY_TIMER = 0.03f;
|
2014-01-09 12:51:17 +01:00
|
|
|
Player::Player()
|
2014-01-29 14:33:21 +01:00
|
|
|
:DynamicObject()
|
2014-01-09 12:51:17 +01:00
|
|
|
{
|
2014-01-29 14:33:21 +01:00
|
|
|
|
|
|
|
}
|
2014-02-12 14:48:58 +01:00
|
|
|
|
2014-02-14 09:53:02 +01:00
|
|
|
Player::Player(Oyster::Physics::ICustomBody *rigidBody, void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, int teamID)
|
|
|
|
:DynamicObject(rigidBody, EventOnCollision, type, objectID)
|
2014-01-29 14:33:21 +01:00
|
|
|
{
|
2014-02-14 09:53:02 +01:00
|
|
|
weapon = new Weapon(2,this);
|
|
|
|
|
|
|
|
this->life = 100;
|
2014-02-14 12:03:29 +01:00
|
|
|
this->teamID = teamID;
|
2014-02-14 09:53:02 +01:00
|
|
|
this->playerState = PLAYER_STATE_IDLE;
|
|
|
|
this->lookDir = Oyster::Math::Float3(0,0,-1);
|
|
|
|
key_forward = 0;
|
|
|
|
key_backward = 0;
|
|
|
|
key_strafeRight = 0;
|
|
|
|
key_strafeLeft = 0;
|
2014-02-14 12:03:29 +01:00
|
|
|
|
2014-02-14 12:07:49 +01:00
|
|
|
this->previousPosition = Oyster::Math::Float3(0,0,0);
|
2014-02-14 12:46:27 +01:00
|
|
|
|
2014-02-14 12:03:29 +01:00
|
|
|
this->moveDir = Oyster::Math::Float3(0,0,0);
|
|
|
|
this->moveSpeed = 100;
|
|
|
|
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
|
2014-01-27 08:54:25 +01:00
|
|
|
}
|
|
|
|
|
2014-02-14 09:53:02 +01:00
|
|
|
Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, int teamID)
|
|
|
|
:DynamicObject(rigidBody, EventOnCollision, type, objectID)
|
2014-01-27 08:54:25 +01:00
|
|
|
{
|
2014-01-27 13:54:57 +01:00
|
|
|
weapon = new Weapon(2,this);
|
|
|
|
|
|
|
|
this->life = 100;
|
2014-02-14 09:53:02 +01:00
|
|
|
this->teamID = teamID;
|
2014-01-27 13:54:57 +01:00
|
|
|
this->playerState = PLAYER_STATE_IDLE;
|
2014-02-12 11:36:08 +01:00
|
|
|
this->lookDir = Oyster::Math::Float3(0,0,-1);
|
2014-02-12 12:16:16 +01:00
|
|
|
key_forward = 0;
|
|
|
|
key_backward = 0;
|
|
|
|
key_strafeRight = 0;
|
|
|
|
key_strafeLeft = 0;
|
2014-02-13 19:49:33 +01:00
|
|
|
|
2014-02-14 11:52:44 +01:00
|
|
|
this->previousPosition = Oyster::Math::Float3(0,0,0);
|
2014-02-13 19:49:33 +01:00
|
|
|
this->moveDir = Oyster::Math::Float3(0,0,0);
|
2014-02-17 06:44:29 +01:00
|
|
|
this->moveSpeed = 20;
|
2014-02-13 19:49:33 +01:00
|
|
|
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
|
2014-01-27 13:56:31 +01:00
|
|
|
}
|
2014-01-09 12:51:17 +01:00
|
|
|
|
|
|
|
Player::~Player(void)
|
|
|
|
{
|
2014-01-27 13:54:57 +01:00
|
|
|
if(weapon)
|
|
|
|
{
|
|
|
|
delete weapon;
|
|
|
|
weapon = NULL;
|
2014-01-29 14:33:21 +01:00
|
|
|
}
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-03 15:32:46 +01:00
|
|
|
void Player::BeginFrame()
|
|
|
|
{
|
2014-02-11 11:46:06 +01:00
|
|
|
//weapon->Update(0.002f);
|
2014-02-03 15:32:46 +01:00
|
|
|
Object::BeginFrame();
|
2014-02-14 11:52:44 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
Oyster::Math::Float maxSpeed = 30;
|
2014-02-14 11:52:44 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
Oyster::Math::Float4x4 xform;
|
|
|
|
xform = this->rigidBody->GetState().GetOrientation();
|
2014-02-14 11:52:44 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
/* Handle turning */
|
|
|
|
/*if (left)
|
|
|
|
m_turnAngle -= dt * m_turnVelocity;
|
|
|
|
if (right)
|
|
|
|
m_turnAngle += dt * m_turnVelocity;*/
|
2014-02-13 19:49:33 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
//xform.setRotation (btQuaternion (btVector3(0.0, 1.0, 0.0), m_turnAngle));
|
|
|
|
|
|
|
|
Oyster::Math::Float3 linearVelocity = this->rigidBody->GetLinearVelocity();
|
|
|
|
Oyster::Math::Float speed = this->rigidBody->GetLinearVelocity().GetLength();
|
|
|
|
|
|
|
|
Oyster::Math::Float3 forwardDir = xform.v[2];
|
|
|
|
Oyster::Math::Float3 rightDir = xform.v[0];
|
|
|
|
forwardDir.Normalize();
|
|
|
|
rightDir.Normalize();
|
|
|
|
Oyster::Math::Float3 walkDirection = Oyster::Math::Float3(0.0, 0.0, 0.0);
|
2014-02-18 13:47:40 +01:00
|
|
|
Oyster::Math::Float walkSpeed = this->moveSpeed*0.2f;
|
2014-02-13 19:49:33 +01:00
|
|
|
|
2014-02-12 12:16:16 +01:00
|
|
|
if (key_forward > 0.001)
|
|
|
|
{
|
2014-02-17 06:44:29 +01:00
|
|
|
key_forward -= gameInstance->GetFrameTime();
|
|
|
|
walkDirection += forwardDir;
|
|
|
|
walkDirection.Normalize();
|
2014-02-12 12:16:16 +01:00
|
|
|
}
|
|
|
|
if (key_backward > 0.001)
|
|
|
|
{
|
|
|
|
key_backward -= gameInstance->GetFrameTime();
|
2014-02-17 06:44:29 +01:00
|
|
|
walkDirection -= forwardDir;
|
|
|
|
walkDirection.Normalize();
|
2014-02-12 12:16:16 +01:00
|
|
|
}
|
|
|
|
if (key_strafeRight > 0.001)
|
|
|
|
{
|
|
|
|
key_strafeRight -= gameInstance->GetFrameTime();
|
2014-02-17 06:44:29 +01:00
|
|
|
walkDirection -= rightDir;
|
|
|
|
walkDirection.Normalize();
|
2014-02-12 12:16:16 +01:00
|
|
|
}
|
|
|
|
if (key_strafeLeft > 0.001)
|
|
|
|
{
|
|
|
|
key_strafeLeft -= gameInstance->GetFrameTime();
|
2014-02-17 06:44:29 +01:00
|
|
|
walkDirection += rightDir;
|
|
|
|
walkDirection.Normalize();
|
|
|
|
maxSpeed = 40;
|
2014-02-12 12:16:16 +01:00
|
|
|
}
|
2014-02-17 06:44:29 +01:00
|
|
|
|
2014-02-12 13:12:51 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
if (key_forward <= 0.001 && key_backward <= 0.001 && key_strafeRight <= 0.001 && key_strafeLeft <= 0.001 && key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.7f)
|
2014-02-13 19:49:33 +01:00
|
|
|
{
|
2014-02-17 06:44:29 +01:00
|
|
|
/* Dampen when on the ground and not being moved by the player */
|
2014-02-18 13:47:40 +01:00
|
|
|
linearVelocity *= 0.2f;
|
2014-02-17 06:44:29 +01:00
|
|
|
this->rigidBody->SetLinearVelocity (linearVelocity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (speed < maxSpeed && this->rigidBody->GetLambda() < 1.0f)
|
|
|
|
{
|
|
|
|
Oyster::Math::Float3 velocity = linearVelocity + walkDirection * walkSpeed;
|
|
|
|
this->rigidBody->SetLinearVelocity(velocity);
|
|
|
|
}
|
|
|
|
else if(speed < maxSpeed)
|
|
|
|
{
|
|
|
|
Oyster::Math::Float3 velocity = linearVelocity + (walkDirection * walkSpeed)*0.2f;
|
|
|
|
this->rigidBody->SetLinearVelocity(velocity);
|
|
|
|
}
|
2014-02-12 12:16:16 +01:00
|
|
|
}
|
2014-02-12 13:12:51 +01:00
|
|
|
|
2014-02-17 06:44:29 +01:00
|
|
|
if (key_jump > 0.001)
|
|
|
|
{
|
|
|
|
this->key_jump -= this->gameInstance->GetFrameTime();
|
|
|
|
if(this->rigidBody->GetLambda() < 1.0f)
|
|
|
|
{
|
|
|
|
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized();
|
|
|
|
this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass*20);
|
|
|
|
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 19:49:33 +01:00
|
|
|
|
|
|
|
this->weapon->Update(0.01f);
|
2014-02-03 15:32:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::EndFrame()
|
|
|
|
{
|
2014-02-04 16:08:28 +01:00
|
|
|
// snap to axis
|
2014-02-13 19:49:33 +01:00
|
|
|
Oyster::Math::Float4 rotation;
|
|
|
|
|
|
|
|
this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized());
|
|
|
|
|
|
|
|
|
2014-02-11 11:46:06 +01:00
|
|
|
Object::EndFrame();
|
2014-02-03 15:32:46 +01:00
|
|
|
}
|
|
|
|
|
2014-01-09 12:51:17 +01:00
|
|
|
void Player::Move(const PLAYER_MOVEMENT &movement)
|
|
|
|
{
|
|
|
|
switch(movement)
|
|
|
|
{
|
|
|
|
case PLAYER_MOVEMENT::PLAYER_MOVEMENT_FORWARD:
|
|
|
|
MoveForward();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_MOVEMENT::PLAYER_MOVEMENT_BACKWARD:
|
|
|
|
MoveBackwards();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_MOVEMENT::PLAYER_MOVEMENT_LEFT:
|
|
|
|
MoveLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_MOVEMENT::PLAYER_MOVEMENT_RIGHT:
|
|
|
|
MoveRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAYER_MOVEMENT::PLAYER_MOVEMENT_JUMP:
|
|
|
|
Jump();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::MoveForward()
|
|
|
|
{
|
2014-02-12 12:16:16 +01:00
|
|
|
key_forward = KEY_TIMER;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
void Player::MoveBackwards()
|
|
|
|
{
|
2014-02-12 12:16:16 +01:00
|
|
|
key_backward = KEY_TIMER;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
void Player::MoveRight()
|
|
|
|
{
|
2014-02-12 12:16:16 +01:00
|
|
|
key_strafeRight = KEY_TIMER;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
void Player::MoveLeft()
|
|
|
|
{
|
2014-02-12 12:16:16 +01:00
|
|
|
key_strafeLeft = KEY_TIMER;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 11:17:19 +01:00
|
|
|
void Player::UseWeapon(const WEAPON_FIRE &usage)
|
2014-01-09 12:51:17 +01:00
|
|
|
{
|
2014-01-21 15:46:54 +01:00
|
|
|
this->weapon->Use(usage,gameInstance->GetFrameTime());
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::Respawn(Oyster::Math::Float3 spawnPoint)
|
2014-02-14 10:09:03 +01:00
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
this->life = 100;
|
|
|
|
this->playerState = PLAYER_STATE::PLAYER_STATE_IDLE;
|
|
|
|
this->lookDir = Oyster::Math::Float4(1,0,0);
|
2014-02-11 11:46:06 +01:00
|
|
|
this->rigidBody->SetPosition(spawnPoint);
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 13:11:35 +01:00
|
|
|
void Player::Rotate(const Oyster::Math3D::Float3 lookDir, const Oyster::Math3D::Float3 right)
|
2014-01-21 09:52:48 +01:00
|
|
|
{
|
2014-02-12 11:36:08 +01:00
|
|
|
// this is the camera right vector
|
2014-02-12 13:11:35 +01:00
|
|
|
this->lookDir = lookDir;
|
2014-02-11 11:46:06 +01:00
|
|
|
|
2014-02-14 11:52:44 +01:00
|
|
|
//Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1];
|
|
|
|
//this->rigidBody->SetUpAndRight(up, right);
|
2014-01-21 09:52:48 +01:00
|
|
|
}
|
|
|
|
|
2014-01-09 12:51:17 +01:00
|
|
|
void Player::Jump()
|
|
|
|
{
|
2014-02-17 06:44:29 +01:00
|
|
|
this->key_jump = KEY_TIMER;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::IsWalking()
|
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
return (this->playerState == PLAYER_STATE::PLAYER_STATE_WALKING);
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
bool Player::IsJumping()
|
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
return (this->playerState == PLAYER_STATE::PLAYER_STATE_JUMPING);
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
bool Player::IsIdle()
|
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE);
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 11:03:25 +01:00
|
|
|
Oyster::Math::Float3 Player::GetPosition() const
|
2014-01-09 12:51:17 +01:00
|
|
|
{
|
2014-02-11 10:11:38 +01:00
|
|
|
return (Oyster::Math::Float3) this->rigidBody->GetState().centerPos;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
2014-01-16 12:26:14 +01:00
|
|
|
Oyster::Math::Float4x4 Player::GetOrientation() const
|
2014-01-09 12:51:17 +01:00
|
|
|
{
|
2014-02-11 10:11:38 +01:00
|
|
|
return this->rigidBody->GetState().GetOrientation();
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
2014-01-15 11:03:25 +01:00
|
|
|
Oyster::Math::Float3 Player::GetLookDir() const
|
|
|
|
{
|
2014-01-28 15:44:32 +01:00
|
|
|
return this->lookDir;
|
2014-01-15 11:03:25 +01:00
|
|
|
}
|
|
|
|
int Player::GetTeamID() const
|
|
|
|
{
|
|
|
|
return this->teamID;
|
|
|
|
}
|
|
|
|
PLAYER_STATE Player::GetState() const
|
2014-01-09 12:51:17 +01:00
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
return this->playerState;
|
2014-01-09 12:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::DamageLife(int damage)
|
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
this->life -= damage;
|
2014-02-04 11:13:02 +01:00
|
|
|
this->life = 0;
|
|
|
|
|
|
|
|
if(this->life <= 0)
|
|
|
|
{
|
|
|
|
this->life = 0;
|
|
|
|
playerState = PLAYER_STATE_DEAD;
|
2014-02-09 16:42:26 +01:00
|
|
|
this->gameInstance->onDisableFnc(this, 0.0f);
|
2014-02-04 11:13:02 +01:00
|
|
|
}
|
2014-01-15 11:03:25 +01:00
|
|
|
}
|