GL - added shot protocol

This commit is contained in:
lindaandersson 2014-01-27 13:56:31 +01:00
parent 5b984db9ba
commit 4085a5f5e2
7 changed files with 201 additions and 106 deletions

View File

@ -4,7 +4,7 @@
#include "C_obj/C_DynamicObj.h"
#include <Protocols.h>
#include "NetworkClient.h"
#include "Camera.h"
using namespace DanBias::Client;
@ -19,7 +19,6 @@ struct GameState::myData
gameStateState state;
}privData;
GameState::GameState(void)
@ -38,22 +37,35 @@ GameState::~GameState(void)
bool GameState::Init(Oyster::Network::NetworkClient* nwClient)
{
// load models
camera = new Camera;
privData = new myData();
privData->state = gameStateState_loading;
privData->nwClient = nwClient;
privData->state = LoadGame();
return true;
}
GameState::gameStateState GameState::LoadGame()
{
Oyster::Graphics::Definitions::Pointlight plight;
plight.Pos = Oyster::Math::Float3(0,3,0);
plight.Pos = Oyster::Math::Float3(0,15,5);
plight.Color = Oyster::Math::Float3(0,1,0);
plight.Radius = 5;
plight.Radius = 50;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
plight.Pos = Oyster::Math::Float3(10,15,5);
plight.Color = Oyster::Math::Float3(1,0,0);
plight.Radius = 50;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
plight.Pos = Oyster::Math::Float3(10,-15,5);
plight.Color = Oyster::Math::Float3(0,0,1);
plight.Radius = 50;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
LoadModels(L"map");
InitCamera(Oyster::Math::Float3(0,0,5.4f));
InitCamera(Oyster::Math::Float3(0,0,20.0f));
return gameStateState_playing;
}
bool GameState::LoadModels(std::wstring mapFile)
@ -90,6 +102,15 @@ bool GameState::LoadModels(std::wstring mapFile)
modelData.modelPath = L"..\\Content\\Models\\char_white.dan";
modelData.id ++;
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(0,0,0));
Oyster::Math3D::Float4x4 scale = Oyster::Math3D::Float4x4::identity;
scale.v[0].x = 8;
scale.v[1].y = 8;
scale.v[2].z = 8;
modelData.world = scale; //modelData.world * translate
modelData.modelPath = L"ball.dan";
modelData.id ++;
obj = new C_DynamicObj();
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
@ -99,10 +120,19 @@ bool GameState::LoadModels(std::wstring mapFile)
}
bool GameState::InitCamera(Oyster::Math::Float3 startPos)
{
Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,-1);
Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0);
Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 20);
camera->LookAt(pos, dir, up);
camera->SetLens(3.14f/2, 1024/768, 1, 1000);
privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000);
//privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
Oyster::Graphics::API::SetProjection(privData->proj);
camera->UpdateViewMatrix();
privData->view = camera->View();
privData->view = Oyster::Math3D::ViewMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view);
return true;
@ -127,14 +157,57 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI
// read server data
// update objects
{
readKeyInput(KeyInput);
camera->UpdateViewMatrix();
}
break;
case gameStateState_end:
return ClientState_Lobby;
break;
default:
break;
}
// send key input to server.
return ClientState_Same;
}
bool GameState::Render()
{
Oyster::Graphics::API::SetView(camera->View());
//Oyster::Graphics::API::SetProjection(camera->Proj());
//Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection(privData->proj);
Oyster::Graphics::API::NewFrame();
for (unsigned int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Render();
}
Oyster::Graphics::API::EndFrame();
return true;
}
bool GameState::Release()
{
for (unsigned int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Release();
delete privData->object[i];
privData->object[i] = NULL;
}
delete privData;
privData = NULL;
return true;
}
void GameState::readKeyInput(InputClass* KeyInput)
{
bool send = false;
GameLogic::Protocol_PlayerMovement movePlayer;
movePlayer.bForward = false;
movePlayer.bBackward = false;
movePlayer.bStrafeLeft = false;
movePlayer.bStrafeRight = false;
movePlayer.bTurnLeft = false;
movePlayer.bTurnRight = false;
movePlayer.bLeft = false;
movePlayer.bRight = false;
if(KeyInput->IsKeyPressed(DIK_W))
{
@ -165,7 +238,7 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI
{
if(!key_strafeLeft)
{
movePlayer.bStrafeLeft = true;
movePlayer.bLeft = true;
send = true;
key_strafeLeft = true;
}
@ -177,7 +250,7 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI
{
if(!key_strafeRight)
{
movePlayer.bStrafeRight = true;
movePlayer.bRight = true;
send = true;
key_strafeRight = true;
}
@ -194,53 +267,35 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI
//send delta mouse movement
if (KeyInput->IsMousePressed())
{
GameLogic::Protocol_PlayerMouse deltaMouseMove;
deltaMouseMove.dxMouse = KeyInput->GetYaw();
deltaMouseMove.dyMouse = KeyInput->GetPitch();
//privData->nwClient->Send(deltaMouseMove);
camera->Yaw(KeyInput->GetYaw());
camera->Pitch(KeyInput->GetPitch());
camera->UpdateViewMatrix();
GameLogic::Protocol_PlayerLook playerLookDir;
Oyster::Math::Float3 look = camera->GetLook();
playerLookDir.lookDirX = look.x;
playerLookDir.lookDirY = look.y;
playerLookDir.lookDirZ = look.z;
privData->nwClient->Send(playerLookDir);
}
if(KeyInput->IsKeyPressed(DIK_Z))
{
if(!key_Shoot)
{
GameLogic::Protocol_PlayerShot playerShot;
playerShot.hasShot = true;
privData->nwClient->Send(playerShot);
key_Shoot = true;
}
}
else
key_Shoot = false;
// send event data
//
if(KeyInput->IsKeyPressed(DIK_L))
privData->state = GameState::gameStateState_end;
}
break;
case gameStateState_end:
return ClientState_Lobby;
break;
default:
break;
}
// send key input to server.
return ClientState_Same;
}
bool GameState::Render()
{
Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection(privData->proj);
Oyster::Graphics::API::NewFrame();
for (int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Render();
}
Oyster::Graphics::API::EndFrame();
return true;
}
bool GameState::Release()
{
for (int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Release();
delete privData->object[i];
privData->object[i] = NULL;
}
delete privData;
privData = NULL;
return true;
}
void GameState::Protocol(ProtocolStruct* pos)
@ -267,14 +322,16 @@ void GameState::Protocol( ObjPos* pos )
world[i] = pos->worldPos[i];
}
for (int i = 0; i < privData->object.size(); i++)
for (unsigned int i = 0; i < privData->object.size(); i++)
{
if(privData->object[i]->GetId() == pos->object_ID)
{
privData->object[i]->setPos(world);
//privData->view = world;
//privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view);
//camera->setRight((Oyster::Math::Float3(world[0], world[1], world[2])));
//camera->setUp((Oyster::Math::Float3(world[4], world[5], world[6])));
//camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10])));
camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14]));
camera->UpdateViewMatrix();
}
}
@ -306,7 +363,7 @@ void GameState::Protocol( NewObj* newObj )
void DanBias::Client::GameState::Protocol( RemoveObj* obj )
{
for (int i = 0; i < privData->object.size(); i++)
for (unsigned int i = 0; i < privData->object.size(); i++)
{
if(privData->object[i]->GetId() == obj->object_ID)
{

View File

@ -22,6 +22,7 @@ private:
bool key_backward;
bool key_strafeRight;
bool key_strafeLeft;
bool key_Shoot;
Camera* camera;
struct myData;

View File

@ -67,6 +67,8 @@ namespace DanBias
case protocol_Gameplay_PlayerChangeWeapon:
break;
case protocol_Gameplay_PlayerShot:
c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_PRIMARY_PRESS);
case protocol_Gameplay_ObjectDamage:
break;

View File

@ -18,14 +18,13 @@ Player::Player()
lookDir = Oyster::Math::Float4(0,0,-1,0);
setState.SetCenterPosition(Oyster::Math::Float4(0,15,0,1));
setState.SetReach(Oyster::Math::Float4(2,3.5,2,0));
//setState.SetRotation(Oyster::Math::Float4(0,0,0,0).Normalize());
}
Player::Player(Oyster::Physics::ICustomBody *rigidBody ,void (*collisionFunc)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), OBJECT_TYPE type)
:DynamicObject(rigidBody, collisionFunc, type)
{
}
Player::~Player(void)
{
delete weapon;
@ -70,6 +69,7 @@ void Player::MoveBackwards()
void Player::MoveRight()
{
//Do cross product with forward vector and negative gravity vector
// temp up vector
Oyster::Math::Float4 r = Oyster::Math::Float4(1, 0, 0, 0 );
//Oyster::Math::Float4 r = (-rigidBody->GetGravityNormal()).Cross((Oyster::Math::Float3)this->lookDir);
setState.ApplyLinearImpulse(r * 20 * this->gameInstance->GetFrameTime());
@ -78,6 +78,7 @@ void Player::MoveRight()
void Player::MoveLeft()
{
//Do cross product with forward vector and negative gravity vector
// temp up vector
Oyster::Math::Float4 r = Oyster::Math::Float4(1, 0, 0, 0 );
//Oyster::Math::Float4 r1 = -(-rigidBody->GetGravityNormal()).Cross((Oyster::Math::Float3)this->lookDir); //Still get zero
setState.ApplyLinearImpulse(-r * 20 * this->gameInstance->GetFrameTime());
@ -96,10 +97,17 @@ void Player::Respawn(Oyster::Math::Float3 spawnPoint)
this->lookDir = Oyster::Math::Float4(1,0,0);
}
void Player::Rotate(float dx, float dy)
void Player::Rotate(const Oyster::Math3D::Float3 lookDir)
{
//this->lookDir = lookDir;
this->lookDir = lookDir;
Oyster::Math::Float4 up(0,1,0,0);//-setState.GetGravityNormal();
Oyster::Math::Float4 pos = setState.GetCenterPosition();
Oyster::Math::Float4x4 world = Oyster::Math3D::OrientationMatrix_LookAtDirection(lookDir, up.xyz, pos.xyz);
// cant set rotation
//setState.SetOrientation(world);
//this->lookDir = lookDir - up.xyz;
//this->setState.AddRotation(Oyster::Math::Float4(x, y));
//this->setState.SetRotation();

View File

@ -43,7 +43,7 @@ namespace GameLogic
void Respawn(Oyster::Math::Float3 spawnPoint);
void Rotate(float x, float y);
void Rotate(const Oyster::Math3D::Float3 lookDir);
/********************************************************
* Collision function for player, this is to be sent to physics through the subscribe function with the rigidbody

View File

@ -122,6 +122,32 @@ namespace GameLogic
Oyster::Network::CustomNetProtocol protocol;
};
struct Protocol_PlayerShot :public Oyster::Network::CustomProtocolObject
{
bool hasShot;
Protocol_PlayerShot()
{
this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerShot;
this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short;
this->protocol[1].type = Oyster::Network::NetAttributeType_Bool;
}
const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val)
{
hasShot = val[1].value.netBool;
return *this;
}
Oyster::Network::CustomNetProtocol* GetProtocol() override
{
this->protocol[1].value = hasShot;
return &protocol;
}
private:
Oyster::Network::CustomNetProtocol protocol;
};
}
#endif // !GAMELOGIC_PLAYER_PROTOCOLS_H

View File

@ -49,12 +49,13 @@
#define protocol_Gameplay_PlayerMovement 300
#define protocol_Gameplay_PlayerLookDir 301
#define protocol_Gameplay_PlayerChangeWeapon 302
#define protocol_Gameplay_ObjectPickup 303
#define protocol_Gameplay_ObjectDamage 304
#define protocol_Gameplay_ObjectPosition 305
#define protocol_Gameplay_ObjectEnabled 306
#define protocol_Gameplay_ObjectDisabled 307
#define protocol_Gameplay_ObjectCreate 308
#define protocol_Gameplay_PlayerShot 303
#define protocol_Gameplay_ObjectPickup 304
#define protocol_Gameplay_ObjectDamage 305
#define protocol_Gameplay_ObjectPosition 306
#define protocol_Gameplay_ObjectEnabled 307
#define protocol_Gameplay_ObjectDisabled 308
#define protocol_Gameplay_ObjectCreate 309
#define protocol_GameplayMAX 399