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

515 lines
15 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-19 10:59:23 +01:00
#include "C_Light.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-20 16:35:49 +01:00
#include "GamingUI.h"
#include "RespawnUI.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;
2014-02-19 10:59:23 +01:00
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_Light>> *lights;
2014-02-17 14:33:11 +01:00
bool key_forward;
bool key_backward;
bool key_strafeRight;
bool key_strafeLeft;
bool key_Shoot;
bool key_Jump;
2014-02-18 15:54:09 +01:00
// DEGUG KEYS
2014-02-18 12:02:48 +01:00
bool key_Reload_Shaders;
2014-02-18 15:54:09 +01:00
bool key_Wireframe_Toggle;
bool renderWhireframe;
// !DEGUG KEYS
2014-02-18 12:02:48 +01:00
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-19 10:59:23 +01:00
this->privData->lights = &shared.lights;
2014-02-18 10:28:46 +01:00
Graphics::API::Option gfxOp = Graphics::API::GetOption();
Float aspectRatio = gfxOp.Resolution.x / gfxOp.Resolution.y;
2014-02-19 10:59:23 +01:00
this->privData->camera.SetPerspectiveProjection( Utility::Value::Radian(90.0f), aspectRatio, 0.1f, 1000.0f );
2014-02-18 10:28:46 +01:00
Graphics::API::SetProjection( this->privData->camera.GetProjectionMatrix() );
2014-02-20 15:15:54 +01:00
gfxOp.AmbientValue = 0.5f;
gfxOp.GlobalGlowTint = Math::Float3(2,1,1);
gfxOp.GlobalTint = Math::Float3(1,1,1);
2014-02-19 15:57:52 +01:00
Graphics::API::SetOptions(gfxOp);
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
2014-02-18 15:54:09 +01:00
// DEGUG KEYS
this->privData->key_Reload_Shaders = false;
this->privData->key_Wireframe_Toggle = false;
this->privData->renderWhireframe = false;
// !DEGUG KEYS
2014-02-19 10:59:23 +01:00
auto light = this->privData->lights->begin();
for( ; light != this->privData->lights->end(); ++light )
{
light->second->Render();
}
2014-02-20 16:35:49 +01:00
// create UI states
this->gameUI = new GamingUI(this->privData->input, this->privData->nwClient, &this->privData->camera);
this->respawnUI = new RespawnUI(this->privData->nwClient, 20);
this->currGameUI = gameUI;
((GamingUI*)gameUI)->Init();
// TODO init respawn
2014-02-19 15:57:52 +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;
// RB DEBUG
RBInitData RBData;
RBData.position = position;
RBData.rotation = ArrayToQuaternion( rotation );
2014-02-19 13:41:05 +01:00
RBData.scale = scale;
RBData.type = RB_Type_Cube;
2014-02-18 15:07:40 +01:00
// !RB DEBUG
2014-02-17 16:16:27 +01:00
if( isMyPlayer )
{
if( this->privData->player.Init(modelData) )
{
// RB DEBUG
this->privData->player.InitRB( RBData );
2014-02-18 15:07:40 +01:00
// !RB DEBUG
2014-02-17 16:16:27 +01:00
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 );
// DEBUG position of camera so we can see the player model
//offset.y = this->privData->player.getScale().y * 5.0f;
//offset.z = this->privData->player.getScale().z * -5.0f;
// !DEBUG
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) )
{
// RB DEBUG
this->privData->player.InitRB( RBData );
2014-02-18 15:07:40 +01:00
// !RB DEBUG
2014-02-17 16:16:27 +01:00
(*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-20 16:35:49 +01:00
GameStateUI::UIState UIstate = this->currGameUI->Update( deltaTime );
switch (UIstate)
{
case DanBias::Client::GameStateUI::UIState_same:
break;
case DanBias::Client::GameStateUI::UIState_gaming:
break;
case DanBias::Client::GameStateUI::UIState_main_menu:
//this->privData->nextState =
break;
case DanBias::Client::GameStateUI::UIState_shut_down:
this->privData->nextState = ClientState_Quit;
break;
default:
break;
}
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 )
2014-02-20 12:07:53 +01:00
{
2014-02-18 15:09:01 +01:00
dynamicObject->second->Render();
2014-02-20 15:47:11 +01:00
2014-02-20 12:07:53 +01:00
}
}
#ifdef _DEBUG
//RB DEBUG render wire frame
if(this->privData->renderWhireframe)
{
Oyster::Graphics::API::StartRenderWireFrame();
Oyster::Math3D::Float4x4 translation = Oyster::Math3D::TranslationMatrix(Float3( 0,132, 20));
Oyster::Math3D::Float4x4 scale = Oyster::Math3D::ScalingMatrix(Float3( 0.5f, 0.5f, 0.5f));
Oyster::Math3D::Float4x4 world = translation * scale;
Oyster::Graphics::API::RenderDebugCube( world );
Oyster::Graphics::API::RenderDebugCube(this->privData->player.getRBWorld());
staticObject = this->privData->staticObjects->begin();
for( ; staticObject != this->privData->staticObjects->end(); ++staticObject )
{
if( staticObject->second->getBRtype() == RB_Type_Cube)
{
Oyster::Graphics::API::RenderDebugCube( staticObject->second->getRBWorld());
}
if( staticObject->second->getBRtype() == RB_Type_Sphere)
{
Oyster::Graphics::API::RenderDebugSphere( staticObject->second->getRBWorld());
}
}
dynamicObject = this->privData->dynamicObjects->begin();
for( ; dynamicObject != this->privData->dynamicObjects->end(); ++dynamicObject )
{
if( dynamicObject->second )
{
if( dynamicObject->second->getBRtype() == RB_Type_Cube)
{
Oyster::Graphics::API::RenderDebugCube( dynamicObject->second->getRBWorld());
}
if( dynamicObject->second->getBRtype() == RB_Type_Sphere)
{
Oyster::Graphics::API::RenderDebugSphere( dynamicObject->second->getRBWorld());
}
}
}
}
//!RB DEBUG
#endif
2014-02-18 15:07:40 +01:00
2014-02-20 16:35:49 +01:00
// render current UI state
if(currGameUI->HaveGUIRender())
currGameUI->RenderGUI();
if(currGameUI->HaveTextRender())
currGameUI->RenderText();
Oyster::Graphics::API::EndFrame();
return true;
}
2014-02-12 16:31:15 +01:00
bool GameState::Release()
{
2014-02-19 15:57:52 +01:00
Graphics::API::Option o = Graphics::API::GetOption();
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;
}
2014-02-19 10:59:23 +01:00
auto light = this->privData->lights->begin();
for( ; light != this->privData->lights->end(); ++light )
{
light->second->Render();
}
2014-02-17 14:33:11 +01:00
this->privData->staticObjects->clear();
this->privData->dynamicObjects->clear();
2014-02-19 10:59:23 +01:00
this->privData->lights->clear();
2014-02-17 14:33:11 +01:00
privData = NULL;
}
2014-02-12 16:31:15 +01:00
2014-02-20 16:35:49 +01:00
if(respawnUI)
2014-01-27 13:56:31 +01:00
{
2014-02-20 16:35:49 +01:00
respawnUI->Release();
delete respawnUI;
respawnUI = NULL;
2014-01-27 13:56:31 +01:00
}
2014-02-20 16:35:49 +01:00
if(gameUI)
{
2014-02-20 16:35:49 +01:00
gameUI->Release();
delete gameUI;
gameUI = NULL;
2014-01-30 09:07:56 +01:00
}
2014-02-20 16:35:49 +01:00
currGameUI = NULL;
2014-02-18 15:54:09 +01:00
2014-02-20 16:35:49 +01:00
return true;
}
2014-02-18 15:54:09 +01:00
2014-02-20 16:35:49 +01:00
void GameState::ChangeState( ClientState next )
{
this->privData->nextState = next;
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 );
2014-02-18 15:54:09 +01:00
// RB DEBUG
(*this->privData->dynamicObjects)[decoded.object_ID]->setRBPos ( decoded.position );
// !RB DEBUG
}
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-18 15:54:09 +01:00
// RB DEBUG
(*this->privData->dynamicObjects)[decoded.object_ID]->setRBScale ( decoded.scale );
// !RB DEBUG
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-18 15:54:09 +01:00
// RB DEBUG
(*this->privData->dynamicObjects)[decoded.object_ID]->setRBRot ( rotation );
// !RB DEBUG
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-19 15:47:43 +01:00
if( !Within(position.Dot(position), 2500.0f, 90000.0f) )
{ // HACK: bug trap
const char *breakPoint = "Something is wrong.";
position = Float3( 0.0f, 160.0f, 0.0f );
2014-02-19 15:47:43 +01:00
}
2014-02-19 13:41:05 +01:00
this->privData->camera.SetPosition( position );
this->privData->camera.SetRotation( rotation );
2014-02-19 10:55:59 +01:00
this->privData->player.setPos( position );
2014-02-19 14:20:46 +01:00
this->privData->player.setRot( rotation );
this->privData->player.updateWorld();
// RB DEBUG
this->privData->player.setRBPos ( position );
this->privData->player.setRBRot ( rotation );
this->privData->player.updateRBWorld();
// !RB DEBUG
}
2014-02-20 15:47:11 +01:00
else
2014-02-18 15:09:01 +01:00
{
2014-02-20 15:47:11 +01:00
C_DynamicObj *object = (*this->privData->dynamicObjects)[decoded.object_ID];
if( object )
{
object->setPos( position );
object->setRot( rotation );
object->updateWorld();
// RB DEBUG
object->setRBPos ( position );
object->setRBRot ( rotation );
object->updateRBWorld();
// !RB DEBUG
}
2014-02-18 15:09:01 +01:00
}
}
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 );
2014-02-18 15:54:09 +01:00
modelData.rotation = Quaternion( Float3(decoded.position), 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-18 15:54:09 +01:00
// RB DEBUG
// Is just using the model position since the rigid body data should never be sent to the client
RBInitData RBData;
RBData.position = decoded.position;
RBData.rotation = ArrayToQuaternion( decoded.position );
RBData.scale = Float3( decoded.scale );
this->privData->player.InitRB( RBData );
// !RB DEBUG
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 */
2014-02-20 16:35:49 +01:00
case protocol_Gameplay_ObjectRespawn:
this->currGameUI = this->gameUI;
return GameClientState::event_processed;
case protocol_Gameplay_ObjectDie:
this->currGameUI = this->respawnUI;
// set countdown
2014-02-20 15:47:11 +01:00
case protocol_Gameplay_ObjectDisconnectPlayer:
{
//Removes
Protocol_ObjectDisconnectPlayer decoded(data);
auto object = this->privData->dynamicObjects->find( decoded.objectID );
if( object != this->privData->dynamicObjects->end() )
{
object->second = nullptr;
this->privData->dynamicObjects->erase( object );
}
}
2014-02-20 16:35:49 +01:00
return GameClientState::event_processed;
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
}