Merge branch 'GameClient' of https://github.com/dean11/Danbias into GameClient

This commit is contained in:
lanariel 2014-02-18 09:33:44 +01:00
commit 0f492e7bc6
15 changed files with 496 additions and 475 deletions

View File

@ -100,9 +100,6 @@ namespace DanBias
Graphics::API::Update( dt ); Graphics::API::Update( dt );
if(data.networkClient.IsConnected())
data.networkClient.Update();
data.capFrame += dt; data.capFrame += dt;
if(data.capFrame > 0.03) if(data.capFrame > 0.03)
{ {
@ -118,6 +115,9 @@ namespace DanBias
data.capFrame = 0; data.capFrame = 0;
} }
if(data.networkClient.IsConnected())
data.networkClient.Update();
} }
return DanBiasClientReturn_Success; return DanBiasClientReturn_Success;
} }

View File

@ -112,7 +112,7 @@
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>NetworkAPI_$(PlatformShortName)D.dll;OysterGraphics_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>NetworkAPI_$(PlatformShortName)D.dll;OysterGraphics_$(PlatformShortName)D.dll;GameClient_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -129,7 +129,7 @@
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>NetworkAPI_$(PlatformShortName)D.dll;OysterGraphics_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>NetworkAPI_$(PlatformShortName)D.dll;OysterGraphics_$(PlatformShortName)D.dll;GameClient_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -150,7 +150,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>NetworkAPI_$(PlatformShortName).dll;OysterGraphics_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>NetworkAPI_$(PlatformShortName).dll;OysterGraphics_$(PlatformShortName).dll;GameClient_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -171,7 +171,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>NetworkAPI_$(PlatformShortName).dll;OysterGraphics_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs> <DelayLoadDLLs>NetworkAPI_$(PlatformShortName).dll;OysterGraphics_$(PlatformShortName).dll;GameClient_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ShowAllFiles>false</ShowAllFiles> <ShowAllFiles>true</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory> <LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>

View File

