Danbias/Code/Game/GameLogic/Player.cpp

255 lines
6.4 KiB
C++
Raw Normal View History

#include "Player.h"
#include "Weapon.h"
2014-01-20 15:47:52 +01:00
#include "CollisionManager.h"
#include "Game.h"
using namespace GameLogic;
using namespace Oyster::Physics;
2014-02-14 10:09:03 +01:00
const float MOVE_FORCE = 30;
const float KEY_TIMER = 0.03f;
Player::Player()
:DynamicObject()
{
}
2014-02-12 14:48:58 +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)
{
weapon = new Weapon(2,this);
this->life = 100;
this->teamID = -1;
this->playerState = PLAYER_STATE_IDLE;
this->lookDir = Oyster::Math::Float3(0,0,-1);
this->moveDir = Oyster::Math::Float3(0,0,0);
key_forward = 0;
key_backward = 0;
key_strafeRight = 0;
key_strafeLeft = 0;
2014-01-27 08:54:25 +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
{
this->rigidBody = rigidBody;
Oyster::Math::Float3 centerPosition = Oyster::Math::Float3(0,400,0);
Oyster::Math::Float3 size = Oyster::Math::Float3(0.25f,1.0f,0.5f);
Oyster::Math::Float mass = 60;
Oyster::Math::Float restitutionCoeff = 0.5;
Oyster::Math::Float frictionCoeff_Static = 0.4;
Oyster::Math::Float frictionCoeff_Dynamic = 0.3;
this->rigidBody = Oyster::Physics::API::Instance().AddCollisionBox(size, Oyster::Math::Float4(0, 0, 0, 1), centerPosition, mass, 0.5f, 0.8f, 0.6f );
this->rigidBody->SetAngularFactor(0.0f);
weapon = new Weapon(2,this);
this->life = 100;
this->teamID = teamID;
this->playerState = PLAYER_STATE_IDLE;
2014-02-12 11:36:08 +01:00
this->lookDir = Oyster::Math::Float3(0,0,-1);
key_forward = 0;
key_backward = 0;
key_strafeRight = 0;
key_strafeLeft = 0;
this->moveDir = Oyster::Math::Float3(0,0,0);
this->moveSpeed = 100;
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
2014-01-27 13:56:31 +01:00
}
Player::~Player(void)
{
if(weapon)
{
delete weapon;
weapon = NULL;
}
}
void Player::BeginFrame()
{
2014-02-11 11:46:06 +01:00
//weapon->Update(0.002f);
Object::BeginFrame();
if(this->moveDir != Oyster::Math::Float3::null)
{
Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity();
Oyster::Math::Float3 lostVelocity = (this->previousMoveSpeed - velocity).GetMagnitude()*this->moveDir;
this->rigidBody->SetLinearVelocity(velocity + lostVelocity - this->moveDir*this->moveSpeed );
}
this->moveDir = Oyster::Math::Float3::null;
if (key_forward > 0.001)
{
key_forward -= gameInstance->GetFrameTime(); // fixed timer
this->moveDir += this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz;
}
if (key_backward > 0.001)
{
key_backward -= gameInstance->GetFrameTime();
this->moveDir -= this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz;
}
if (key_strafeRight > 0.001)
{
key_strafeRight -= gameInstance->GetFrameTime();
Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2];
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos;
this->moveDir -= (up).Cross(forward).GetNormalized();
}
if (key_strafeLeft > 0.001)
{
key_strafeLeft -= gameInstance->GetFrameTime();
Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2];
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos;
this->moveDir += (up).Cross(forward).GetNormalized();
}
2014-02-12 13:12:51 +01:00
if(this->moveDir != Oyster::Math::Float3::null)
{
this->moveDir.Normalize();
this->rigidBody->SetLinearVelocity(this->moveDir*this->moveSpeed + this->rigidBody->GetLinearVelocity());
this->previousMoveSpeed = this->rigidBody->GetLinearVelocity();
}
2014-02-12 13:12:51 +01:00
this->weapon->Update(0.01f);
}
void Player::EndFrame()
{
// snap to axis
Oyster::Math::Float4 rotation;
this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized());
2014-02-11 11:46:06 +01:00
Object::EndFrame();
}
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()
{
key_forward = KEY_TIMER;
}
void Player::MoveBackwards()
{
key_backward = KEY_TIMER;
}
void Player::MoveRight()
{
key_strafeRight = KEY_TIMER;
}
void Player::MoveLeft()
{
key_strafeLeft = KEY_TIMER;
}
2014-01-16 11:17:19 +01:00
void Player::UseWeapon(const WEAPON_FIRE &usage)
{
this->weapon->Use(usage,gameInstance->GetFrameTime());
}
void Player::Respawn(Oyster::Math::Float3 spawnPoint)
2014-02-14 10:09:03 +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);
}
void Player::Rotate(const Oyster::Math3D::Float3 lookDir, const Oyster::Math3D::Float3 right)
{
2014-02-12 11:36:08 +01:00
// this is the camera right vector
this->lookDir = lookDir;
2014-02-11 11:46:06 +01:00
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1];
this->rigidBody->SetUpAndRight(up, right);
}
void Player::Jump()
{
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized();
2014-02-12 11:36:08 +01:00
this->rigidBody->ApplyImpulse(up *1500);
2014-02-14 10:09:03 +01:00
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
}
bool Player::IsWalking()
{
return (this->playerState == PLAYER_STATE::PLAYER_STATE_WALKING);
}
bool Player::IsJumping()
{
return (this->playerState == PLAYER_STATE::PLAYER_STATE_JUMPING);
}
bool Player::IsIdle()
{
return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE);
}
Oyster::Math::Float3 Player::GetPosition() const
{
2014-02-11 10:11:38 +01:00
return (Oyster::Math::Float3) this->rigidBody->GetState().centerPos;
}
2014-01-16 12:26:14 +01:00
Oyster::Math::Float4x4 Player::GetOrientation() const
{
2014-02-11 10:11:38 +01:00
return this->rigidBody->GetState().GetOrientation();
}
Oyster::Math::Float3 Player::GetLookDir() const
{
return this->lookDir;
}
int Player::GetTeamID() const
{
return this->teamID;
}
PLAYER_STATE Player::GetState() const
{
return this->playerState;
}
void Player::DamageLife(int damage)
{
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;
this->gameInstance->onDisableFnc(this, 0.0f);
2014-02-04 11:13:02 +01:00
}
}