Danbias/Code/Game/GameClient/GameClientState/GameState.cpp

382 lines
10 KiB
C++
Raw Normal View History

2013-12-18 12:18:01 +01:00
#include "GameState.h"
#include "DllInterfaces/GFXAPI.h"
#include <Protocols.h>
2013-12-19 10:22:32 +01:00
#include "NetworkClient.h"
2014-02-12 09:02:44 +01:00
#include "Camera_FPS.h"
2014-01-30 14:17:50 +01:00
#include <GameServerAPI.h>
2014-02-17 14:33:11 +01:00
#include "C_obj/C_Player.h"
#include "C_obj/C_DynamicObj.h"
#include "C_obj/C_StaticObj.h"
2014-02-12 16:31:15 +01:00
using namespace ::DanBias::Client;
using namespace ::Oyster;
using namespace ::Oyster::Network;
using namespace ::Oyster::Math3D;
2014-02-17 14:33:11 +01:00
using namespace ::GameLogic;
using namespace ::Utility::DynamicMemory;
2014-02-12 09:02:44 +01:00
2014-02-12 16:31:15 +01:00
struct GameState::MyData
{
2014-02-12 16:31:15 +01:00
MyData(){}
GameClientState::ClientState nextState;
NetworkClient *nwClient;
2014-02-17 11:27:43 +01:00
InputClass *input;
2014-02-17 12:02:18 +01:00
2014-02-17 14:33:11 +01:00
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_StaticObj>> *staticObjects;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_DynamicObj>> *dynamicObjects;
bool key_forward;
bool key_backward;
bool key_strafeRight;
bool key_strafeLeft;
bool key_Shoot;
bool key_Jump;
C_Player player;
Camera_FPS camera;
int myId;
2014-02-17 12:02:18 +01:00
2014-02-12 09:49:08 +01:00
} privData;
2014-02-17 11:27:43 +01:00
GameState::GameState()
{
2014-02-17 14:33:11 +01:00
this->privData = nullptr;
}
2014-02-17 11:27:43 +01:00
GameState::~GameState()
{
2014-02-12 16:31:15 +01:00
if( this->privData )
this->Release();
}
2014-02-12 16:31:15 +01:00
2014-02-17 11:27:43 +01:00
bool GameState::Init( SharedStateContent &shared )
{
2014-02-17 14:33:11 +01:00
// we may assume that shared.network is properly connected
// and there is content in shared.dynamicObjects and shared.staticObjects
this->privData = new MyData();
this->privData->key_forward = false;
this->privData->key_backward = false;
this->privData->key_strafeRight = false;
this->privData->key_strafeLeft = false;
2014-02-12 16:31:15 +01:00
this->privData->nextState = GameClientState::ClientState_Same;
2014-02-17 11:27:43 +01:00
this->privData->nwClient = shared.network;
this->privData->input = shared.input;
2014-02-17 14:33:11 +01:00
this->privData->staticObjects = &shared.staticObjects;
this->privData->dynamicObjects = &shared.dynamicObjects;
2014-02-12 16:31:15 +01:00
//tell server ready
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_General_Status(Protocol_General_Status::States_ready) );
return true;
}
2014-02-12 16:31:15 +01:00
2014-02-17 14:33:11 +01:00
void GameState::InitiatePlayer( int id, std::wstring modelName, Float4x4 world )
{
2014-02-17 14:33:11 +01:00
this->privData->myId = id;
ModelInitData modelData;
modelData.visible = true;
2014-02-12 09:02:44 +01:00
modelData.position = Float3(world[12], world[13], world[14]);
modelData.rotation = Quaternion(Float3(0,0,0), 1);
modelData.scale = Float3(1,1,1);
2014-02-05 15:16:31 +01:00
modelData.modelPath = modelName;
2014-02-17 14:33:11 +01:00
modelData.id = this->privData->myId;
this->privData->player.Init( modelData );
2014-02-12 09:02:44 +01:00
2014-02-17 14:33:11 +01:00
this->privData->camera.SetPosition( this->privData->player.getPos() );
this->privData->camera.UpdateOrientation();
2014-01-31 16:33:16 +01:00
}
2014-02-12 16:31:15 +01:00
2014-02-17 11:27:43 +01:00
GameClientState::ClientState GameState::Update( float deltaTime )
{
2014-02-12 16:31:15 +01:00
return this->privData->nextState;
}
2014-02-12 16:31:15 +01:00
bool GameState::Render()
{
2014-02-17 14:33:11 +01:00
Oyster::Graphics::API::SetView( this->privData->camera.GetViewMatrix() );
Oyster::Graphics::API::NewFrame();
2014-02-17 14:33:11 +01:00
// for debugging to be replaced with render weapon
this->privData->player.Render();
auto staticObject = this->privData->staticObjects->begin();
for( ; staticObject != this->privData->staticObjects->end(); ++staticObject )
{
2014-02-17 14:33:11 +01:00
staticObject->second->Render();
}
2014-02-17 14:33:11 +01:00
auto dynamicObject = this->privData->dynamicObjects->begin();
for( ; dynamicObject != this->privData->dynamicObjects->end(); ++dynamicObject )
{
2014-02-17 14:33:11 +01:00
dynamicObject->second->Render();
}
Oyster::Graphics::API::EndFrame();
return true;
}
2014-02-12 16:31:15 +01:00
bool GameState::Release()
{
2014-02-17 14:33:11 +01:00
if( privData )
{
auto staticObject = this->privData->staticObjects->begin();
for( ; staticObject != this->privData->staticObjects->end(); ++staticObject )
{
staticObject->second = nullptr;
}
2014-02-17 14:33:11 +01:00
auto dynamicObject = this->privData->dynamicObjects->begin();
for( ; dynamicObject != this->privData->dynamicObjects->end(); ++dynamicObject )
{
dynamicObject->second = nullptr;
}
this->privData->staticObjects->clear();
this->privData->dynamicObjects->clear();
privData = NULL;
}
return true;
2013-12-13 12:02:49 +01:00
}
2014-02-12 16:31:15 +01:00
void GameState::ChangeState( ClientState next )
{
this->privData->nextState = next;
}
2014-02-17 14:33:11 +01:00
void GameState::ReadKeyInput()
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_W) )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if(!this->privData->key_forward)
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_PlayerMovementForward() );
this->privData->key_forward = true;
2014-01-27 13:56:31 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_forward = false;
2014-01-27 13:56:31 +01:00
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_S) )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_backward )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_PlayerMovementBackward() );
this->privData->key_backward = true;
2014-01-27 13:56:31 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_backward = false;
2014-01-27 13:56:31 +01:00
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_A) )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_strafeLeft )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_PlayerMovementLeft() );
this->privData->key_strafeLeft = true;
2014-01-27 13:56:31 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_strafeLeft = false;
2014-01-27 13:56:31 +01:00
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_D) )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_strafeRight )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_PlayerMovementRight() );
this->privData->key_strafeRight = true;
2014-01-27 13:56:31 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_strafeRight = false;
2014-01-27 13:56:31 +01:00
//send delta mouse movement
{
2014-02-17 14:33:11 +01:00
this->privData->camera.YawRight( -this->privData->input->GetYaw() );
this->privData->camera.PitchUp( this->privData->input->GetPitch() );
this->privData->camera.UpdateOrientation();
2014-02-17 14:33:11 +01:00
privData->nwClient->Send( Protocol_PlayerLook(this->privData->camera.GetLook(), this->privData->camera.GetRight()) );
2014-01-27 13:56:31 +01:00
}
2014-01-30 09:07:56 +01:00
// shoot
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_Z) )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_Shoot )
2014-01-27 13:56:31 +01:00
{
2014-02-17 14:33:11 +01:00
Protocol_PlayerShot playerShot;
playerShot.primaryPressed = true;
playerShot.secondaryPressed = false;
playerShot.utilityPressed = false;
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( playerShot );
this->privData->key_Shoot = true;
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_Shoot = false;
if( this->privData->input->IsKeyPressed(DIK_X) )
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_Shoot )
{
2014-02-17 14:33:11 +01:00
Protocol_PlayerShot playerShot;
playerShot.primaryPressed = false;
playerShot.secondaryPressed = true;
playerShot.utilityPressed = false;
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( playerShot );
this->privData->key_Shoot = true;
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_Shoot = false;
if( this->privData->input->IsKeyPressed(DIK_C) )
{
2014-02-17 14:33:11 +01:00
if( !this->privData->key_Shoot )
{
2014-02-17 14:33:11 +01:00
Protocol_PlayerShot playerShot;
playerShot.primaryPressed = false;
playerShot.secondaryPressed = false;
playerShot.utilityPressed = true;
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( playerShot );
this->privData->key_Shoot = true;
2014-01-27 13:56:31 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_Shoot = false;
2014-01-27 13:56:31 +01:00
2014-01-30 09:07:56 +01:00
// jump
2014-02-17 14:33:11 +01:00
if( this->privData->input->IsKeyPressed(DIK_SPACE) )
2014-01-30 09:07:56 +01:00
{
2014-02-17 14:33:11 +01:00
if(!this->privData->key_Jump)
2014-01-30 09:07:56 +01:00
{
2014-02-17 14:33:11 +01:00
this->privData->nwClient->Send( Protocol_PlayerJump() );
this->privData->key_Jump = true;
2014-01-30 09:07:56 +01:00
}
}
else
2014-02-17 14:33:11 +01:00
this->privData->key_Jump = false;
2014-01-30 09:07:56 +01:00
2014-02-17 14:33:11 +01:00
// TODO: implement sub-menu
2013-12-16 11:08:10 +01:00
}
2014-02-12 10:43:06 +01:00
void GameState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
2013-12-16 11:08:10 +01:00
{
2014-02-12 09:02:44 +01:00
CustomNetProtocol data = e.args.data.protocol;
short ID = data[0].value.netShort; // fetching the id data.
if( ProtocolIsGameplay(ID) )
2013-12-19 11:58:42 +01:00
{
switch(ID)
2014-01-16 12:26:14 +01:00
{
case protocol_Gameplay_ObjectPickup: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectDamage: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectHealthStatus: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectPosition:
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectPosition decoded(data);
2014-02-12 09:02:44 +01:00
// if is this player. Remember to change camera
2014-02-17 14:33:11 +01:00
if( this->privData->myId == decoded.object_ID )
this->privData->camera.SetPosition( decoded.position );
2014-02-12 09:02:44 +01:00
2014-02-17 14:33:11 +01:00
(*this->privData->dynamicObjects)[decoded.object_ID]->setPos( decoded.position );
}
break;
case protocol_Gameplay_ObjectScale:
2014-02-12 09:02:44 +01:00
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectScale decoded(data);
(*this->privData->dynamicObjects)[decoded.object_ID]->setScale( decoded.scale );
2014-02-12 09:02:44 +01:00
}
break;
case protocol_Gameplay_ObjectRotation:
2014-02-12 09:02:44 +01:00
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectRotation decoded(data);
Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] );
// if is this player. Remember to change camera
2014-02-17 14:33:11 +01:00
if( this->privData->myId == decoded.object_ID )
this->privData->camera.SetAngular( AngularAxis(rotation) );
2014-02-17 14:33:11 +01:00
(*this->privData->dynamicObjects)[decoded.object_ID]->setRot( rotation );
2014-02-12 09:02:44 +01:00
}
break;
case protocol_Gameplay_ObjectPositionRotation:
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectPositionRotation decoded(data);
Float3 position = decoded.position;
Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] );
2013-12-18 12:18:01 +01:00
// if is this player. Remember to change camera
2014-02-17 14:33:11 +01:00
if( this->privData->myId == decoded.object_ID )
{
2014-02-17 14:33:11 +01:00
this->privData->camera.SetPosition( position );
this->privData->camera.SetAngular( AngularAxis(rotation) );
}
2014-02-17 14:33:11 +01:00
C_DynamicObj *object = (*this->privData->dynamicObjects)[decoded.object_ID];
object->setPos( position );
object->setRot( rotation );
}
break;
case protocol_Gameplay_ObjectEnabled: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectDisabled:
2014-02-12 09:02:44 +01:00
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectDisable decoded(data);
2013-12-18 12:18:01 +01:00
2014-02-17 14:33:11 +01:00
auto object = this->privData->dynamicObjects->find( decoded.objectID );
if( object != this->privData->dynamicObjects->end() )
{
2014-02-17 14:33:11 +01:00
object->second = nullptr;
this->privData->dynamicObjects->erase( object );
}
2014-02-12 09:02:44 +01:00
}
break;
case protocol_Gameplay_ObjectCreate:
{
2014-02-17 14:33:11 +01:00
Protocol_ObjectCreate decoded(data);
C_DynamicObj* object = new C_DynamicObj();
2013-12-17 13:39:10 +01:00
ModelInitData modelData;
{
modelData.position = Float3( decoded.position );
modelData.rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] );
modelData.scale = Float3( decoded.scale );
modelData.visible = true;
modelData.id = decoded.object_ID;
2013-12-18 15:28:47 +01:00
::Utility::String::StringToWstring( decoded.name, modelData.modelPath );
}
object->Init(modelData);
2014-02-17 14:33:11 +01:00
(*this->privData->dynamicObjects)[decoded.object_ID] = object;
}
break;
case protocol_Gameplay_ObjectCreatePlayer: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectJoinTeam: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectLeaveTeam: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectWeaponCooldown: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectWeaponEnergy: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectRespawn: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectDie: break; /** @todo TODO: implement */
default: break;
}
}
else if( ProtocolIsGeneral(ID) )
{
switch( ID )
{
case protocol_General_Status: break; /** @todo TODO: implement */
case protocol_General_Text: break; /** @todo TODO: implement */
default: break;
}
2013-12-16 11:08:10 +01:00
}
2013-12-13 12:02:49 +01:00
}