Sending dmg, respawn and dead from player to client

This commit is contained in:
lindaandersson 2014-02-21 15:42:09 +01:00
parent b0aa6acdd6
commit 22f52150d5
12 changed files with 185 additions and 120 deletions

View File

@ -33,7 +33,7 @@ bool GamingUI::Init()
// z value should be between 0.5 - 0.9 so that it will be behind other states // z value should be between 0.5 - 0.9 so that it will be behind other states
// add textures and text // add textures and text
this->plane = new Plane_UI(L"box_tex.png", Float3(0.5f, 0.0f, 0.5f), Float2(0.3f, 0.1f)); this->plane = new Plane_UI(L"box_tex.png", Float3(0.5f, 0.0f, 0.5f), Float2(0.3f, 0.1f));
this->text = new Text_UI(L"hej", Float3(0.5f,0.0f,0.1f), Float2(0.1f,0.1f)); this->text = new Text_UI(L"hej", Float3(0.3f,0.0f,0.1f), Float2(0.1f,0.1f));
return true; return true;
} }

View File

@ -43,6 +43,9 @@ Game::Game(void)
: initiated(false) : initiated(false)
, onMoveFnc(0) , onMoveFnc(0)
, onDisableFnc(0) , onDisableFnc(0)
, onDamageTakenFnc(0)
, onRespawnFnc(0)
, onDeadFnc(0)
, frameTime(1.0f/120.0f) , frameTime(1.0f/120.0f)
{} {}
@ -156,7 +159,18 @@ void Game::SetSubscription(GameEvent::ObjectMovedFunction functionPointer)
void Game::SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) void Game::SetSubscription(GameEvent::ObjectDisabledFunction functionPointer)
{ {
this->onDisableFnc = functionPointer; this->onDisableFnc = functionPointer;
}
void Game::SetHpSubscription(GameEvent::ObjectHpFunction functionPointer)
{
this->onDamageTakenFnc = functionPointer;
}
void Game::SetRespawnSubscription(GameEvent::ObjectRespawnedFunction functionPointer)
{
this->onRespawnFnc = functionPointer;
}
void Game::SetDeadSubscription(GameEvent::ObjectDeadFunction functionPointer)
{
this->onDeadFnc = functionPointer;
} }
bool Game::Initiate() bool Game::Initiate()

View File

@ -80,6 +80,10 @@ namespace GameLogic
void SetFrameTimeLength( float seconds ) override; void SetFrameTimeLength( float seconds ) override;
void SetSubscription(GameEvent::ObjectMovedFunction functionPointer) override; void SetSubscription(GameEvent::ObjectMovedFunction functionPointer) override;
void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) override; void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) override;
void SetHpSubscription(GameEvent::ObjectHpFunction functionPointer) override;
void SetRespawnSubscription(GameEvent::ObjectRespawnedFunction functionPointer) override;
void SetDeadSubscription(GameEvent::ObjectDeadFunction functionPointer) override;
bool Initiate() override; bool Initiate() override;
float GetFrameTime() const; float GetFrameTime() const;
@ -91,9 +95,11 @@ namespace GameLogic
LevelData* level; LevelData* level;
float frameTime; float frameTime;
bool initiated; bool initiated;
GameEvent::ObjectDisabledFunction onDisableFnc; GameEvent::ObjectDisabledFunction onDisableFnc;
GameEvent::ObjectMovedFunction onMoveFnc; GameEvent::ObjectMovedFunction onMoveFnc;
GameEvent::ObjectHpFunction onDamageTakenFnc;
GameEvent::ObjectRespawnedFunction onRespawnFnc;
GameEvent::ObjectDeadFunction onDeadFnc;
}; };
} }

View File

@ -27,6 +27,9 @@ namespace GameLogic
{ {
typedef void(*ObjectMovedFunction)(IObjectData* object); // Callback method that recieves and object typedef void(*ObjectMovedFunction)(IObjectData* object); // Callback method that recieves and object
typedef void(*ObjectDisabledFunction)(IObjectData* object, float seconds); // Callback method that recieves and object typedef void(*ObjectDisabledFunction)(IObjectData* object, float seconds); // Callback method that recieves and object
typedef void(*ObjectHpFunction)(IObjectData* object, float hp); // Callback method that sends obj HP
typedef void(*ObjectRespawnedFunction)(IObjectData* object, Oyster::Math::Float3 spawnPos ); // Callback method that sends spawnPos
typedef void(*ObjectDeadFunction)(IObjectData* object, float seconds); // Callback method that sends death timer
//etc... //etc...
}; };
@ -178,6 +181,9 @@ namespace GameLogic
* @param * @param
*/ */
virtual void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) = 0; virtual void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) = 0;
virtual void SetHpSubscription(GameEvent::ObjectHpFunction functionPointer) = 0;
virtual void SetRespawnSubscription(GameEvent::ObjectRespawnedFunction functionPointer) = 0;
virtual void SetDeadSubscription(GameEvent::ObjectDeadFunction functionPointer) = 0;
}; };
} }

