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

180 lines
4.3 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\LevelLoader.h"
#include "Utilities.h"
#include "C_obj\C_StaticObj.h"
#include "C_obj\C_DynamicObj.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-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-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-17 11:27:43 +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;
}
void NetLoadState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
{
2014-02-14 17:12:38 +01:00
// fetching the id data.
short ID = e.args.data.protocol[0].value.netShort;
2014-02-14 17:12:38 +01:00
if( ID == protocol_Lobby_CreateGame && !this->privData->loading )
{
this->LoadGame( Protocol_LobbyCreateGame(e.args.data.protocol).modelName );
2014-02-17 14:33:11 +01:00
this->ChangeState( ClientState_Game );
2014-02-17 16:16:27 +01:00
this->privData->loading = false;
2014-02-14 17:12:38 +01:00
}
}
void NetLoadState::LoadGame( const ::std::string &fileName )
{
this->privData->loading = true;
2014-02-17 14:33:11 +01:00
LevelLoader loader;
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;
C_StaticObj *staticObject = new C_StaticObj();
if( staticObject->Init( desc ) )
{
(*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 ) )
{
(*this->privData->dynamicObjects)[objectID] = dynamicObject;
}
else
{
delete dynamicObject;
}
}
break;
case ObjectType::ObjectType_Light:
{
/* TODO: implement light into the leveformat */
}
break;
default: break;
}
}
2014-02-14 17:12:38 +01:00
this->privData->nextState = ClientState::ClientState_Game;
}