@ -5,10 +5,18 @@
#include "Camera_FPS.h" #include "Camera_FPS.h"
#include <GameServerAPI.h> #include <GameServerAPI.h>
#include "C_obj/C_Player.h"
#include "C_obj/C_DynamicObj.h"
#include "C_obj/C_StaticObj.h"
#include "Utilities.h"
using namespace ::DanBias::Client; using namespace ::DanBias::Client;
using namespace ::Oyster; using namespace ::Oyster;
using namespace ::Oyster::Network; using namespace ::Oyster::Network;
using namespace ::Oyster::Math3D; using namespace ::Oyster::Math3D;
using namespace ::GameLogic;
using namespace ::Utility::DynamicMemory;
using namespace ::Utility::String;
struct GameState::MyData struct GameState::MyData
{ {
@ -17,17 +25,31 @@ struct GameState::MyData
NetworkClient *nwClient; NetworkClient *nwClient;
InputClass *input; InputClass *input;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_Object>> *staticObjects; ::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_StaticObj>> *staticObjects;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_Object>> *dynamicObjects; ::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;
} privData; } privData;
inline Quaternion ArrayToQuaternion( const float source[4] )
{
return Quaternion( Float3(source[0], source[1], source[2]), source[3] );
}
GameState::GameState() GameState::GameState()
{ {
key_forward = false; this->privData = nullptr;
key_backward = false;
key_strafeRight = false;
key_strafeLeft = false;
} }
GameState::~GameState() GameState::~GameState()
@ -38,202 +60,81 @@ GameState::~GameState()
bool GameState::Init( SharedStateContent &shared ) bool GameState::Init( SharedStateContent &shared )
{ {
// load models // we may assume that shared.network is properly connected
privData = new MyData(); // 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;
this->privData->nextState = GameClientState::ClientState_Same; this->privData->nextState = GameClientState::ClientState_Same;
this->privData->nwClient = shared.network; this->privData->nwClient = shared.network;
this->privData->input = shared.input; this->privData->input = shared.input;
this->privData->staticObjects = &shared.staticObjects;
LoadGame(); this->privData->dynamicObjects = &shared.dynamicObjects;
//tell server ready //tell server ready
this->privData->nwClient->Send( GameLogic::Protocol_General_Status(GameLogic::Protocol_General_Status::States_ready) ); this->privData->nwClient->Send( Protocol_General_Status(Protocol_General_Status::States_ready) );
return true; return true;
} }
GameState::gameStateState GameState::LoadGame() void GameState::InitiatePlayer( int id, const std::string &modelName, const float position[3], const float rotation[4], const float scale[3], bool isMyPlayer )
{ {
return gameStateState_playing;
}
bool GameState::LoadModels(std::string mapFile)
{
GameLogic::LevelLoader levelLoader;
std::vector<Utility::DynamicMemory::SmartPointer<GameLogic::ObjectTypeHeader>> objects;
objects = levelLoader.LoadLevel(mapFile);
int objCount = objects.size();
int modelId = 0;
ModelInitData modelData; ModelInitData modelData;
for (int i = 0; i < objCount; i++) modelData.visible = true;
modelData.position = position;
modelData.rotation = ArrayToQuaternion( rotation );
modelData.scale = scale;
StringToWstring( modelName, modelData.modelPath );
modelData.id = id;
if( isMyPlayer )
{ {
GameLogic::ObjectTypeHeader* obj = objects.at(i); if( this->privData->player.Init(modelData) )
switch (obj->typeID)
{ {
case GameLogic::ObjectType::ObjectType_LevelMetaData: this->privData->myId = id;
this->privData->camera.SetPosition( this->privData->player.getPos() );
break; this->privData->camera.UpdateOrientation();
case GameLogic::ObjectType::ObjectType_Static: }
{ }
GameLogic::ObjectHeader* staticObjData = ((GameLogic::ObjectHeader*)obj); else
{
modelData.modelPath.assign(staticObjData->ModelFile.begin(), staticObjData->ModelFile.end()); C_DynamicObj *p = new C_DynamicObj();
modelData.visible = true; if( p->Init(modelData) )
//modelData.position = ; {
//modelData.rotation = Quaternion(Float3(2,2,-2), 1); (*this->privData->dynamicObjects)[id] = p;
//modelData.scale = Float3(2,2,2);
modelData.id = modelId++;
this->staticObjects.Push(new C_StaticObj());
this->staticObjects[this->staticObjects.Size() -1 ]->Init(modelData);
}
break;
case GameLogic::ObjectType::ObjectType_Dynamic:
{
GameLogic::ObjectHeader* dynamicObjData = ((GameLogic::ObjectHeader*)obj);
//modelData.position = ;
//modelData.rotation = Quaternion(Float3(2,2,-2), 1);
//modelData.scale = Float3(2,2,2);
modelData.modelPath.assign(dynamicObjData->ModelFile.begin(), dynamicObjData->ModelFile.end());
modelData.visible = true;
modelData.id = modelId++;
this->dynamicObjects.Push(new C_DynamicObj());
this->dynamicObjects[this->dynamicObjects.Size() -1 ]->Init(modelData);
}
break;
case GameLogic::ObjectType::ObjectType_Light:
{
GameLogic::BasicLight* lightData = ((GameLogic::BasicLight*)obj);
switch( lightData->lightType )
{
case GameLogic::LightType_PointLight:
{
//Oyster::Graphics::Definitions::Pointlight plight;
//plight.Pos = ((GameLogic::PointLight*)lightData)->position;
//plight.Color = lightData->diffuseColor;
//plight.Radius = 100;
//plight.Bright = 0.9f;
//Oyster::Graphics::API::AddLight(plight);
}
break;
default: break;
}
}
break;
default:
break;
} }
} }
myId += modelId++;
// add player model
//modelData.position = ;
//modelData.rotation = Quaternion(Float3(2,2,-2), 1);
//modelData.scale = Float3(2,2,2);
modelData.visible = true;
modelData.modelPath = L"char_still_sizeref.dan";
modelData.id = myId;
// load models
this->dynamicObjects.Push(new C_DynamicObj());
this->dynamicObjects[this->dynamicObjects.Size() -1 ]->Init(modelData);
/*C_Player* obj = new C_Player();
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
*/
return true;
}
bool GameState::InitCamera(Float3 startPos)
{
camera.SetHeadOffset( Float3(0.0f, 1.0f, 1.0f) );
camera.SetPerspectiveProjection( pi / 4.0f, 1024.0f/768.0f, 1.0f, 1000.0f );
camera.UpdateOrientation();
Oyster::Graphics::API::SetProjection(camera.GetProjectionMatrix());
return true;
}
void GameState::InitiatePlayer(int id, std::wstring modelName, Float4x4 world)
{
myId = id;
ModelInitData modelData;
C_Object* obj;
modelData.visible = true;
//modelData.world = world;
modelData.position = Float3(world[12], world[13], world[14]);
modelData.rotation = Quaternion(Float3(0,0,0), 1);
modelData.scale = Float3(1,1,1);
modelData.modelPath = modelName;
modelData.id = myId;
obj = new C_Player();
this->dynamicObjects.Push(obj);
this->dynamicObjects[this->dynamicObjects.Size() -1 ]->Init(modelData);
Float3 pos = Float3(world[12], world[13], world[14]);
camera.SetPosition( pos );
camera.UpdateOrientation();
} }
GameClientState::ClientState GameState::Update( float deltaTime ) GameClientState::ClientState GameState::Update( float deltaTime )
{ {
//switch (privData->state)
//{
//case gameStateState_loading: //Will this ever happen in this scope??
// {
// // load map
// // wait for all players
// LoadGame();
// GameLogic::Protocol_General_Status gameStatus;
// gameStatus.status = GameLogic::Protocol_General_Status::States_ready;
// privData->nwClient->Send(gameStatus);
// privData->state = gameStateState_playing;
// }
// break;
//case gameStateState_playing:
// // read server data
// // update objects
// {
// readKeyInput(KeyInput);
// camera.UpdateOrientation();
// }
// break;
//case gameStateState_end:
// return ClientState_Lobby;
// break;
//default:
// break;
//}
//
//// send key input to server.
//return ClientState_Same;
return this->privData->nextState; return this->privData->nextState;
} }
bool GameState::Render() bool GameState::Render()
{ {
Oyster::Graphics::API::SetView( camera.GetViewMatrix() ); Oyster::Graphics::API::SetView( this->privData->camera.GetViewMatrix() );
Oyster::Graphics::API::NewFrame(); Oyster::Graphics::API::NewFrame();
for (unsigned int i = 0; i < staticObjects.Size(); i++)
// 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 )
{ {
staticObjects[i]->Render(); staticObject->second->Render();
} }
for (unsigned int i = 0; i < dynamicObjects.Size(); i++)
auto dynamicObject = this->privData->dynamicObjects->begin();
for( ; dynamicObject != this->privData->dynamicObjects->end(); ++dynamicObject )
{ {
dynamicObjects[i]->Render(); dynamicObject->second->Render();
} }
Oyster::Graphics::API::EndFrame(); Oyster::Graphics::API::EndFrame();
@ -242,8 +143,25 @@ bool GameState::Render()
bool GameState::Release() bool GameState::Release()
{ {
if( privData )
{
auto staticObject = this->privData->staticObjects->begin();
for( ; staticObject != this->privData->staticObjects->end(); ++staticObject )
{
staticObject->second = nullptr;
}
privData = NULL; 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; return true;
} }
@ -252,138 +170,119 @@ void GameState::ChangeState( ClientState next )
this->privData->nextState = next; this->privData->nextState = next;
} }
void GameState::readKeyInput(InputClass* KeyInput) void GameState::ReadKeyInput()
{ {
if(KeyInput->IsKeyPressed(DIK_W)) if( this->privData->input->IsKeyPressed(DIK_W) )
{ {
if(!key_forward) if(!this->privData->key_forward)
{ {
privData->nwClient->Send(GameLogic::Protocol_PlayerMovementForward()); this->privData->nwClient->Send( Protocol_PlayerMovementForward() );
key_forward = true; this->privData->key_forward = true;
} }
} }
else else
key_forward = false; this->privData->key_forward = false;
if(KeyInput->IsKeyPressed(DIK_S)) if( this->privData->input->IsKeyPressed(DIK_S) )
{ {
if(!key_backward) if( !this->privData->key_backward )
{ {
privData->nwClient->Send(GameLogic::Protocol_PlayerMovementBackward()); this->privData->nwClient->Send( Protocol_PlayerMovementBackward() );
key_backward = true; this->privData->key_backward = true;
} }
} }
else else
key_backward = false; this->privData->key_backward = false;
if(KeyInput->IsKeyPressed(DIK_A)) if( this->privData->input->IsKeyPressed(DIK_A) )
{ {
if(!key_strafeLeft) if( !this->privData->key_strafeLeft )
{ {
privData->nwClient->Send(GameLogic::Protocol_PlayerMovementLeft()); this->privData->nwClient->Send( Protocol_PlayerMovementLeft() );
key_strafeLeft = true; this->privData->key_strafeLeft = true;
} }
} }
else else
key_strafeLeft = false; this->privData->key_strafeLeft = false;
if(KeyInput->IsKeyPressed(DIK_D)) if( this->privData->input->IsKeyPressed(DIK_D) )
{ {
if(!key_strafeRight) if( !this->privData->key_strafeRight )
{ {
privData->nwClient->Send(GameLogic::Protocol_PlayerMovementRight()); this->privData->nwClient->Send( Protocol_PlayerMovementRight() );
key_strafeRight = true; this->privData->key_strafeRight = true;
} }
} }
else else
key_strafeRight = false; this->privData->key_strafeRight = false;
//send delta mouse movement //send delta mouse movement
//if (KeyInput->IsMousePressed())
{ {
camera.YawRight( -KeyInput->GetYaw() ); this->privData->camera.YawRight( -this->privData->input->GetYaw() );
camera.PitchUp( KeyInput->GetPitch() ); this->privData->camera.PitchUp( this->privData->input->GetPitch() );
camera.UpdateOrientation(); this->privData->camera.UpdateOrientation();
GameLogic::Protocol_PlayerLook playerLookDir; privData->nwClient->Send( Protocol_PlayerLook(this->privData->camera.GetLook(), this->privData->camera.GetRight()) );
Float4 look = camera.GetLook();
privData->nwClient->Send( playerLookDir );
} }
// shoot // shoot
if(KeyInput->IsKeyPressed(DIK_Z)) if( this->privData->input->IsKeyPressed(DIK_Z) )
{ {
if(!key_Shoot) if( !this->privData->key_Shoot )
{ {
GameLogic::Protocol_PlayerShot playerShot; Protocol_PlayerShot playerShot;
playerShot.primaryPressed = true; playerShot.primaryPressed = true;
playerShot.secondaryPressed = false; playerShot.secondaryPressed = false;
playerShot.utilityPressed = false; playerShot.utilityPressed = false;
privData->nwClient->Send(playerShot); this->privData->nwClient->Send( playerShot );
key_Shoot = true; this->privData->key_Shoot = true;
} }
} }
else else
key_Shoot = false; this->privData->key_Shoot = false;
if(KeyInput->IsKeyPressed(DIK_X)) if( this->privData->input->IsKeyPressed(DIK_X) )
{ {
if(!key_Shoot) if( !this->privData->key_Shoot )
{ {
GameLogic::Protocol_PlayerShot playerShot; Protocol_PlayerShot playerShot;
playerShot.primaryPressed = false; playerShot.primaryPressed = false;
playerShot.secondaryPressed = true; playerShot.secondaryPressed = true;
playerShot.utilityPressed = false; playerShot.utilityPressed = false;
privData->nwClient->Send(playerShot); this->privData->nwClient->Send( playerShot );
key_Shoot = true; this->privData->key_Shoot = true;
} }
} }
else else
key_Shoot = false; this->privData->key_Shoot = false;
if(KeyInput->IsKeyPressed(DIK_C)) if( this->privData->input->IsKeyPressed(DIK_C) )
{ {
if(!key_Shoot) if( !this->privData->key_Shoot )
{ {
GameLogic::Protocol_PlayerShot playerShot; Protocol_PlayerShot playerShot;
playerShot.primaryPressed = false; playerShot.primaryPressed = false;
playerShot.secondaryPressed = false; playerShot.secondaryPressed = false;
playerShot.utilityPressed = true; playerShot.utilityPressed = true;
privData->nwClient->Send(playerShot); this->privData->nwClient->Send( playerShot );
key_Shoot = true; this->privData->key_Shoot = true;
} }
} }
else else
key_Shoot = false; this->privData->key_Shoot = false;
// jump // jump
if(KeyInput->IsKeyPressed(DIK_SPACE)) if( this->privData->input->IsKeyPressed(DIK_SPACE) )
{ {
if(!key_Jump) if(!this->privData->key_Jump)
{ {
privData->nwClient->Send(GameLogic::Protocol_PlayerJump()); this->privData->nwClient->Send( Protocol_PlayerJump() );
key_Jump = true; this->privData->key_Jump = true;
} }
} }
else else
key_Jump = false; this->privData->key_Jump = false;
// send event data // TODO: implement sub-menu
//
//if(KeyInput->IsKeyPressed(DIK_L))
// privData->state = GameState::gameStateState_end;
}
using namespace ::Oyster::Network;
using namespace ::Utility::DynamicMemory;
// returns -1 if none found
int FindObject( const DynamicArray<SmartPointer<C_Object>> &collection, int id )
{
int num = collection.Size();
for( int i = 0; i < num; ++i ) if( id == collection[i]->GetId() )
return i;
return -1;
} }
void GameState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e ) void GameState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
@ -400,76 +299,67 @@ void GameState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEven
case protocol_Gameplay_ObjectHealthStatus: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectHealthStatus: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectPosition: case protocol_Gameplay_ObjectPosition:
{ {
GameLogic::Protocol_ObjectPosition decoded(data); Protocol_ObjectPosition decoded(data);
// if is this player. Remember to change camera // if is this player. Remember to change camera
if( this->myId == decoded.object_ID ) if( this->privData->myId == decoded.object_ID )
camera.SetPosition( decoded.position ); this->privData->camera.SetPosition( decoded.position );
int i = FindObject( this->dynamicObjects, decoded.object_ID ); (*this->privData->dynamicObjects)[decoded.object_ID]->setPos( decoded.position );
if( i > -1 )
this->dynamicObjects[i]->setPos( decoded.position );
} }
break; break;
case protocol_Gameplay_ObjectScale: case protocol_Gameplay_ObjectScale:
{ {
GameLogic::Protocol_ObjectScale decoded(data); Protocol_ObjectScale decoded(data);
int i = FindObject( this->dynamicObjects, decoded.object_ID ); (*this->privData->dynamicObjects)[decoded.object_ID]->setScale( decoded.scale );
if( i > -1 )
this->dynamicObjects[i]->setScale( decoded.scale );
} }
break; break;
case protocol_Gameplay_ObjectRotation: case protocol_Gameplay_ObjectRotation:
{ {
GameLogic::Protocol_ObjectRotation decoded(data); Protocol_ObjectRotation decoded(data);
Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] ); Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] );
// if is this player. Remember to change camera // if is this player. Remember to change camera
if( this->myId == decoded.object_ID ) if( this->privData->myId == decoded.object_ID )
camera.SetAngular( AngularAxis(rotation) ); this->privData->camera.SetAngular( AngularAxis(rotation) );
int i = FindObject( this->dynamicObjects, decoded.object_ID ); (*this->privData->dynamicObjects)[decoded.object_ID]->setRot( rotation );
if( i > -1 )
this->dynamicObjects[i]->setRot( rotation );
} }
break; break;
case protocol_Gameplay_ObjectPositionRotation: case protocol_Gameplay_ObjectPositionRotation:
{ {
GameLogic::Protocol_ObjectPositionRotation decoded(data); Protocol_ObjectPositionRotation decoded(data);
Float3 position = decoded.position; Float3 position = decoded.position;
Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] ); Quaternion rotation = Quaternion( Float3(decoded.rotationQ), decoded.rotationQ[3] );
// if is this player. Remember to change camera // if is this player. Remember to change camera
if( this->myId == decoded.object_ID ) if( this->privData->myId == decoded.object_ID )
{ {
camera.SetPosition( position ); this->privData->camera.SetPosition( position );
camera.SetAngular( AngularAxis(rotation) ); this->privData->camera.SetAngular( AngularAxis(rotation) );
} }
int i = FindObject( this->dynamicObjects, decoded.object_ID ); C_DynamicObj *object = (*this->privData->dynamicObjects)[decoded.object_ID];
if( i > -1 ) object->setPos( position );
{ object->setRot( rotation );
this->dynamicObjects[i]->setPos( position );
this->dynamicObjects[i]->setRot( rotation );
}
} }
break; break;
case protocol_Gameplay_ObjectEnabled: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectEnabled: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectDisabled: case protocol_Gameplay_ObjectDisabled:
{ {
GameLogic::Protocol_ObjectDisable decoded(data); Protocol_ObjectDisable decoded(data);
int i = FindObject( this->dynamicObjects, decoded.objectID ); auto object = this->privData->dynamicObjects->find( decoded.objectID );
if( i > -1 ) if( object != this->privData->dynamicObjects->end() )
{ {
this->dynamicObjects[i].Release(); object->second = nullptr;
this->dynamicObjects.Pop(i); this->privData->dynamicObjects->erase( object );
} }
} }
break; break;
case protocol_Gameplay_ObjectCreate: case protocol_Gameplay_ObjectCreate:
{ {
GameLogic::Protocol_ObjectCreate decoded(data); Protocol_ObjectCreate decoded(data);
C_DynamicObj* object = new C_DynamicObj(); C_DynamicObj* object = new C_DynamicObj();
ModelInitData modelData; ModelInitData modelData;
@ -484,11 +374,16 @@ void GameState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientEven
} }
object->Init(modelData); object->Init(modelData);
dynamicObjects.Push(object); (*this->privData->dynamicObjects)[decoded.object_ID] = object;
} }
break; break;
case protocol_Gameplay_ObjectCreatePlayer: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectCreatePlayer:
{
Protocol_ObjectCreatePlayer decoded(data);
this->InitiatePlayer( decoded.object_ID, decoded.meshName, decoded.position, decoded.rotationQ, decoded.scale, decoded.owner );
}
break;
case protocol_Gameplay_ObjectJoinTeam: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectJoinTeam: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectLeaveTeam: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectLeaveTeam: break; /** @todo TODO: implement */
case protocol_Gameplay_ObjectWeaponCooldown: break; /** @todo TODO: implement */ case protocol_Gameplay_ObjectWeaponCooldown: break; /** @todo TODO: implement */

