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

447 lines
12 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"
#include "Camera_FPSV2.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-17 16:16:27 +01:00
#include "Utilities.h"
2014-02-17 14:33:11 +01:00
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-17 16:16:27 +01:00
using namespace ::Utility::String;
2014-02-18 10:28:46 +01:00
using namespace ::Utility::Value;
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;
2014-02-18 12:02:48 +01:00
bool key_Reload_Shaders;
2014-02-17 14:33:11 +01:00
C_Player player;
Camera_FPSV2 camera;
2014-02-17 14:33:11 +01:00
int myId;
2014-02-17 12:02:18 +01:00
2014-02-12 09:49:08 +01:00
} privData;
2014-02-17 16:16:27 +01:00
inline Quaternion ArrayToQuaternion( const float source[4] )
{
return Quaternion( Float3(source[0], source[1], source[2]), source[3] );
}
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-18 10:28:46 +01:00
Graphics::API::Option gfxOp = Graphics::API::GetOption();
Float aspectRatio = gfxOp.Resolution.x / gfxOp.Resolution.y;
this->privData->camera.SetPerspectiveProjection( Radian(90.0f), aspectRatio, 0.1f, 1000.0f );
Graphics::API::SetProjection( this->privData->camera.GetProjectionMatrix() );
2014-02-18 10:28:46 +01:00
//tell server ready
2014-02-18 12:59:51 +01:00
this->privData->nwClient->Send( Protocol_General_Status(Protocol_General_Status::States_ready) );
2014-02-18 10:28:46 +01:00
return true;
}
2014-02-12 16:31:15 +01:00
2014-02-17 16:16:27 +01:00
void GameState::InitiatePlayer( int id, const std::string &modelName, const float position[3], const float rotation[4], const float scale[3], bool isMyPlayer )
{
ModelInitData modelData;
2014-02-17 16:16:27 +01:00
modelData.visible = true;
modelData.position = position;
modelData.rotation = ArrayToQuaternion( rotation );
modelData.scale = scale;
StringToWstring( modelName, modelData.modelPath );
modelData.id = id;
if( isMyPlayer )
{
if( this->privData->player.Init(modelData) )
{
this->privData->myId = id;
this->privData->camera.SetPosition( this->privData->player.getPos() );
2014-02-18 10:28:46 +01:00
Float3 offset = Float3( 0.0f );
2014-02-18 12:59:51 +01:00
offset.y = this->privData->player.getScale().y * 0.9f;
2014-02-18 10:28:46 +01:00
this->privData->camera.SetHeadOffset( offset );
2014-02-17 16:16:27 +01:00
this->privData->camera.UpdateOrientation();
}
}
else
{
C_DynamicObj *p = new C_DynamicObj();
if( p->Init(modelData) )
{
(*this->privData->dynamicObjects)[id] = p;
}
}
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-18 10:28:46 +01:00
this->ReadKeyInput();
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-18 15:09:01 +01:00
if( dynamicObject->second )
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
2014-02-18 12:02:48 +01:00
if( this->privData->input->IsKeyPressed(DIK_R) )
{
if( !this->privData->key_Reload_Shaders )
{
//this->privData->nwClient->Send( Protocol_PlayerMovementRight() );
#ifdef _DEBUG
Graphics::API::ReloadShaders();
#endif
this->privData->key_Reload_Shaders = true;
}
}
else
this->privData->key_Reload_Shaders = false;
2014-01-27 13:56:31 +01:00
//send delta mouse movement
{
2014-02-18 10:28:46 +01:00
this->privData->camera.YawRight( this->privData->input->GetYaw() * 0.017f );
this->privData->camera.PitchDown( this->privData->input->GetPitch() * 0.017f );
2014-02-17 14:33:11 +01:00
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
}
const GameClientState::NetEvent & GameState::DataRecieved( const GameClientState::NetEvent &message )
2013-12-16 11:08:10 +01:00
{
if( message.args.type == NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend )
2014-02-18 16:44:38 +01:00
{ // TODO: Reconnect
const char *breakpoint = "temp trap";
this->privData->nwClient->Disconnect();
this->ChangeState( GameClientState::ClientState_Main );
}
// fetching the id data.
short ID = message.args.data.protocol[0].value.netShort;
2014-02-18 16:44:38 +01:00
if( ProtocolIsGameplay(ID) )
2013-12-19 11:58:42 +01:00
{
CustomNetProtocol data = message.args.data.protocol;
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 );
}
return GameClientState::event_processed;
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
}
return GameClientState::event_processed;
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.SetRotation( 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
}
return GameClientState::event_processed;
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.SetRotation( rotation );
}
2014-02-17 14:33:11 +01:00
C_DynamicObj *object = (*this->privData->dynamicObjects)[decoded.object_ID];
2014-02-18 15:09:01 +01:00
if( object )
{
object->setPos( position );
object->setRot( rotation );
}
}
return GameClientState::event_processed;
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
}
return GameClientState::event_processed;
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;
}
return GameClientState::event_processed;
2014-02-17 16:16:27 +01:00
case protocol_Gameplay_ObjectCreatePlayer:
{
Protocol_ObjectCreatePlayer decoded(data);
2014-02-18 08:53:52 +01:00
this->InitiatePlayer( decoded.object_ID, decoded.meshName, decoded.position, decoded.rotationQ, decoded.scale, decoded.owner );
2014-02-17 16:16:27 +01:00
}
return GameClientState::event_processed;
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
}
return message;
2013-12-13 12:02:49 +01:00
}