2013-12-18 12:18:01 +01:00
|
|
|
|
#include "GameState.h"
|
2013-12-06 10:38:43 +01:00
|
|
|
|
#include "DllInterfaces/GFXAPI.h"
|
2013-12-16 09:45:12 +01:00
|
|
|
|
#include "C_obj/C_Player.h"
|
|
|
|
|
#include "C_obj/C_DynamicObj.h"
|
2013-12-13 12:02:49 +01:00
|
|
|
|
#include "PlayerProtocols.h"
|
2013-12-18 15:28:47 +01:00
|
|
|
|
#include "ControlProtocols.h"
|
2013-12-19 10:22:32 +01:00
|
|
|
|
#include "GameProtocols.h"
|
|
|
|
|
#include "NetworkClient.h"
|
|
|
|
|
|
2013-12-10 11:26:18 +01:00
|
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
|
using namespace DanBias::Client;
|
2013-12-09 09:23:39 +01:00
|
|
|
|
|
|
|
|
|
struct GameState::myData
|
|
|
|
|
{
|
|
|
|
|
myData(){}
|
|
|
|
|
Oyster::Math3D::Float4x4 view;
|
|
|
|
|
Oyster::Math3D::Float4x4 proj;
|
2013-12-10 11:26:18 +01:00
|
|
|
|
C_Object* object[3];
|
|
|
|
|
int modelCount;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
Oyster::Network::NetworkClient* nwClient;
|
2013-12-10 11:26:18 +01:00
|
|
|
|
gameStateState state;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
|
2013-12-09 09:23:39 +01:00
|
|
|
|
}privData;
|
|
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
|
GameState::GameState(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GameState::~GameState(void)
|
|
|
|
|
{
|
2013-12-09 11:05:47 +01:00
|
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
|
}
|
2013-12-16 09:45:12 +01:00
|
|
|
|
bool GameState::Init(Oyster::Network::NetworkClient* nwClient)
|
2013-12-06 10:38:43 +01:00
|
|
|
|
{
|
2013-12-10 11:26:18 +01:00
|
|
|
|
// load models
|
|
|
|
|
privData = new myData();
|
|
|
|
|
privData->state = gameStateState_loading;
|
2013-12-16 09:45:12 +01:00
|
|
|
|
privData->nwClient = nwClient;
|
2013-12-10 11:26:18 +01:00
|
|
|
|
privData->state = LoadGame();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-12-13 12:02:49 +01:00
|
|
|
|
GameState::gameStateState GameState::LoadGame()
|
2013-12-10 11:26:18 +01:00
|
|
|
|
{
|
|
|
|
|
LoadModels(L"map");
|
|
|
|
|
InitCamera(Oyster::Math::Float3(0,0,5.4f));
|
|
|
|
|
return gameStateState_playing;
|
|
|
|
|
}
|
|
|
|
|
bool GameState::LoadModels(std::wstring mapFile)
|
|
|
|
|
{
|
|
|
|
|
// open file
|
|
|
|
|
// read file
|
|
|
|
|
// init models
|
|
|
|
|
privData->modelCount = 2;
|
|
|
|
|
|
2013-12-09 12:01:36 +01:00
|
|
|
|
ModelInitData modelData;
|
|
|
|
|
|
|
|
|
|
modelData.world = Oyster::Math3D::Float4x4::identity;
|
|
|
|
|
modelData.visible = true;
|
|
|
|
|
modelData.modelPath = L"worldDummy";
|
2013-12-09 09:23:39 +01:00
|
|
|
|
// load models
|
2013-12-10 11:26:18 +01:00
|
|
|
|
privData->object[0] = new C_Player();
|
|
|
|
|
privData->object[0]->Init(modelData);
|
2013-12-09 09:23:39 +01:00
|
|
|
|
|
2013-12-10 11:26:18 +01:00
|
|
|
|
Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(2,2,2));
|
|
|
|
|
modelData.world = modelData.world * translate;
|
|
|
|
|
modelData.modelPath = L"crate";
|
|
|
|
|
|
|
|
|
|
privData->object[1] = new C_DynamicObj();
|
|
|
|
|
privData->object[1]->Init(modelData);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool GameState::InitCamera(Oyster::Math::Float3 startPos)
|
|
|
|
|
{
|
2013-12-09 09:23:39 +01:00
|
|
|
|
privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000);
|
2013-12-10 11:26:18 +01:00
|
|
|
|
//privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
|
2013-12-09 09:23:39 +01:00
|
|
|
|
Oyster::Graphics::API::SetProjection(privData->proj);
|
|
|
|
|
|
2013-12-10 11:26:18 +01:00
|
|
|
|
privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
|
2013-12-09 09:23:39 +01:00
|
|
|
|
privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view);
|
2013-12-06 10:38:43 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-12-10 11:26:18 +01:00
|
|
|
|
|
2013-12-09 09:23:39 +01:00
|
|
|
|
GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput)
|
2013-12-06 10:38:43 +01:00
|
|
|
|
{
|
2013-12-10 11:26:18 +01:00
|
|
|
|
switch (privData->state)
|
|
|
|
|
{
|
|
|
|
|
case gameStateState_loading:
|
2013-12-18 15:28:47 +01:00
|
|
|
|
{
|
|
|
|
|
// load map
|
|
|
|
|
// wait for all players
|
|
|
|
|
LoadGame();
|
|
|
|
|
GameLogic::Protocol_Status gameStatus;
|
|
|
|
|
gameStatus.status = GameLogic::Protocol_Status::States_ready;
|
|
|
|
|
privData->nwClient->Send(gameStatus);
|
|
|
|
|
privData->state = gameStateState_playing;
|
|
|
|
|
}
|
2013-12-10 11:26:18 +01:00
|
|
|
|
break;
|
|
|
|
|
case gameStateState_playing:
|
|
|
|
|
// read server data
|
|
|
|
|
// update objects
|
2013-12-13 12:02:49 +01:00
|
|
|
|
{
|
2013-12-17 13:39:10 +01:00
|
|
|
|
bool send = false;
|
2013-12-16 09:45:12 +01:00
|
|
|
|
GameLogic::Protocol_PlayerMovement movePlayer;
|
|
|
|
|
movePlayer.bForward = false;
|
|
|
|
|
movePlayer.bBackward = false;
|
|
|
|
|
movePlayer.bStrafeLeft = false;
|
|
|
|
|
movePlayer.bStrafeRight = false;
|
|
|
|
|
movePlayer.bTurnLeft = false;
|
|
|
|
|
movePlayer.bTurnRight = false;
|
2013-12-11 12:14:00 +01:00
|
|
|
|
|
2013-12-16 09:45:12 +01:00
|
|
|
|
if(KeyInput->IsKeyPressed(DIK_W))
|
|
|
|
|
{
|
|
|
|
|
movePlayer.bForward = true;
|
2013-12-17 13:39:10 +01:00
|
|
|
|
send = true;
|
2013-12-16 09:45:12 +01:00
|
|
|
|
}
|
|
|
|
|
if(KeyInput->IsKeyPressed(DIK_S))
|
|
|
|
|
{
|
|
|
|
|
movePlayer.bBackward = true;
|
2013-12-17 13:39:10 +01:00
|
|
|
|
send = true;
|
2013-12-16 09:45:12 +01:00
|
|
|
|
}
|
|
|
|
|
if(KeyInput->IsKeyPressed(DIK_A))
|
|
|
|
|
{
|
|
|
|
|
movePlayer.bStrafeLeft = true;
|
2013-12-17 13:39:10 +01:00
|
|
|
|
send = true;
|
2013-12-16 09:45:12 +01:00
|
|
|
|
}
|
|
|
|
|
if(KeyInput->IsKeyPressed(DIK_D))
|
|
|
|
|
{
|
|
|
|
|
movePlayer.bStrafeRight = true;
|
2013-12-17 13:39:10 +01:00
|
|
|
|
send = true;
|
2013-12-17 10:07:46 +01:00
|
|
|
|
}
|
2013-12-16 09:45:12 +01:00
|
|
|
|
|
2013-12-17 13:39:10 +01:00
|
|
|
|
if (privData->nwClient->IsConnected() && send)
|
2013-12-17 10:07:46 +01:00
|
|
|
|
{
|
|
|
|
|
privData->nwClient->Send(movePlayer);
|
|
|
|
|
}
|
2013-12-18 12:18:01 +01:00
|
|
|
|
|
|
|
|
|
//send delta mouse movement
|
|
|
|
|
if (KeyInput->IsMousePressed())
|
|
|
|
|
{
|
|
|
|
|
GameLogic::Protocol_PlayerMouse deltaMouseMove;
|
|
|
|
|
deltaMouseMove.dxMouse = KeyInput->GetYaw();
|
|
|
|
|
deltaMouseMove.dyMouse = KeyInput->GetPitch();
|
|
|
|
|
//privData->nwClient->Send(deltaMouseMove);
|
|
|
|
|
}
|
2013-12-17 10:07:46 +01:00
|
|
|
|
|
|
|
|
|
// send event data
|
|
|
|
|
//
|
2013-12-16 09:45:12 +01:00
|
|
|
|
if(KeyInput->IsKeyPressed(DIK_L))
|
|
|
|
|
privData->state = GameState::gameStateState_end;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
}
|
2013-12-10 11:26:18 +01:00
|
|
|
|
break;
|
|
|
|
|
case gameStateState_end:
|
2013-12-09 09:23:39 +01:00
|
|
|
|
return ClientState_Lobby;
|
2013-12-10 11:26:18 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 09:23:39 +01:00
|
|
|
|
// send key input to server.
|
2013-12-06 10:38:43 +01:00
|
|
|
|
return ClientState_Same;
|
|
|
|
|
}
|
|
|
|
|
bool GameState::Render()
|
|
|
|
|
{
|
2013-12-09 09:23:39 +01:00
|
|
|
|
Oyster::Graphics::API::SetView(privData->view);
|
2013-12-10 11:26:18 +01:00
|
|
|
|
Oyster::Graphics::API::SetProjection(privData->proj);
|
2013-12-09 09:23:39 +01:00
|
|
|
|
Oyster::Graphics::API::NewFrame();
|
2013-12-10 11:26:18 +01:00
|
|
|
|
for (int i = 0; i < privData->modelCount; i++)
|
|
|
|
|
{
|
|
|
|
|
privData->object[i]->Render();
|
|
|
|
|
}
|
2013-12-06 10:38:43 +01:00
|
|
|
|
Oyster::Graphics::API::EndFrame();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
bool GameState::Release()
|
|
|
|
|
{
|
2013-12-10 11:26:18 +01:00
|
|
|
|
for (int i = 0; i < privData->modelCount; i++)
|
|
|
|
|
{
|
|
|
|
|
privData->object[i]->Release();
|
|
|
|
|
delete privData->object[i];
|
|
|
|
|
privData->object[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 11:05:47 +01:00
|
|
|
|
delete privData;
|
|
|
|
|
privData = NULL;
|
2013-12-06 10:38:43 +01:00
|
|
|
|
return true;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GameState::Protocol(ProtocolStruct* pos)
|
|
|
|
|
{
|
2013-12-18 15:28:47 +01:00
|
|
|
|
|
2013-12-13 12:02:49 +01:00
|
|
|
|
}
|
2013-12-16 11:08:10 +01:00
|
|
|
|
|
2013-12-17 13:39:10 +01:00
|
|
|
|
void GameState::Protocol( PlayerPos* pos )
|
2013-12-16 11:08:10 +01:00
|
|
|
|
{
|
|
|
|
|
Oyster::Math::Float4x4 world, translate;
|
|
|
|
|
|
|
|
|
|
world = Oyster::Math::Float4x4::identity;
|
|
|
|
|
translate = Oyster::Math::Float4x4::identity;
|
2013-12-17 10:07:46 +01:00
|
|
|
|
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(pos->playerPos[0],pos->playerPos[1],pos->playerPos[2]));
|
|
|
|
|
world = world * translate;
|
2013-12-16 11:08:10 +01:00
|
|
|
|
privData->object[0]->setPos( world );
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 13:39:10 +01:00
|
|
|
|
void GameState::Protocol( ObjPos* pos )
|
2013-12-16 11:08:10 +01:00
|
|
|
|
{
|
|
|
|
|
Oyster::Math::Float4x4 world;
|
|
|
|
|
for(int i = 0; i<16; i++)
|
|
|
|
|
{
|
|
|
|
|
world[i] = pos->worldPos[i];
|
|
|
|
|
}
|
2013-12-17 10:07:46 +01:00
|
|
|
|
privData->object[pos->object_ID]->setPos(world);
|
2013-12-16 11:08:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 12:18:01 +01:00
|
|
|
|
void GameState::Protocol( NewObj* pos )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Oyster::Math::Float4x4 world;
|
|
|
|
|
for(int i = 0; i<16; i++)
|
|
|
|
|
{
|
|
|
|
|
world[i] = pos->worldPos[i];
|
|
|
|
|
}
|
|
|
|
|
ModelInitData modelData;
|
|
|
|
|
|
|
|
|
|
modelData.world = world;
|
|
|
|
|
modelData.visible = true;
|
2013-12-18 15:28:47 +01:00
|
|
|
|
//not sure if this is good parsing rom char* to wstring
|
2013-12-18 12:18:01 +01:00
|
|
|
|
const char* path = pos->path;
|
|
|
|
|
modelData.modelPath = std::wstring(path, path + strlen(path));
|
|
|
|
|
// load models
|
|
|
|
|
privData->object[pos->object_ID] = new C_Player();
|
|
|
|
|
privData->object[pos->object_ID]->Init(modelData);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 13:39:10 +01:00
|
|
|
|
void GameState::Protocol( KeyInput* pos )
|
|
|
|
|
{
|
|
|
|
|
bool key = false;
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
key = pos->key[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 15:28:47 +01:00
|
|
|
|
void DanBias::Client::GameState::Protocol( RemoveObj* obj )
|
|
|
|
|
{
|
|
|
|
|
privData->object[obj->object_ID]->Release( );
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 12:02:49 +01:00
|
|
|
|
void GameState::PlayerPosProtocol(PlayerPos* pos)
|
|
|
|
|
{
|
2013-12-16 11:08:10 +01:00
|
|
|
|
Oyster::Math::Float4x4 world, translate;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
|
2013-12-16 11:08:10 +01:00
|
|
|
|
world = Oyster::Math::Float4x4::identity;
|
|
|
|
|
translate = Oyster::Math::Float4x4::identity;
|
|
|
|
|
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(pos->playerPos[0],pos->playerPos[1],pos->playerPos[2] ));
|
|
|
|
|
world = translate;
|
|
|
|
|
privData->object[0]->setPos( world );
|
2013-12-13 12:02:49 +01:00
|
|
|
|
}
|
|
|
|
|
void GameState::ObjectPosProtocol(ObjPos* pos)
|
|
|
|
|
{
|
2013-12-16 11:08:10 +01:00
|
|
|
|
Oyster::Math::Float4x4 world;
|
|
|
|
|
for(int i = 0; i<16; i++)
|
|
|
|
|
{
|
|
|
|
|
world[i] = pos->worldPos[i];
|
|
|
|
|
}
|
|
|
|
|
privData->object[1]->setPos(world);
|
2013-12-13 12:02:49 +01:00
|
|
|
|
}
|
|
|
|
|
//void GameState::Protocol(LightPos pos);
|