View File

@ -4,13 +4,6 @@
#include "OysterMath.h" #include "OysterMath.h"
#include <string> #include <string>
#include "Camera_FPS.h"
#include "LevelLoader/LevelLoader.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_DynamicObj.h"
#include "C_obj/C_StaticObj.h"
#include "DynamicArray.h"
namespace DanBias { namespace Client namespace DanBias { namespace Client
{ {
class GameState : public GameClientState class GameState : public GameClientState
@ -27,13 +20,8 @@ namespace DanBias { namespace Client
~GameState(void); ~GameState(void);
bool Init( SharedStateContent &shared ); bool Init( SharedStateContent &shared );
GameClientState::ClientState Update( float deltaTime ) override; GameClientState::ClientState Update( float deltaTime ) override;
void InitiatePlayer( int id, const std::string &modelName, const float position[3], const float rotation[4], const float scale[3], bool isMyPlayer );
bool LoadModels(std::string mapFile); void ReadKeyInput();
bool InitCamera(Oyster::Math::Float3 startPos) ;
void InitiatePlayer(int id, std::wstring modelName, Oyster::Math::Float4x4 world);
gameStateState LoadGame();
void readKeyInput(InputClass* KeyInput);
bool Render()override; bool Render()override;
bool Release()override; bool Release()override;
void ChangeState( ClientState next ); void ChangeState( ClientState next );
@ -43,20 +31,6 @@ namespace DanBias { namespace Client
private: private:
struct MyData; struct MyData;
::Utility::DynamicMemory::UniquePointer<MyData> privData; ::Utility::DynamicMemory::UniquePointer<MyData> privData;
bool key_forward;
bool key_backward;
bool key_strafeRight;
bool key_strafeLeft;
bool key_Shoot;
bool key_Jump;
Camera_FPS camera;
int myId;
Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<C_StaticObj>> staticObjects;
Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<C_Object>> dynamicObjects;
//Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<C_Player>> playObjects;
}; };
} } } }
#endif #endif

