From 4085a5f5e2efcccb11a913e78c313f7cb0b4d2e0 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Mon, 27 Jan 2014 13:56:31 +0100 Subject: [PATCH] GL - added shot protocol --- .../DanBiasGame/GameClientState/GameState.cpp | 239 +++++++++++------- .../DanBiasGame/GameClientState/GameState.h | 1 + .../GameSession/GameSession_Events.cpp | 2 + Code/Game/GameLogic/Player.cpp | 24 +- Code/Game/GameLogic/Player.h | 2 +- Code/Game/GameProtocols/PlayerProtocols.h | 26 ++ .../GameProtocols/ProtocolIdentificationID.h | 13 +- 7 files changed, 201 insertions(+), 106 deletions(-) diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index b86fe931..f133a394 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -4,7 +4,7 @@ #include "C_obj/C_DynamicObj.h" #include #include "NetworkClient.h" - +#include "Camera.h" using namespace DanBias::Client; @@ -17,7 +17,6 @@ struct GameState::myData int modelCount; Oyster::Network::NetworkClient* nwClient; gameStateState state; - }privData; @@ -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) @@ -89,6 +101,15 @@ bool GameState::LoadModels(std::wstring mapFile) modelData.world = modelData.world * translate; 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); @@ -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,84 +157,9 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI // read server data // update objects { - 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; + readKeyInput(KeyInput); + camera->UpdateViewMatrix(); - if(KeyInput->IsKeyPressed(DIK_W)) - { - - if(!key_forward) - { - movePlayer.bForward = true; - send = true; - key_forward = true; - } - } - else - key_forward = false; - - if(KeyInput->IsKeyPressed(DIK_S)) - { - if(!key_backward) - { - movePlayer.bBackward = true; - send = true; - key_backward = true; - } - } - else - key_backward = false; - - if(KeyInput->IsKeyPressed(DIK_A)) - { - if(!key_strafeLeft) - { - movePlayer.bStrafeLeft = true; - send = true; - key_strafeLeft = true; - } - } - else - key_strafeLeft = false; - - if(KeyInput->IsKeyPressed(DIK_D)) - { - if(!key_strafeRight) - { - movePlayer.bStrafeRight = true; - send = true; - key_strafeRight = true; - } - } - else - key_strafeRight = false; - - - if (privData->nwClient->IsConnected() && send) - { - privData->nwClient->Send(movePlayer); - } - - //send delta mouse movement - if (KeyInput->IsMousePressed()) - { - GameLogic::Protocol_PlayerMouse deltaMouseMove; - deltaMouseMove.dxMouse = KeyInput->GetYaw(); - deltaMouseMove.dyMouse = KeyInput->GetPitch(); - //privData->nwClient->Send(deltaMouseMove); - - } - - // send event data - // - if(KeyInput->IsKeyPressed(DIK_L)) - privData->state = GameState::gameStateState_end; } break; case gameStateState_end: @@ -219,10 +174,12 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI } bool GameState::Render() { - Oyster::Graphics::API::SetView(privData->view); + 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 (int i = 0; i < privData->object.size(); i++) + for (unsigned int i = 0; i < privData->object.size(); i++) { privData->object[i]->Render(); } @@ -231,7 +188,7 @@ bool GameState::Render() } bool GameState::Release() { - for (int i = 0; i < privData->object.size(); i++) + for (unsigned int i = 0; i < privData->object.size(); i++) { privData->object[i]->Release(); delete privData->object[i]; @@ -242,6 +199,104 @@ bool GameState::Release() privData = NULL; return true; } +void GameState::readKeyInput(InputClass* KeyInput) +{ + + bool send = false; + GameLogic::Protocol_PlayerMovement movePlayer; + movePlayer.bForward = false; + movePlayer.bBackward = false; + movePlayer.bLeft = false; + movePlayer.bRight = false; + + if(KeyInput->IsKeyPressed(DIK_W)) + { + + if(!key_forward) + { + movePlayer.bForward = true; + send = true; + key_forward = true; + } + } + else + key_forward = false; + + if(KeyInput->IsKeyPressed(DIK_S)) + { + if(!key_backward) + { + movePlayer.bBackward = true; + send = true; + key_backward = true; + } + } + else + key_backward = false; + + if(KeyInput->IsKeyPressed(DIK_A)) + { + if(!key_strafeLeft) + { + movePlayer.bLeft = true; + send = true; + key_strafeLeft = true; + } + } + else + key_strafeLeft = false; + + if(KeyInput->IsKeyPressed(DIK_D)) + { + if(!key_strafeRight) + { + movePlayer.bRight = true; + send = true; + key_strafeRight = true; + } + } + else + key_strafeRight = false; + + + if (privData->nwClient->IsConnected() && send) + { + privData->nwClient->Send(movePlayer); + } + + //send delta mouse movement + if (KeyInput->IsMousePressed()) + { + + + 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; +} 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) { diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.h b/Code/Game/DanBiasGame/GameClientState/GameState.h index a6ed7d3f..30884043 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameState.h @@ -22,6 +22,7 @@ private: bool key_backward; bool key_strafeRight; bool key_strafeLeft; + bool key_Shoot; Camera* camera; struct myData; diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp b/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp index 44392da0..1c1e5127 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp +++ b/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp @@ -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; diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index 92e5b61e..f2f2f02b 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -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) + :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,11 +97,18 @@ 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(); } diff --git a/Code/Game/GameLogic/Player.h b/Code/Game/GameLogic/Player.h index 2b5a64fe..ac799670 100644 --- a/Code/Game/GameLogic/Player.h +++ b/Code/Game/GameLogic/Player.h @@ -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 diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index 9ad819e8..ffb009cf 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -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 diff --git a/Code/Game/GameProtocols/ProtocolIdentificationID.h b/Code/Game/GameProtocols/ProtocolIdentificationID.h index 353338b6..bb0414f8 100644 --- a/Code/Game/GameProtocols/ProtocolIdentificationID.h +++ b/Code/Game/GameProtocols/ProtocolIdentificationID.h @@ -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