Health pickups is now working!

This commit is contained in:
Pontus Fransson 2014-02-26 10:23:38 +01:00 committed by Dander7BD
parent 8a65907625
commit 28e84854d1
7 changed files with 101 additions and 30 deletions

View File

@ -146,8 +146,8 @@ void GameState::InitiatePlayer( int id, const std::string &modelName, const floa
this->privData->camera.SetPosition( p->getPos() ); this->privData->camera.SetPosition( p->getPos() );
Float3 offset = Float3( 0.0f ); Float3 offset = Float3( 0.0f );
// DEBUG position of camera so we can see the player model // DEBUG position of camera so we can see the player model
offset.y = p->getScale().y * 5.0f; //offset.y = p->getScale().y * 5.0f;
offset.z = p->getScale().z * -5.0f; //offset.z = p->getScale().z * -5.0f;
// !DEBUG // !DEBUG
this->privData->camera.SetHeadOffset( offset ); this->privData->camera.SetHeadOffset( offset );
this->privData->camera.UpdateOrientation(); this->privData->camera.UpdateOrientation();
@ -191,7 +191,7 @@ bool GameState::Render()
{ {
if(playerObject->second) if(playerObject->second)
{ {
//if( this->privData->myId != playerObject->second->GetId() ) if( this->privData->myId != playerObject->second->GetId() )
{ {
playerObject->second->Render(); playerObject->second->Render();
} }
@ -601,6 +601,12 @@ const GameClientState::NetEvent & GameState::DataRecieved( const GameClientState
{ {
// if it is not a player // if it is not a player
object = (*this->privData->dynamicObjects)[decoded.objectID]; object = (*this->privData->dynamicObjects)[decoded.objectID];
if(!object)
{
//If it is a static object
object = (*this->privData->staticObjects)[decoded.objectID];
}
} }
if( object ) if( object )
@ -618,6 +624,12 @@ const GameClientState::NetEvent & GameState::DataRecieved( const GameClientState
{ {
// if it is not a player // if it is not a player
object = (*this->privData->dynamicObjects)[decoded.objectID]; object = (*this->privData->dynamicObjects)[decoded.objectID];
if(!object)
{
//If it is a static object
object = (*this->privData->staticObjects)[decoded.objectID];
}
} }
if( object ) if( object )

View File

@ -44,7 +44,6 @@ using namespace GameLogic;
realObjB = realObjA; realObjB = realObjA;
} }
switch (realObjB->GetObjectType()) switch (realObjB->GetObjectType())
{ {
case ObjectSpecialType::ObjectSpecialType_Generic: case ObjectSpecialType::ObjectSpecialType_Generic:
@ -375,9 +374,14 @@ using namespace GameLogic;
return; return;
if(b->GetObjectType() == ObjectSpecialType_Player) if(b->GetObjectType() == ObjectSpecialType_Player)
{
//Only update if it is active. And if the player is alive
if(((Pickup*)a)->IsActive() && ((Player*)b)->GetState() != PLAYER_STATE_DEAD && ((Player*)b)->GetState() != PLAYER_STATE_DIED)
{ {
((Pickup*)a)->OnCollision((Player*)(b)); ((Pickup*)a)->OnCollision((Player*)(b));
} }
return;
}
else if(a->GetObjectType() != ObjectSpecialType_Player) else if(a->GetObjectType() != ObjectSpecialType_Player)
{ {
//One of the objects are not a player. //One of the objects are not a player.
@ -385,5 +389,9 @@ using namespace GameLogic;
return; return;
} }
//Only update if it is active. And if the player is alive
if(((Pickup*)b)->IsActive() && ((Player*)a)->GetState() != PLAYER_STATE_DEAD && ((Player*)a)->GetState() != PLAYER_STATE_DIED)
{
((Pickup*)b)->OnCollision((Player*)a); ((Pickup*)b)->OnCollision((Player*)a);
} }
}

View File

@ -9,6 +9,7 @@ Pickup::Pickup(Oyster::Physics::ICustomBody *rigidBody, EventOnCollision collisi
this->active = true; this->active = true;
this->spawnTime = spawnTime; this->spawnTime = spawnTime;
timer.reset(); timer.reset();
this->GetRigidBody()->MoveToLimbo();
} }
Pickup::~Pickup() Pickup::~Pickup()

View File

@ -1,4 +1,5 @@
#include "PickupHealth.h" #include "PickupHealth.h"
#include "../Game.h"
using namespace GameLogic; using namespace GameLogic;
@ -14,5 +15,8 @@ PickupHealth::~PickupHealth()
void PickupHealth::OnCollision(Player *player) void PickupHealth::OnCollision(Player *player)
{ {
timer.reset(); timer.reset();
((Game*)&Game::Instance())->onDisableFnc(this);
this->active = false;
player->DamageLife(-hpValue); player->DamageLife(-hpValue);
} }

View File

@ -12,24 +12,26 @@ Player::Player()
:DynamicObject() :DynamicObject()
{ {
Player::initPlayerData(); Player::initPlayerData();
AffectedObjects.Reserve(15);
this->weapon = NULL;
this->teamID = -1; this->teamID = -1;
} }
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) 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) :DynamicObject(rigidBody, EventOnCollision, type, objectID)
{ {
weapon = new Weapon(2,this); this->weapon = new Weapon(2,this);
Player::initPlayerData(); Player::initPlayerData();
AffectedObjects.Reserve(15);
this->teamID = teamID; this->teamID = teamID;
} }
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) 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) :DynamicObject(rigidBody, EventOnCollision, type, objectID)
{ {
weapon = new Weapon(2,this); this->weapon = new Weapon(2,this);
Player::initPlayerData(); Player::initPlayerData();
AffectedObjects.Reserve(15);
this->teamID = teamID; this->teamID = teamID;
} }
@ -70,8 +72,12 @@ void Player::BeginFrame()
Oyster::Math::Float maxSpeed = 30; Oyster::Math::Float maxSpeed = 30;
// Rotate player accordingly // Rotate player accordingly
this->rigidBody->AddRotationAroundY(this->rotationUp);
this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized()); this->rigidBody->SetUp(this->rigidBody->GetState().centerPos.GetNormalized());
Oyster::Math::Quaternion firstUp = this->rigidBody->GetState().quaternion;
this->rigidBody->SetRotationAsAngularAxis(Oyster::Math3D::Float4(this->rigidBody->GetState().centerPos.GetNormalized(), this->rotationUp));
Oyster::Math::Quaternion secondTurn = this->rigidBody->GetState().quaternion;
this->rigidBody->SetRotation(secondTurn*firstUp);
// Direction data // Direction data
Oyster::Math::Float4x4 xform; Oyster::Math::Float4x4 xform;
@ -119,7 +125,7 @@ void Player::BeginFrame()
} }
// Dampen velocity if certain keys are not pressed // Dampen velocity if certain keys are not pressed
if(key_jump <= 0.001 && IsWalking()) if(key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.9f)
{ {
if(key_forward <= 0.001 && key_backward <= 0.001) if(key_forward <= 0.001 && key_backward <= 0.001)
{ {
@ -146,7 +152,7 @@ void Player::BeginFrame()
walkDirection.Normalize(); walkDirection.Normalize();
// If on the ground, accelerate normally // If on the ground, accelerate normally
if(IsWalking()) if(this->rigidBody->GetLambda() < 0.9f)
{ {
if(forwardSpeed < maxSpeed) if(forwardSpeed < maxSpeed)
{ {
@ -158,7 +164,7 @@ void Player::BeginFrame()
} }
} }
// If in the air, accelerate slower // If in the air, accelerate slower
if(IsJumping()) if(this->rigidBody->GetLambda() >= 0.9f)
{ {
if(forwardSpeed < maxSpeed) if(forwardSpeed < maxSpeed)
{ {
@ -188,7 +194,7 @@ void Player::BeginFrame()
if(key_jump > 0.001) if(key_jump > 0.001)
{ {
this->key_jump -= this->gameInstance->GetFrameTime(); this->key_jump -= this->gameInstance->GetFrameTime();
if(IsWalking()) if(this->rigidBody->GetLambda() < 0.9f)
{ {
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos.GetNormalized(); Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos.GetNormalized();
this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass * 20); this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass * 20);
@ -211,6 +217,16 @@ void Player::BeginFrame()
void Player::EndFrame() void Player::EndFrame()
{ {
//check if there are any objects that can be removed from the AffectedObjects list
for(int i = 0; i < this->AffectedObjects.Size(); i++)
{
if(this->AffectedObjects[i] && (this->AffectedObjects[i]->GetRigidBody()->GetState().previousVelocity).GetMagnitude() <= 0.1f)
{
this->AffectedObjects[i]->RemoveAffectedBy();
this->AffectedObjects.Remove(i);
}
}
} }
void Player::Move(const PLAYER_MOVEMENT &movement) void Player::Move(const PLAYER_MOVEMENT &movement)
@ -280,7 +296,7 @@ void Player::SetLookDir(const Oyster::Math3D::Float3& lookDir)
} }
void Player::TurnLeft(Oyster::Math3D::Float deltaRadians) void Player::TurnLeft(Oyster::Math3D::Float deltaRadians)
{ {
this->rotationUp = deltaRadians; this->rotationUp += deltaRadians;
} }
void Player::Jump() void Player::Jump()
@ -290,15 +306,15 @@ void Player::Jump()
bool Player::IsWalking() bool Player::IsWalking()
{ {
return (this->rigidBody->GetLambda() < 0.99f); return (this->playerState == PLAYER_STATE::PLAYER_STATE_WALKING);
} }
bool Player::IsJumping() bool Player::IsJumping()
{ {
return (this->rigidBody->GetLambda() == 1.0f); return (this->playerState == PLAYER_STATE::PLAYER_STATE_JUMPING);
} }
bool Player::IsIdle() bool Player::IsIdle()
{ {
return (this->rigidBody->GetLambda() == 1.0f && this->rigidBody->GetLinearVelocity().GetMagnitude() < 0.0001f); return (this->playerState == PLAYER_STATE::PLAYER_STATE_IDLE);
} }
void Player::Inactivate() void Player::Inactivate()
@ -329,16 +345,38 @@ PLAYER_STATE Player::GetState() const
void Player::DamageLife(int damage) void Player::DamageLife(int damage)
{ {
this->life -= damage; if(damage != 0)
this->life = 0;
if(this->life <= 0)
{ {
this->life = 0; this->playerStats.hp -= damage;
playerState = PLAYER_STATE_DEAD;
this->gameInstance->onDisableFnc(this, 0.0f); if(this->playerStats.hp > 100)
this->playerStats.hp = 100;
// send hp to client
this->gameInstance->onDamageTakenFnc( this, this->playerStats.hp);
if(this->playerStats.hp <= 0)
{
this->playerStats.hp = 0;
this->playerState = PLAYER_STATE_DIED;
} }
} }
}
void Player::AddAffectedObject(DynamicObject &AffectedObject)
{
//check if object already exists in the list, if so then do not add
for(int i = 0; i < AffectedObjects.Size(); i++)
{
if(AffectedObjects[i]->GetID() == AffectedObject.GetID())
{
//object already exists, exit function
return;
}
}
//else you add the object to the stack
AffectedObjects.Push(&AffectedObject);
}
bool Player::deathTimerTick(float dt) bool Player::deathTimerTick(float dt)
{ {
this->deathTimer -= dt; this->deathTimer -= dt;

View File

@ -152,7 +152,7 @@ using namespace DanBias;
} }
void GameSession::ObjectEnabled( GameLogic::IObjectData* movedObject ) void GameSession::ObjectEnabled( GameLogic::IObjectData* movedObject )
{ {
GameSession::gameSession->Send(Protocol_ObjectDisable(movedObject->GetID()).GetProtocol()); GameSession::gameSession->Send(Protocol_ObjectEnable(movedObject->GetID()).GetProtocol());
} }
void GameSession::ObjectDamaged( GameLogic::IObjectData* movedObject, float hp ) void GameSession::ObjectDamaged( GameLogic::IObjectData* movedObject, float hp )
{ {

View File

@ -168,6 +168,14 @@ std::vector<SmartPointer<ObjectTypeHeader>> LevelParser::Parse(std::string filen
ParseObject(&buffer[counter], &header->healthValue, 4); ParseObject(&buffer[counter], &header->healthValue, 4);
counter += 4; counter += 4;
// DEBUG
header->position[1] = 150;
header->spawnTime = 5;
header->boundingVolume.box.mass = 0;
header->typeID = ObjectType_Static;
header->healthValue = 50;
// !DEBUG
objects.push_back(header); objects.push_back(header);
break; break;
} }