View File

@ -123,8 +123,8 @@ void LanMenuState::ChangeState( ClientState next )
{ {
case GameClientState::ClientState_Lobby: case GameClientState::ClientState_Lobby:
// attempt to connect to lobby // attempt to connect to lobby
//if( !this->privData->nwClient->Connect(this->privData->connectPort, (*this->privData->connectIP)[0]) ) if( !this->privData->nwClient->Connect(this->privData->connectPort, (*this->privData->connectIP)[0]) )
// return; // TODO: temporary commented out return;
break; break;
default: break; default: break;
} }

View File

@ -1,13 +1,18 @@
#include "NetLoadState.h" #include "NetLoadState.h"
#include "NetworkClient.h" #include "NetworkClient.h"
#include "OysterMath.h" #include "OysterMath.h"
#include "../Game/GameProtocols/Protocols.h" #include "Protocols.h"
#include "LevelLoader\LevelLoader.h"
#include "Utilities.h"
#include "C_obj\C_StaticObj.h"
#include "C_obj\C_DynamicObj.h"
using namespace ::DanBias::Client; using namespace ::DanBias::Client;
using namespace ::Oyster; using namespace ::Oyster;
using namespace ::Oyster::Math; using namespace ::Oyster::Math;
using namespace ::Oyster::Network; using namespace ::Oyster::Network;
using namespace ::GameLogic; using namespace ::GameLogic;
using namespace ::Utility::String;
struct NetLoadState::MyData struct NetLoadState::MyData
{ {
@ -16,9 +21,17 @@ struct NetLoadState::MyData
GameClientState::ClientState nextState; GameClientState::ClientState nextState;
NetworkClient *nwClient; NetworkClient *nwClient;
Graphics::API::Texture background; Graphics::API::Texture background;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_StaticObj>> *staticObjects;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_DynamicObj>> *dynamicObjects;
bool loading; bool loading;
}; };
inline Quaternion ArrayToQuaternion( const float source[4] )
{
return Quaternion( Float3(source[0], source[1], source[2]), source[3] );
}
NetLoadState::NetLoadState(void) {} NetLoadState::NetLoadState(void) {}
NetLoadState::~NetLoadState(void) NetLoadState::~NetLoadState(void)
@ -31,9 +44,12 @@ bool NetLoadState::Init( SharedStateContent &shared )
{ {
this->privData = new MyData(); this->privData = new MyData();
this->privData->nextState = GameClientState::ClientState_Same; this->privData->nextState = GameClientState::ClientState_Same;
this->privData->nwClient = shared.network; this->privData->nwClient = shared.network;
this->privData->background = Graphics::API::CreateTexture( L"grass_md.png" ); this->privData->background = Graphics::API::CreateTexture( L"grass_md.png" );
this->privData->dynamicObjects = &shared.dynamicObjects;
this->privData->staticObjects = &shared.staticObjects;
this->privData->loading = false; this->privData->loading = false;
// we may assume that nwClient is properly connected to the server // we may assume that nwClient is properly connected to the server
@ -82,6 +98,8 @@ void NetLoadState::DataRecieved( NetEvent<NetworkClient*, NetworkClient::ClientE
if( ID == protocol_Lobby_CreateGame && !this->privData->loading ) if( ID == protocol_Lobby_CreateGame && !this->privData->loading )
{ {
this->LoadGame( Protocol_LobbyCreateGame(e.args.data.protocol).modelName ); this->LoadGame( Protocol_LobbyCreateGame(e.args.data.protocol).modelName );
this->ChangeState( ClientState_Game );
this->privData->loading = false;
} }
} }
@ -89,7 +107,73 @@ void NetLoadState::LoadGame( const ::std::string &fileName )
{ {
this->privData->loading = true; this->privData->loading = true;
// TODO: ask Sam about level loader LevelLoader loader;
auto objects = loader.LoadLevel( fileName );
auto object = objects.begin();
ObjectTypeHeader *oth;
int objectID = 100; // first 100 is reserved for players. This is how the server does it.
for( ; object != objects.end(); ++object )
{
++objectID;
oth = (ObjectTypeHeader*)(*object._Ptr);
switch( oth->typeID )
{
case ObjectType::ObjectType_Static:
{
ObjectHeader *oh = (ObjectHeader*)oth;
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:
{
ObjectHeader *oh = (ObjectHeader*)oth;
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;
}
}
this->privData->nextState = ClientState::ClientState_Game; this->privData->nextState = ClientState::ClientState_Game;
} }

View File

@ -11,6 +11,8 @@
#include <map> #include <map>
#include "Utilities.h" #include "Utilities.h"
#include "C_Object.h" #include "C_Object.h"
#include "C_obj\C_StaticObj.h"
#include "C_obj\C_DynamicObj.h"
#include "NetworkClient.h" #include "NetworkClient.h"
#include "L_inputClass.h" #include "L_inputClass.h"
@ -19,8 +21,8 @@ namespace DanBias { namespace Client
struct SharedStateContent struct SharedStateContent
{ {
public: public:
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_Object>> staticObjects; ::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_StaticObj>> staticObjects;
::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_Object>> dynamicObjects; ::std::map<int, ::Utility::DynamicMemory::UniquePointer<::DanBias::Client::C_DynamicObj>> dynamicObjects;
::Oyster::Network::NetworkClient *network; ::Oyster::Network::NetworkClient *network;
InputClass* input; InputClass* input;
}; };

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ShowAllFiles>true</ShowAllFiles> <ShowAllFiles>false</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory> <LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>

View File

@ -476,7 +476,21 @@ namespace GameLogic
} }
Protocol_ObjectCreate( Oyster::Network::CustomNetProtocol& p ) Protocol_ObjectCreate( Oyster::Network::CustomNetProtocol& p )
{ {
/** @todo TODO: not implemented */ this->object_ID = p[1].value.netInt;
this->name.assign(p[2].value.netCharPtr);
this->position[0] = p[3].value.netFloat;
this->position[1] = p[4].value.netFloat;
this->position[2] = p[5].value.netFloat;
this->rotationQ[0] = p[6].value.netFloat;
this->rotationQ[1] = p[7].value.netFloat;
this->rotationQ[2] = p[8].value.netFloat;
this->rotationQ[3] = p[9].value.netFloat;
this->scale[0] = p[10].value.netFloat;
this->scale[1] = p[11].value.netFloat;
this->scale[2] = p[12].value.netFloat;
} }
Protocol_ObjectCreate(float p[3], float r[4], float s[3], int id, char *path) Protocol_ObjectCreate(float p[3], float r[4], float s[3], int id, char *path)
{ {
@ -532,14 +546,14 @@ namespace GameLogic
//#define protocol_Gameplay_ObjectCreatePlayer 359 //#define protocol_Gameplay_ObjectCreatePlayer 359
struct Protocol_ObjectCreatePlayer :public Oyster::Network::CustomProtocolObject struct Protocol_ObjectCreatePlayer :public Oyster::Network::CustomProtocolObject
{ {
//ObjectType type; //ie player, box or whatever /*1*/ int object_ID;
int object_ID; /*2*/ int teamId;
int teamId; /*3*/ bool owner;
std::string name; /*4*/ std::string name;
std::string meshName; /*5*/ std::string meshName;
float position[3]; /*6 - 8*/ float position[3];
float rotation[3]; /*9 - 11*/ float rotationQ[4];
float scale[3]; /*12 - 14*/ float scale[3];
Protocol_ObjectCreatePlayer() Protocol_ObjectCreatePlayer()
{ {
@ -550,28 +564,47 @@ namespace GameLogic
this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[1].type = Oyster::Network::NetAttributeType_Int;
//TEAM_ID //TEAM_ID
this->protocol[2].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Int;
//OWNER
this->protocol[3].type = Oyster::Network::NetAttributeType_Bool;
//PLAYER-NAME //PLAYER-NAME
this->protocol[3].type = Oyster::Network::NetAttributeType_CharArray;
//MESH-NAME
this->protocol[4].type = Oyster::Network::NetAttributeType_CharArray; this->protocol[4].type = Oyster::Network::NetAttributeType_CharArray;
//MESH-NAME
this->protocol[5].type = Oyster::Network::NetAttributeType_CharArray;
//POSITION //POSITION
this->protocol[5].type = Oyster::Network::NetAttributeType_Float;
this->protocol[6].type = Oyster::Network::NetAttributeType_Float; this->protocol[6].type = Oyster::Network::NetAttributeType_Float;
this->protocol[7].type = Oyster::Network::NetAttributeType_Float; this->protocol[7].type = Oyster::Network::NetAttributeType_Float;
//ROTATION
this->protocol[8].type = Oyster::Network::NetAttributeType_Float; this->protocol[8].type = Oyster::Network::NetAttributeType_Float;
//ROTATION
this->protocol[9].type = Oyster::Network::NetAttributeType_Float; this->protocol[9].type = Oyster::Network::NetAttributeType_Float;
this->protocol[10].type = Oyster::Network::NetAttributeType_Float; this->protocol[10].type = Oyster::Network::NetAttributeType_Float;
//SCALE
this->protocol[11].type = Oyster::Network::NetAttributeType_Float; this->protocol[11].type = Oyster::Network::NetAttributeType_Float;
//SCALE
this->protocol[12].type = Oyster::Network::NetAttributeType_Float; this->protocol[12].type = Oyster::Network::NetAttributeType_Float;
this->protocol[13].type = Oyster::Network::NetAttributeType_Float; this->protocol[13].type = Oyster::Network::NetAttributeType_Float;
this->protocol[14].type = Oyster::Network::NetAttributeType_Float;
} }
Protocol_ObjectCreatePlayer(Oyster::Network::CustomNetProtocol& p) Protocol_ObjectCreatePlayer(Oyster::Network::CustomNetProtocol& p)
{ {
this->object_ID = p[1].value.netInt;
this->teamId = this->protocol[2].value.netInt;
this->owner = this->protocol[3].value.netBool;
this->name.assign(p[4].value.netCharPtr);
this->meshName.assign(p[5].value.netCharPtr);
this->position[0] = p[6].value.netFloat;
this->position[1] = p[7].value.netFloat;
this->position[2] = p[8].value.netFloat;
this->rotationQ[0] = p[9].value.netFloat;
this->rotationQ[1] = p[10].value.netFloat;
this->rotationQ[2] = p[11].value.netFloat;
this->rotationQ[3] = p[12].value.netFloat;
this->scale[0] = p[13].value.netFloat;
this->scale[1] = p[14].value.netFloat;
this->scale[2] = p[15].value.netFloat;
} }
Protocol_ObjectCreatePlayer(float position[3], float rotation[3], float scale[3], int ObjectID, int teamID, std::string name, std::string meshName) Protocol_ObjectCreatePlayer(float position[3], float rotation[4], float scale[3], int ObjectID, bool owner, int teamID, std::string name, std::string meshName)
{ {
this->protocol[0].value = protocol_Gameplay_ObjectCreatePlayer; this->protocol[0].value = protocol_Gameplay_ObjectCreatePlayer;
this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[0].type = Oyster::Network::NetAttributeType_Short;
@ -580,29 +613,33 @@ namespace GameLogic
this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[1].type = Oyster::Network::NetAttributeType_Int;
//TEAM_ID //TEAM_ID
this->protocol[2].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Int;
//OWNER
this->protocol[3].type = Oyster::Network::NetAttributeType_Bool;
//PLAYER-NAME //PLAYER-NAME
this->protocol[3].type = Oyster::Network::NetAttributeType_CharArray;
//MESH-NAME
this->protocol[4].type = Oyster::Network::NetAttributeType_CharArray; this->protocol[4].type = Oyster::Network::NetAttributeType_CharArray;
//MESH-NAME
this->protocol[5].type = Oyster::Network::NetAttributeType_CharArray;
//POSITION //POSITION
this->protocol[5].type = Oyster::Network::NetAttributeType_Float;
this->protocol[6].type = Oyster::Network::NetAttributeType_Float; this->protocol[6].type = Oyster::Network::NetAttributeType_Float;
this->protocol[7].type = Oyster::Network::NetAttributeType_Float; this->protocol[7].type = Oyster::Network::NetAttributeType_Float;
//ROTATION
this->protocol[8].type = Oyster::Network::NetAttributeType_Float; this->protocol[8].type = Oyster::Network::NetAttributeType_Float;
//ROTATION
this->protocol[9].type = Oyster::Network::NetAttributeType_Float; this->protocol[9].type = Oyster::Network::NetAttributeType_Float;
this->protocol[10].type = Oyster::Network::NetAttributeType_Float; this->protocol[10].type = Oyster::Network::NetAttributeType_Float;
//SCALE
this->protocol[11].type = Oyster::Network::NetAttributeType_Float; this->protocol[11].type = Oyster::Network::NetAttributeType_Float;
this->protocol[12].type = Oyster::Network::NetAttributeType_Float; this->protocol[12].type = Oyster::Network::NetAttributeType_Float;
//SCALE
this->protocol[13].type = Oyster::Network::NetAttributeType_Float; this->protocol[13].type = Oyster::Network::NetAttributeType_Float;
this->protocol[14].type = Oyster::Network::NetAttributeType_Float;
this->protocol[15].type = Oyster::Network::NetAttributeType_Float;
this->object_ID = ObjectID; this->object_ID = ObjectID;
this->teamId = teamID; this->teamId = teamID;
this->owner = owner;
this->name = name; this->name = name;
this->meshName = meshName; this->meshName = meshName;
memcpy(&this->position[0], &position[0], sizeof(float)*3); memcpy(&this->position[0], &position[0], sizeof(float)*3);
memcpy(&this->rotation[0], &rotation[0], sizeof(float)*3); memcpy(&this->rotationQ[0], &rotation[0], sizeof(float)*4);
memcpy(&this->scale[0], &scale[0], sizeof(float)*3); memcpy(&this->scale[0], &scale[0], sizeof(float)*3);
} }
Oyster::Network::CustomNetProtocol GetProtocol() override Oyster::Network::CustomNetProtocol GetProtocol() override
@ -610,21 +647,23 @@ namespace GameLogic
this->protocol[1].value = this->object_ID; this->protocol[1].value = this->object_ID;
this->protocol[2].value = this->teamId; this->protocol[2].value = this->teamId;
this->protocol.Set(3, this->name); this->protocol[3].value = this->owner;
this->protocol.Set(4, this->meshName); this->protocol.Set(4, this->name);
this->protocol.Set(5, this->meshName);
//POSITION //POSITION
this->protocol[5].value = this->position[0]; this->protocol[6].value = this->position[0];
this->protocol[6].value = this->position[1]; this->protocol[7].value = this->position[1];
this->protocol[7].value = this->position[2]; this->protocol[8].value = this->position[2];
//ROTATION //ROTATION
this->protocol[8].value = this->rotation[0]; this->protocol[9].value = this->rotationQ[0];
this->protocol[9].value = this->rotation[1]; this->protocol[10].value = this->rotationQ[1];
this->protocol[10].value = this->rotation[2]; this->protocol[11].value = this->rotationQ[2];
this->protocol[12].value = this->rotationQ[3];
//SCALE //SCALE
this->protocol[11].value = this->scale[0]; this->protocol[13].value = this->scale[0];
this->protocol[12].value = this->scale[1]; this->protocol[14].value = this->scale[1];
this->protocol[13].value = this->scale[2]; this->protocol[15].value = this->scale[2];
return protocol; return protocol;
} }

View File

@ -41,11 +41,12 @@ namespace DanBias
//void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: //void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create:
void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start:
//void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: //void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join:
void LobbyLogin(GameLogic::Protocol_LobbyJoinGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyLogin(GameLogic::Protocol_LobbyJoinGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login:
void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh:
void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData:
void LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: void LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData:
void LobbyReady(GameLogic::Protocol_LobbyClientReadyState& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_ClientReadyState: void LobbyReady(GameLogic::Protocol_LobbyClientReadyState& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_ClientReadyState:
void LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_QuerryGameType:
private: private:
void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override; void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override;

View File

@ -11,130 +11,132 @@ using namespace Oyster::Network;
using namespace Oyster; using namespace Oyster;
using namespace GameLogic; using namespace GameLogic;
namespace DanBias using namespace DanBias;
GameLobby::GameLobby()
{ }
GameLobby::~GameLobby()
{ {
GameLobby::GameLobby() this->clients.Clear();
{ } }
void GameLobby::Release()
GameLobby::~GameLobby() {
NetworkSession::CloseSession(true);
this->gameSession.CloseSession(true);
}
void GameLobby::Update()
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{ {
this->clients.Clear(); if(this->clients[i])
}
void GameLobby::Release()
{
NetworkSession::CloseSession(true);
this->gameSession.CloseSession(true);
}
void GameLobby::Update()
{
this->ProcessClients();
}
void GameLobby::SetGameDesc(const LobbyLevelData& desc)
{
this->description.gameMode = desc.gameMode;
this->description.gameTime = desc.gameTime;
this->description.mapNumber = desc.mapNumber;
this->description.maxClients = desc.maxClients;
}
void GameLobby::GetGameDesc(LobbyLevelData& desc)
{
desc.gameMode = this->description.gameMode;
desc.gameTime = this->description.gameTime;
desc.mapNumber = this->description.mapNumber;
desc.maxClients = this->description.maxClients;
}
bool GameLobby::StartGameSession( )
{
//Check if all clients is ready
if(this->GetClientCount() && this->GetClientCount() == this->readyList.Size())
{ {
GameSession::GameDescription desc; this->clients[i]->Update();
desc.maxClients = this->description.maxClients; }
desc.gameMode = this->description.gameMode; }
desc.gameTime = this->description.gameTime; }
desc.mapNumber = this->description.mapNumber; void GameLobby::SetGameDesc(const LobbyLevelData& desc)
desc.owner = this; {
desc.clients = this->clients; this->description.gameMode = desc.gameMode;
this->description.gameTime = desc.gameTime;
this->description.mapNumber = desc.mapNumber;
this->description.maxClients = desc.maxClients;
}
void GameLobby::GetGameDesc(LobbyLevelData& desc)
{
desc.gameMode = this->description.gameMode;
desc.gameTime = this->description.gameTime;
desc.mapNumber = this->description.mapNumber;
desc.maxClients = this->description.maxClients;
}
bool GameLobby::StartGameSession( )
{
//Check if all clients is ready
if(this->GetClientCount() && this->GetClientCount() == this->readyList.Size())
{
GameSession::GameDescription desc;
desc.maxClients = this->description.maxClients;
desc.gameMode = this->description.gameMode;
desc.gameTime = this->description.gameTime;
desc.mapNumber = this->description.mapNumber;
desc.owner = this;
desc.clients = this->clients;
if(desc.gameTime == 0.0f) if(desc.gameTime == 0.0f)
desc.gameTime = (int)(60.0f * 10.0f); //note: should be fetched from somewhere. desc.gameTime = (int)(60.0f * 10.0f); //note: should be fetched from somewhere.
if(desc.maxClients == 0) if(desc.maxClients == 0)
desc.maxClients = 10; //note: should be fetched somewhere else.. desc.maxClients = 10; //note: should be fetched somewhere else..
this->clients.Clear(); //Remove clients from lobby list this->clients.Clear(); //Remove clients from lobby list
if(this->gameSession.Create(desc)) if(this->gameSession.Create(desc))
{
this->gameSession.Run();
return true;
}
}
else
{
//?
}
return false;
}
void GameLobby::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
{
switch (e.args.type)
{
case NetworkClient::ClientEventArgs::EventType_Disconnect:
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
e.sender->Disconnect();
this->readyList.Remove(e.sender);
this->clients.Remove(e.sender);
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
this->ParseProtocol(e.args.data.protocol, e.sender);
break;
}
}
void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client)
{
printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str());
if(this->gameSession)
{
Attach(client);
}
else
{
Attach(client);
Protocol_LobbyClientData p1;
Protocol_LobbyGameData p2;
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[i])
{ {
this->gameSession.Run(); Protocol_LobbyClientData::PlayerData t;
t.id = this->clients[i]->GetID();
return true; t.ip = this->clients[i]->GetIpAddress();
t.team = 0;
t.name = "Dennis är kung tycker Erik!";
p1.list.Push(t);
} }
} }
else p2.majorVersion = 1;
{ p2.minorVersion = 0;
//? p2.mapName = "Dennis är kung tycker Erik!";
}
return false; client->Send(p1.GetProtocol());
client->Send(p2.GetProtocol());
} }
}
void GameLobby::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
{
switch (e.args.type)
{
case NetworkClient::ClientEventArgs::EventType_Disconnect:
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
e.sender->Disconnect();
this->readyList.Remove(e.sender);
this->clients.Remove(e.sender);
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
this->ParseProtocol(e.args.data.protocol, e.sender);
break;
}
}
void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client)
{
printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str());
if(this->gameSession)
{
this->gameSession.Attach(client);
}
else
{
Attach(client);
Protocol_LobbyClientData p1;
Protocol_LobbyGameData p2;
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[i])
{
Protocol_LobbyClientData::PlayerData t;
t.id = this->clients[i]->GetID();
t.ip = this->clients[i]->GetIpAddress();
t.team = 0;
t.name = "Dennis är kung tycker Erik!";
p1.list.Push(t);
}
}
p2.majorVersion = 1;
p2.minorVersion = 0;
p2.mapName = "Dennis är kung tycker Erik!";
client->Send(p1.GetProtocol());
client->Send(p2.GetProtocol());
}
}
}//End namespace DanBias

