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

267 lines
6.3 KiB
C++
Raw Normal View History

2013-12-18 12:18:01 +01:00
#include "GameState.h"
#include "DllInterfaces/GFXAPI.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_DynamicObj.h"
2013-12-19 13:46:01 +01:00
#include <GameProtocols.h>
2013-12-19 10:22:32 +01:00
#include "NetworkClient.h"
using namespace DanBias::Client;
struct GameState::myData
{
myData(){}
Oyster::Math3D::Float4x4 view;
Oyster::Math3D::Float4x4 proj;
2013-12-19 11:58:42 +01:00
std::vector<C_Object*> object;
int modelCount;
2013-12-13 12:02:49 +01:00
Oyster::Network::NetworkClient* nwClient;
gameStateState state;
2013-12-13 12:02:49 +01:00
}privData;
GameState::GameState(void)
{
}
GameState::~GameState(void)
{
}
bool GameState::Init(Oyster::Network::NetworkClient* nwClient)
{
// load models
privData = new myData();
privData->state = gameStateState_loading;
privData->nwClient = nwClient;
privData->state = LoadGame();
return true;
}
2013-12-13 12:02:49 +01:00
GameState::gameStateState GameState::LoadGame()
{
Oyster::Graphics::Definitions::Pointlight plight;
plight.Pos = Oyster::Math::Float3(0,3,0);
plight.Color = Oyster::Math::Float3(0,1,0);
plight.Radius = 5;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
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;
2013-12-19 15:35:49 +01:00
modelData.modelPath = L"..\\Content\\worldDummy";
modelData.id = 0;
// load models
2013-12-19 11:58:42 +01:00
C_Object* obj = new C_Player();
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,2,2));
modelData.world = modelData.world * translate;
2013-12-19 15:35:49 +01:00
modelData.modelPath = L"..\\Content\\worldDummy";
modelData.id ++;
2013-12-19 11:58:42 +01:00
obj = new C_DynamicObj();
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
return true;
}
bool GameState::InitCamera(Oyster::Math::Float3 startPos)
{
privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000);
//privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
Oyster::Graphics::API::SetProjection(privData->proj);
privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view);
return true;
}
GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput)
{
switch (privData->state)
{
case gameStateState_loading:
2013-12-18 15:28:47 +01:00
{
// load map
// wait for all players
LoadGame();
2013-12-20 09:42:02 +01:00
GameLogic::Protocol_General_Status gameStatus;
gameStatus.status = GameLogic::Protocol_General_Status::States_ready;
2013-12-18 15:28:47 +01:00
privData->nwClient->Send(gameStatus);
privData->state = gameStateState_playing;
}
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;
GameLogic::Protocol_PlayerMovement movePlayer;
movePlayer.bForward = false;
movePlayer.bBackward = false;
movePlayer.bStrafeLeft = false;
movePlayer.bStrafeRight = false;
movePlayer.bTurnLeft = false;
movePlayer.bTurnRight = false;
if(KeyInput->IsKeyPressed(DIK_W))
{
movePlayer.bForward = true;
2013-12-17 13:39:10 +01:00
send = true;
}
if(KeyInput->IsKeyPressed(DIK_S))
{
movePlayer.bBackward = true;
2013-12-17 13:39:10 +01:00
send = true;
}
if(KeyInput->IsKeyPressed(DIK_A))
{
movePlayer.bStrafeLeft = true;
2013-12-17 13:39:10 +01:00
send = true;
}
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-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
//
if(KeyInput->IsKeyPressed(DIK_L))
privData->state = GameState::gameStateState_end;
2013-12-13 12:02:49 +01:00
}
break;
case gameStateState_end:
return ClientState_Lobby;
break;
default:
break;
}
// send key input to server.
return ClientState_Same;
}
bool GameState::Render()
{
Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection(privData->proj);
Oyster::Graphics::API::NewFrame();
2013-12-20 11:27:04 +01:00
for (int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Render();
}
Oyster::Graphics::API::EndFrame();
return true;
}
bool GameState::Release()
{
2013-12-19 11:58:42 +01:00
for (int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Release();
delete privData->object[i];
privData->object[i] = NULL;
}
delete privData;
privData = NULL;
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-19 11:58:42 +01:00
for (int i = 0; i < privData->object.size(); i++)
{
if(privData->object[i]->GetId() == pos->object_ID)
privData->object[i]->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-19 11:58:42 +01:00
modelData.id = pos->object_ID;
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
2013-12-19 11:58:42 +01:00
C_Object* player = new C_Player();
player->Init(modelData);
2013-12-18 12:18:01 +01:00
2013-12-19 11:58:42 +01:00
privData->object.push_back(player);
2013-12-17 13:39:10 +01:00
2013-12-18 15:28:47 +01:00
}
2013-12-19 11:58:42 +01:00
void DanBias::Client::GameState::Protocol( RemoveObj* obj )
2013-12-13 12:02:49 +01:00
{
2013-12-19 11:58:42 +01:00
for (int i = 0; i < privData->object.size(); i++)
2013-12-16 11:08:10 +01:00
{
2013-12-19 11:58:42 +01:00
if(privData->object[i]->GetId() == obj->object_ID)
{
privData->object.at(i)->Release();
privData->object.erase(privData->object.begin() + i );
}
2013-12-16 11:08:10 +01:00
}
2013-12-19 11:58:42 +01:00
//privData->object[obj->object_ID]->Release( );
2013-12-13 12:02:49 +01:00
}
2013-12-19 11:58:42 +01:00
2013-12-13 12:02:49 +01:00
//void GameState::Protocol(LightPos pos);