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

245 lines
6.8 KiB
C++
Raw Normal View History

2014-02-14 14:32:17 +01:00
#include "NetLoadState.h"
#include "NetworkClient.h"
2014-02-17 11:50:51 +01:00
#include "OysterMath.h"
2014-02-17 14:33:11 +01:00
#include "Protocols.h"
#include "LevelLoader.h"
2014-02-17 14:33:11 +01:00
#include "Utilities.h"
#include "C_obj\C_StaticObj.h"
#include "C_obj\C_DynamicObj.h"
2014-02-19 10:59:23 +01:00
#include "C_Light.h"
2014-02-14 14:32:17 +01:00
using namespace ::DanBias::Client;
using namespace ::Oyster;
2014-02-17 11:50:51 +01:00
using namespace ::Oyster::Math;
2014-02-14 14:32:17 +01:00
using namespace ::Oyster::Network;
2014-02-14 17:12:38 +01:00
using namespace ::GameLogic;
2014-02-17 14:33:11 +01:00
using namespace ::Utility::String;
2014-02-14 14:32:17 +01:00
struct NetLoadState::MyData
{
MyData() {}
GameClientState::ClientState nextState;
2014-02-14 17:12:38 +01:00
NetworkClient *nwClient;
2014-02-17 11:27:43 +01:00
Graphics::API::Texture background;
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
2014-02-14 17:12:38 +01:00
bool loading;
2014-02-14 14:32:17 +01:00
};
2014-02-17 16:16:27 +01:00
inline Quaternion ArrayToQuaternion( const float source[4] )
2014-02-17 14:33:11 +01:00
{
return Quaternion( Float3(source[0], source[1], source[2]), source[3] );
}
2014-02-14 14:32:17 +01:00
NetLoadState::NetLoadState(void) {}
NetLoadState::~NetLoadState(void)
{
if( this->privData )
this->Release();
}
2014-02-17 11:27:43 +01:00
bool NetLoadState::Init( SharedStateContent &shared )
2014-02-14 14:32:17 +01:00
{
this->privData = new MyData();
2014-02-17 14:33:11 +01:00
this->privData->nextState = GameClientState::ClientState_Same;
this->privData->nwClient = shared.network;
this->privData->background = Graphics::API::CreateTexture( L"grass_md.png" );
this->privData->dynamicObjects = &shared.dynamicObjects;
this->privData->staticObjects = &shared.staticObjects;
2014-02-19 10:59:23 +01:00
this->privData->lights = &shared.lights;
2014-02-17 14:33:11 +01:00
2014-02-14 17:12:38 +01:00
this->privData->loading = false;
2014-02-14 14:32:17 +01:00
// we may assume that nwClient is properly connected to the server
2014-02-14 17:12:38 +01:00
// signals querry to server for loading instructions
2014-02-18 12:59:51 +01:00
this->privData->nwClient->Send( Protocol_QuerryGameType() );
2014-02-14 14:32:17 +01:00
return true;
}
2014-02-17 11:27:43 +01:00
GameClientState::ClientState NetLoadState::Update( float deltaTime )
2014-02-14 14:32:17 +01:00
{
return this->privData->nextState;
}
bool NetLoadState::Render()
{
2014-02-17 11:27:43 +01:00
Graphics::API::NewFrame();
Graphics::API::StartGuiRender();
Graphics::API::RenderGuiElement( this->privData->background, Float3(0.5f, 0.5f, 1.0f), Float2(1.0f) );
Graphics::API::EndFrame();
2014-02-14 14:32:17 +01:00
return true;
}
bool NetLoadState::Release()
{
if( this->privData )
{
this->privData = NULL;
}
return true;
}
void NetLoadState::ChangeState( ClientState next )
{
this->privData->nextState = next;
}
const GameClientState::NetEvent & NetLoadState::DataRecieved( const GameClientState::NetEvent &message )
{
2014-02-14 17:12:38 +01:00
// fetching the id data.
short ID = message.args.data.protocol[0].value.netShort;
if( ID == protocol_Lobby_CreateGame )
2014-02-14 17:12:38 +01:00
{
if( !this->privData->loading )
{
this->LoadGame( Protocol_LobbyCreateGame(message.args.data.protocol).mapName );
this->ChangeState( ClientState_Game );
this->privData->loading = false;
}
return GameClientState::event_processed;
2014-02-14 17:12:38 +01:00
}
2014-02-18 15:09:01 +01:00
else
{ // HACK: Debug trap
const char *breakPoint = "Being greedy.";
return message;
2014-02-14 17:12:38 +01:00
}
}
void NetLoadState::LoadGame( const ::std::string &fileName )
{
this->privData->loading = true;
2014-02-18 15:09:01 +01:00
LevelLoader loader( "..\\Content\\Worlds\\" );
2014-02-17 14:33:11 +01:00
auto objects = loader.LoadLevel( fileName );
auto object = objects.begin();
2014-02-17 16:16:27 +01:00
ObjectTypeHeader *oth;
2014-02-17 14:33:11 +01:00
int objectID = 100; // first 100 is reserved for players. This is how the server does it.
for( ; object != objects.end(); ++object )
{
++objectID;
2014-02-17 16:16:27 +01:00
oth = (ObjectTypeHeader*)(*object._Ptr);
switch( oth->typeID )
2014-02-17 14:33:11 +01:00
{
case ObjectType::ObjectType_Static:
{
2014-02-17 16:16:27 +01:00
ObjectHeader *oh = (ObjectHeader*)oth;
2014-02-17 14:33:11 +01:00
ModelInitData desc;
desc.id = objectID;
StringToWstring( oh->ModelFile, desc.modelPath );
desc.position = oh->position;
desc.rotation = ArrayToQuaternion( oh->rotation );
desc.scale = oh->scale;
desc.visible = true;
2014-02-17 14:33:11 +01:00
C_StaticObj *staticObject = new C_StaticObj();
if( staticObject->Init( desc ) )
{
// RB DEBUG
RBInitData RBData;
if(oh->boundingVolume.geoType == CollisionGeometryType_Box)
{
2014-02-20 09:21:38 +01:00
RBData.position = ((Float3)oh->position + (Float3)oh->boundingVolume.box.position) * (Float3)oh->scale;
RBData.rotation = ArrayToQuaternion( oh->rotation ); // Only model rotation
2014-02-20 08:56:36 +01:00
RBData.scale = (Float3)oh->scale * (Float3)oh->boundingVolume.box.size * 2;
2014-02-18 15:07:40 +01:00
RBData.type = RB_Type_Cube;
staticObject->InitRB( RBData );
}
if(oh->boundingVolume.geoType == CollisionGeometryType_Sphere)
{
2014-02-20 09:21:38 +01:00
RBData.position = ((Float3)oh->position + (Float3)oh->boundingVolume.box.position) * (Float3)oh->scale;
RBData.rotation = ArrayToQuaternion( oh->rotation ); // Only model rotation
2014-02-20 08:56:36 +01:00
RBData.scale = (Float3)oh->scale * oh->boundingVolume.sphere.radius * 2;
2014-02-18 15:07:40 +01:00
RBData.type = RB_Type_Sphere;
staticObject->InitRB( RBData );
}
2014-02-18 15:07:40 +01:00
// !RB DEBUG
2014-02-17 14:33:11 +01:00
(*this->privData->staticObjects)[objectID] = staticObject;
}
else
{
delete staticObject;
}
}
break;
case ObjectType::ObjectType_Dynamic:
{
2014-02-17 16:16:27 +01:00
ObjectHeader *oh = (ObjectHeader*)oth;
2014-02-17 14:33:11 +01:00
ModelInitData desc;
desc.id = objectID;
StringToWstring( oh->ModelFile, desc.modelPath );
desc.position = oh->position;
desc.rotation = ArrayToQuaternion( oh->rotation );
desc.scale = oh->scale;
desc.visible = true;
C_DynamicObj *dynamicObject = new C_DynamicObj();
if( dynamicObject->Init( desc ) )
{
// RB DEBUG
RBInitData RBData;
if(oh->boundingVolume.geoType == CollisionGeometryType_Box)
{
2014-02-20 09:21:38 +01:00
RBData.position = ((Float3)oh->position + (Float3)oh->boundingVolume.box.position) * (Float3)oh->scale;
RBData.rotation = ArrayToQuaternion( oh->rotation ); // Only model rotation
2014-02-20 08:56:36 +01:00
RBData.scale = (Float3)oh->scale * (Float3)oh->boundingVolume.box.size * 2;
2014-02-18 15:07:40 +01:00
RBData.type = RB_Type_Cube;
dynamicObject->InitRB( RBData );
}
if(oh->boundingVolume.geoType == CollisionGeometryType_Sphere)
{
2014-02-20 09:21:38 +01:00
RBData.position = ((Float3)oh->position + (Float3)oh->boundingVolume.box.position) * (Float3)oh->scale;
RBData.rotation = ArrayToQuaternion( oh->rotation ); // Only model rotation
2014-02-20 08:56:36 +01:00
RBData.scale = (Float3)oh->scale * oh->boundingVolume.sphere.radius * 2;
2014-02-18 15:07:40 +01:00
RBData.type = RB_Type_Sphere;
dynamicObject->InitRB( RBData );
}
2014-02-18 15:07:40 +01:00
// !RB DEBUG
2014-02-17 14:33:11 +01:00
(*this->privData->dynamicObjects)[objectID] = dynamicObject;
}
else
{
delete dynamicObject;
}
}
break;
case ObjectType::ObjectType_Light:
{
2014-02-19 10:59:23 +01:00
BasicLight *light = (BasicLight*)oth;
Graphics::Definitions::Pointlight pointLight;
pointLight.Color = light->color;
pointLight.Pos = light->position;
pointLight.Bright = light->intensity;
pointLight.Radius = light->raduis;
C_Light *newLight = new C_Light( pointLight, objectID );
(*this->privData->lights)[objectID] = newLight;
2014-02-17 14:33:11 +01:00
}
break;
default: break;
}
}
2014-02-14 17:12:38 +01:00
this->privData->nextState = ClientState::ClientState_Game;
}