View File

@ -18,11 +18,11 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie
break; break;
//case protocol_Lobby_Create: this->LobbyCreateGame (Protocol_LobbyCreateGame (p), c); //case protocol_Lobby_Create: this->LobbyCreateGame (Protocol_LobbyCreateGame (p), c);
//break; //break;
case protocol_Lobby_StartGame: this->LobbyStartGame (Protocol_LobbyStartGame (p), c); case protocol_Lobby_StartGame: this->LobbyStartGame (Protocol_LobbyStartGame (p), c);
break; break;
//case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); //case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c);
//break; //break;
case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyJoinGame (p), c); case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyJoinGame (p), c);
break; break;
case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c);
break; break;
@ -32,6 +32,8 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie
break; break;
case protocol_Lobby_ClientReadyState: this->LobbyReady (Protocol_LobbyClientReadyState (p), c); case protocol_Lobby_ClientReadyState: this->LobbyReady (Protocol_LobbyClientReadyState (p), c);
break; break;
case protocol_Lobby_QuerryGameType: this->LobbyReady (Protocol_LobbyClientReadyState (p), c);
break;
} }
} }
@ -112,4 +114,29 @@ void GameLobby::LobbyReady(GameLogic::Protocol_LobbyClientReadyState& p, Oyster:
this->readyList.Remove(c); this->readyList.Remove(c);
} }
} }
void GameLobby::LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyster::Network::NetworkClient* c)
{
NetClient temp;
bool found = false;
//find client in waiting list
for (unsigned int i = 0; !found && i < this->clients.Size(); i++)
{
if(this->clients[i]->GetID() == c->GetID())
{
temp = this->clients[i];
found = true;
}
}
//Something is wrong
if(!found)
{
c->Disconnect();
}
else
{
//Send game data
this->gameSession.Attach(temp);
}
}