View File

@ -422,5 +422,5 @@ void Level::PhysicsOnMoveLevel(const ICustomBody *object)
// function call from physics update when object was moved // function call from physics update when object was moved
Object* temp = (Object*)object->GetCustomTag(); Object* temp = (Object*)object->GetCustomTag();
((Game*)&Game::Instance())->onMoveFnc(temp); ((Game*)&Game::Instance())->onMoveFnc(temp);
} }

View File

@ -66,6 +66,7 @@ namespace GameLogic
int getNrOfDynamicObj(); int getNrOfDynamicObj();
Object* GetObj( int ID ) const; Object* GetObj( int ID ) const;
static void PhysicsOnMoveLevel(const Oyster::Physics::ICustomBody *object); static void PhysicsOnMoveLevel(const Oyster::Physics::ICustomBody *object);

View File

@ -29,6 +29,8 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, void (*EventOnCollision)
key_strafeLeft = 0; key_strafeLeft = 0;
key_jump = 0; key_jump = 0;
invincibleCooldown = 0; invincibleCooldown = 0;
this->deathTimeLeft = 0;
this->deathTime = 1;
this->previousPosition = Oyster::Math::Float3(0,0,0); this->previousPosition = Oyster::Math::Float3(0,0,0);
@ -54,7 +56,8 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustom
key_strafeLeft = 0; key_strafeLeft = 0;
key_jump = 0; key_jump = 0;
invincibleCooldown = 0; invincibleCooldown = 0;
this->deathTimeLeft = 0;
this->deathTime = 1;
this->previousPosition = Oyster::Math::Float3(0,0,0); this->previousPosition = Oyster::Math::Float3(0,0,0);
this->moveDir = Oyster::Math::Float3(0,0,0); this->moveDir = Oyster::Math::Float3(0,0,0);
this->moveSpeed = 20; this->moveSpeed = 20;
@ -74,137 +77,142 @@ Player::~Player(void)
void Player::BeginFrame() void Player::BeginFrame()
{ {
weapon->Update(0.002f); if( this->playerState != PLAYER_STATE_DEAD)
//Object::BeginFrame(); {
weapon->Update(0.002f);
Oyster::Math::Float maxSpeed = 30; Oyster::Math::Float maxSpeed = 30;
// Rotate player accordingly // Rotate player accordingly
this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized()); this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized());
Oyster::Math::Quaternion firstUp = this->rigidBody->GetState().quaternion; Oyster::Math::Quaternion firstUp = this->rigidBody->GetState().quaternion;
this->rigidBody->SetRotationAsAngularAxis(Oyster::Math3D::Float4(this->rigidBody->GetState().centerPos.GetNormalized(), this->rotationUp)); this->rigidBody->SetRotationAsAngularAxis(Oyster::Math3D::Float4(this->rigidBody->GetState().centerPos.GetNormalized(), this->rotationUp));
Oyster::Math::Quaternion secondTurn = this->rigidBody->GetState().quaternion; Oyster::Math::Quaternion secondTurn = this->rigidBody->GetState().quaternion;
this->rigidBody->SetRotation(secondTurn*firstUp); this->rigidBody->SetRotation(secondTurn*firstUp);
// Direction data // Direction data
Oyster::Math::Float4x4 xform; Oyster::Math::Float4x4 xform;
xform = this->rigidBody->GetState().GetOrientation(); xform = this->rigidBody->GetState().GetOrientation();
Oyster::Math::Float3 forwardDir = xform.v[2]; Oyster::Math::Float3 forwardDir = xform.v[2];
Oyster::Math::Float3 upDir = xform.v[1]; Oyster::Math::Float3 upDir = xform.v[1];
Oyster::Math::Float3 rightDir = xform.v[0]; Oyster::Math::Float3 rightDir = xform.v[0];
forwardDir.Normalize(); forwardDir.Normalize();
upDir.Normalize(); upDir.Normalize();
rightDir.Normalize(); rightDir.Normalize();
// Previous velocities data // Previous velocities data
Oyster::Math::Float3 linearVelocity = this->rigidBody->GetLinearVelocity(); Oyster::Math::Float3 linearVelocity = this->rigidBody->GetLinearVelocity();
Oyster::Math::Float3 forwardVelocity = linearVelocity*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z)); Oyster::Math::Float3 forwardVelocity = linearVelocity*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z));
Oyster::Math::Float forwardSpeed = (linearVelocity*forwardDir).GetLength(); Oyster::Math::Float forwardSpeed = (linearVelocity*forwardDir).GetLength();
Oyster::Math::Float3 rightVelocity = linearVelocity*Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z)); Oyster::Math::Float3 rightVelocity = linearVelocity*Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z));
Oyster::Math::Float rightSpeed = (linearVelocity*rightDir).GetLength(); Oyster::Math::Float rightSpeed = (linearVelocity*rightDir).GetLength();
Oyster::Math::Float3 upVelocity = linearVelocity*Oyster::Math::Float3(fabs(upDir.x), fabs(upDir.y), fabs(upDir.z)); Oyster::Math::Float3 upVelocity = linearVelocity*Oyster::Math::Float3(fabs(upDir.x), fabs(upDir.y), fabs(upDir.z));
// Walking data // Walking data
Oyster::Math::Float3 walkDirection = Oyster::Math::Float3(0.0, 0.0, 0.0); Oyster::Math::Float3 walkDirection = Oyster::Math::Float3(0.0, 0.0, 0.0);
Oyster::Math::Float walkSpeed = this->moveSpeed*0.2f; Oyster::Math::Float walkSpeed = this->moveSpeed*0.2f;
// Check for input // Check for input
if(key_forward > 0.001) if(key_forward > 0.001)
{
key_forward -= gameInstance->GetFrameTime();
walkDirection += forwardDir;
}
if(key_backward > 0.001)
{
key_backward -= gameInstance->GetFrameTime();
walkDirection -= forwardDir;
}
if(key_strafeRight > 0.001)
{
key_strafeRight -= gameInstance->GetFrameTime();
walkDirection += rightDir;
}
if(key_strafeLeft > 0.001)
{
key_strafeLeft -= gameInstance->GetFrameTime();
walkDirection -= rightDir;
}
// Dampen velocity if certain keys are not pressed
if(key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.9f)
{
if(key_forward <= 0.001 && key_backward <= 0.001)
{ {
forwardVelocity *= Oyster::Math::Float3(0.2f*fabs(forwardDir.x), 0.2f*fabs(forwardDir.y), 0.2f*fabs(forwardDir.z)); key_forward -= gameInstance->GetFrameTime();
walkDirection += forwardDir;
} }
if(key_strafeRight <= 0.001 && key_strafeLeft <= 0.001) if(key_backward > 0.001)
{ {
rightVelocity *= Oyster::Math::Float3(0.2f*fabs(rightDir.x), 0.2f*fabs(rightDir.y), 0.2f*fabs(rightDir.z)); key_backward -= gameInstance->GetFrameTime();
walkDirection -= forwardDir;
} }
} if(key_strafeRight > 0.001)
// Walk if walkdirection is something
if(walkDirection != Oyster::Math::Float3::null)
{
walkDirection.Normalize();
// If on the ground, accelerate normally
if(this->rigidBody->GetLambda() < 0.9f)
{ {
if(forwardSpeed < maxSpeed) key_strafeRight -= gameInstance->GetFrameTime();
walkDirection += rightDir;
}
if(key_strafeLeft > 0.001)
{
key_strafeLeft -= gameInstance->GetFrameTime();
walkDirection -= rightDir;
}
// Dampen velocity if certain keys are not pressed
if(key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.9f)
{
if(key_forward <= 0.001 && key_backward <= 0.001)
{ {
forwardVelocity += walkDirection*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z)) * walkSpeed; forwardVelocity *= Oyster::Math::Float3(0.2f*fabs(forwardDir.x), 0.2f*fabs(forwardDir.y), 0.2f*fabs(forwardDir.z));
} }
if(rightSpeed < maxSpeed) if(key_strafeRight <= 0.001 && key_strafeLeft <= 0.001)
{ {
rightVelocity += walkDirection*Oyster::Math::Float3(fabs(rightDir.x), abs(rightDir.y), fabs(rightDir.z)) * walkSpeed; rightVelocity *= Oyster::Math::Float3(0.2f*fabs(rightDir.x), 0.2f*fabs(rightDir.y), 0.2f*fabs(rightDir.z));
} }
} }
// If in the air, accelerate slower
if(this->rigidBody->GetLambda() >= 0.9f) // Walk if walkdirection is something
if(walkDirection != Oyster::Math::Float3::null)
{ {
if(forwardSpeed < maxSpeed) walkDirection.Normalize();
// If on the ground, accelerate normally
if(this->rigidBody->GetLambda() < 0.9f)
{ {
forwardVelocity += walkDirection*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z)) * walkSpeed*0.2f; if(forwardSpeed < maxSpeed)
{
forwardVelocity += walkDirection*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z)) * walkSpeed;
}
if(rightSpeed < maxSpeed)
{
rightVelocity += walkDirection*Oyster::Math::Float3(fabs(rightDir.x), abs(rightDir.y), fabs(rightDir.z)) * walkSpeed;
}
} }
if(rightSpeed < maxSpeed) // If in the air, accelerate slower
if(this->rigidBody->GetLambda() >= 0.9f)
{ {
rightVelocity += walkDirection*Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z)) * walkSpeed*0.2f; if(forwardSpeed < maxSpeed)
{
forwardVelocity += walkDirection*Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z)) * walkSpeed*0.2f;
}
if(rightSpeed < maxSpeed)
{
rightVelocity += walkDirection*Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z)) * walkSpeed*0.2f;
}
}
}
// Adjust velocities so no squaring occurs
forwardVelocity *= Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z));
rightVelocity *= Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z));
upVelocity *= Oyster::Math::Float3(fabs(upDir.x), fabs(upDir.y), fabs(upDir.z));
this->rigidBody->SetLinearVelocity(forwardVelocity+rightVelocity+upVelocity);
//Jump
if(key_jump > 0.001)
{
this->key_jump -= this->gameInstance->GetFrameTime();
if(this->rigidBody->GetLambda() < 0.9f)
{
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos.GetNormalized();
this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass*20);
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
} }
} }
} }
else
// Adjust velocities so no squaring occurs
forwardVelocity *= Oyster::Math::Float3(fabs(forwardDir.x), fabs(forwardDir.y), fabs(forwardDir.z));
rightVelocity *= Oyster::Math::Float3(fabs(rightDir.x), fabs(rightDir.y), fabs(rightDir.z));
upVelocity *= Oyster::Math::Float3(fabs(upDir.x), fabs(upDir.y), fabs(upDir.z));
this->rigidBody->SetLinearVelocity(forwardVelocity+rightVelocity+upVelocity);
//Jump
if(key_jump > 0.001)
{ {
this->key_jump -= this->gameInstance->GetFrameTime(); // player is dead
if(this->rigidBody->GetLambda() < 0.9f) // TODO move this logic to lvl
this->deathTimeLeft -= gameInstance->GetFrameTime();
if( this->deathTimeLeft <= 0)
{ {
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos.GetNormalized(); Respawn( Oyster::Math::Float3( -50, 180, 0));
this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass*20);
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
} }
} }
//this->weapon->Update(0.01f);
} }
void Player::EndFrame() void Player::EndFrame()
{ {
//Object::EndFrame();
} }
@ -258,10 +266,15 @@ void Player::UseWeapon(const WEAPON_FIRE &usage)
void Player::Respawn(Oyster::Math::Float3 spawnPoint) void Player::Respawn(Oyster::Math::Float3 spawnPoint)
{ {
this->life = 100; if( this->playerState == PLAYER_STATE_DEAD)
this->playerState = PLAYER_STATE::PLAYER_STATE_IDLE; {
this->lookDir = Oyster::Math::Float4(1,0,0); this->life = 100;
this->rigidBody->SetPosition(spawnPoint); this->playerState = PLAYER_STATE::PLAYER_STATE_IDLE;
//this->lookDir = Oyster::Math::Float4(1,0,0);
this->rigidBody->SetPosition(spawnPoint);
this->gameInstance->onRespawnFnc( this, spawnPoint);
this->gameInstance->onDamageTakenFnc( this, this->life);
}
} }
void Player::SetLookDir(const Oyster::Math3D::Float3& lookDir) void Player::SetLookDir(const Oyster::Math3D::Float3& lookDir)
@ -321,13 +334,17 @@ PLAYER_STATE Player::GetState() const
void Player::DamageLife(int damage) void Player::DamageLife(int damage)
{ {
this->life -= damage; if( this->playerState != PLAYER_STATE_DEAD)
this->life = 0;
if(this->life <= 0)
{ {
this->life = 0; this->life -= damage;
playerState = PLAYER_STATE_DEAD; this->gameInstance->onDamageTakenFnc( this, this->life);
this->gameInstance->onDisableFnc(this, 0.0f);
if(this->life <= 0)
{
this->life = 0;
playerState = PLAYER_STATE_DEAD;
this->deathTimeLeft = this->deathTime;
this->gameInstance->onDeadFnc(this, this->deathTimeLeft);
}
} }
} }