View File

@ -61,8 +61,6 @@ void GameServerAPI::ServerStart()
{ {
timer.reset(); timer.reset();
server.Start(); server.Start();
} }
void GameServerAPI::ServerStop() void GameServerAPI::ServerStop()
{ {
@ -113,8 +111,6 @@ void GameServerAPI::NotifyWhenClientConnect(ClientConnectedNotify func)
{ {
if(!func) clientConnectedCallback = DefaultClientConnectedNotify; if(!func) clientConnectedCallback = DefaultClientConnectedNotify;
else clientConnectedCallback = func; else clientConnectedCallback = func;
} }
void GameServerAPI::NotifyWhenClientDisconnect(ClientDisconnectedNotify func) void GameServerAPI::NotifyWhenClientDisconnect(ClientDisconnectedNotify func)
{ {

View File

@ -209,6 +209,7 @@ namespace DanBias
{ {
IPlayerData* p = this->clients[i]->GetPlayer(); IPlayerData* p = this->clients[i]->GetPlayer();
Protocol_ObjectCreate oc(p->GetPosition(), p->GetRotation(), p->GetScale(), p->GetID(), "char_white.dan"); Protocol_ObjectCreate oc(p->GetPosition(), p->GetRotation(), p->GetScale(), p->GetID(), "char_white.dan");
//Protocol_ObjectCreatePlayer oc(p->GetPosition(), p->GetRotation(), p->GetScale(), p->GetID(), "char_white.dan");
this->clients[i]->GetClient()->Send(oc); this->clients[i]->GetClient()->Send(oc);
} }
} }