View File

@ -100,6 +100,8 @@ namespace GameLogic
Oyster::Math::Float rotationUp; Oyster::Math::Float rotationUp;
float deathTime;
float deathTimeLeft;
bool hasTakenDamage; bool hasTakenDamage;
float invincibleCooldown; float invincibleCooldown;

View File

@ -75,13 +75,15 @@ namespace GameLogic
} }
Protocol_ObjectDamage(Oyster::Network::CustomNetProtocol& p) Protocol_ObjectDamage(Oyster::Network::CustomNetProtocol& p)
{ {
this->object_ID = p[1].value.netInt;
this->healthLost = p[2].value.netFloat;
} }
Protocol_ObjectDamage(int id, float hp) Protocol_ObjectDamage(int id, float hp)
{ {
this->protocol[0].value = protocol_Gameplay_ObjectDamage; this->protocol[0].value = protocol_Gameplay_ObjectDamage;
this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[0].type = Oyster::Network::NetAttributeType_Short;
this->protocol[1].type = Oyster::Network::NetAttributeType_Int;
this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float;
object_ID = id; object_ID = id;

View File

@ -99,7 +99,10 @@ namespace DanBias
//Callback method recieving from gamelogic //Callback method recieving from gamelogic
static void ObjectMove ( GameLogic::IObjectData* movedObject ); static void ObjectMove ( GameLogic::IObjectData* movedObject );
static void ObjectDisabled ( GameLogic::IObjectData* movedObject, float seconds ); static void ObjectDisabled ( GameLogic::IObjectData* movedObject, float seconds );
static void ObjectDamaged ( GameLogic::IObjectData* movedObject, float hp );
static void ObjectRespawned ( GameLogic::IObjectData* movedObject, Oyster::Math::Float3 spawnPos );
static void ObjectDead ( GameLogic::IObjectData* movedObject, float seconds );
//Private member variables //Private member variables
private: private:
Utility::DynamicMemory::DynamicArray<gClient> gClients; Utility::DynamicMemory::DynamicArray<gClient> gClients;

View File

@ -150,7 +150,18 @@ using namespace DanBias;
{ {
GameSession::gameSession->Send(Protocol_ObjectDisable(movedObject->GetID(), seconds).GetProtocol()); GameSession::gameSession->Send(Protocol_ObjectDisable(movedObject->GetID(), seconds).GetProtocol());
} }
void GameSession::ObjectDamaged( GameLogic::IObjectData* movedObject, float hp )
{
GameSession::gameSession->Send(Protocol_ObjectDamage(movedObject->GetID(), hp).GetProtocol());
}
void GameSession::ObjectRespawned( GameLogic::IObjectData* movedObject, Oyster::Math::Float3 spawnPos )
{
GameSession::gameSession->Send(Protocol_ObjectRespawn(movedObject->GetID(), spawnPos).GetProtocol());
}
void GameSession::ObjectDead( GameLogic::IObjectData* movedObject, float seconds )
{
GameSession::gameSession->Send(Protocol_ObjectDie(movedObject->GetID(), seconds).GetProtocol());
}
//*****************************************************// //*****************************************************//
//****************** Protocol methods *****************// //****************** Protocol methods *****************//
//******************************************************************************************************************// //******************************************************************************************************************//

View File

@ -108,6 +108,9 @@ bool GameSession::Create(GameDescription& desc, bool forceStart)
/* Set some game instance data options */ /* Set some game instance data options */
this->gameInstance.SetSubscription(GameSession::ObjectMove); this->gameInstance.SetSubscription(GameSession::ObjectMove);
this->gameInstance.SetSubscription(GameSession::ObjectDisabled); this->gameInstance.SetSubscription(GameSession::ObjectDisabled);
this->gameInstance.SetHpSubscription(GameSession::ObjectDamaged);
this->gameInstance.SetRespawnSubscription(GameSession::ObjectRespawned);
this->gameInstance.SetDeadSubscription(GameSession::ObjectDead);
this->gameInstance.SetFPS(60); this->gameInstance.SetFPS(60);
this->description.clients.Clear(); this->description.clients.Clear();