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

393 lines
9.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"
#include <Protocols.h>
2013-12-19 10:22:32 +01:00
#include "NetworkClient.h"
2014-01-27 13:56:31 +01:00
#include "Camera.h"
2014-01-30 14:17:50 +01:00
#include <GameServerAPI.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;
}privData;
GameState::GameState(void)
{
key_forward = false;
key_backward = false;
key_strafeRight = false;
key_strafeLeft = false;
}
GameState::~GameState(void)
{
}
bool GameState::Init(Oyster::Network::NetworkClient* nwClient)
{
// load models
2014-01-27 13:56:31 +01:00
camera = new Camera;
privData = new myData();
privData->state = gameStateState_loading;
privData->nwClient = nwClient;
privData->state = LoadGame();
2014-01-27 13:56:31 +01:00
return true;
}
2013-12-13 12:02:49 +01:00
GameState::gameStateState GameState::LoadGame()
{
Oyster::Graphics::Definitions::Pointlight plight;
2014-01-27 13:56:31 +01:00
plight.Pos = Oyster::Math::Float3(0,15,5);
plight.Color = Oyster::Math::Float3(0,1,0);
2014-01-27 13:56:31 +01:00
plight.Radius = 50;
2014-01-21 14:28:27 +01:00
plight.Bright = 2;
2014-01-27 13:56:31 +01:00
Oyster::Graphics::API::AddLight(plight);
plight.Pos = Oyster::Math::Float3(10,15,5);
plight.Color = Oyster::Math::Float3(1,0,0);
plight.Radius = 50;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
plight.Pos = Oyster::Math::Float3(10,-15,5);
plight.Color = Oyster::Math::Float3(0,0,1);
plight.Radius = 50;
plight.Bright = 2;
Oyster::Graphics::API::AddLight(plight);
LoadModels(L"map");
2014-01-27 13:56:31 +01:00
InitCamera(Oyster::Math::Float3(0,0,20.0f));
return gameStateState_playing;
}
bool GameState::LoadModels(std::wstring mapFile)
{
// open file
// read file
// init models
privData->modelCount = 2;
// add world model
2013-12-09 12:01:36 +01:00
ModelInitData modelData;
Oyster::Math3D::Float4x4 translate;
C_Object* obj;
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(0,0,0));
2014-01-30 15:12:53 +01:00
modelData.world = translate ;//modelData.world * translate
2014-01-30 11:11:45 +01:00
modelData.modelPath = L"world_earth.dan";
modelData.id = 0;
obj = new C_Player();
2013-12-19 11:58:42 +01:00
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
// add box model
modelData.world = Oyster::Math3D::Float4x4::identity;
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(4,320,0));
modelData.world = modelData.world * translate;
2014-01-30 00:19:00 +01:00
modelData.modelPath = L"..\\Content\\Models\\box.dan";
modelData.id = 1;
obj = new C_Player();
2013-12-19 11:58:42 +01:00
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
modelData.world = Oyster::Math3D::Float4x4::identity;
// add player model
modelData.world = Oyster::Math3D::Float4x4::identity;
translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(0, 320, 0));
2014-01-16 12:26:14 +01:00
modelData.world = modelData.world * translate;
modelData.visible = true;
2014-01-20 16:36:25 +01:00
modelData.modelPath = L"..\\Content\\Models\\char_white.dan";
modelData.id = 2;
// load models
obj = new C_Player();
2014-01-16 12:26:14 +01:00
privData->object.push_back(obj);
privData->object[privData->object.size() -1 ]->Init(modelData);
return true;
}
bool GameState::InitCamera(Oyster::Math::Float3 startPos)
{
Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,1);
2014-01-27 13:56:31 +01:00
Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0);
Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 20);
camera->LookAt(pos, dir, up);
camera->SetLens(3.14f/2, 1024/768, 1, 1000);
2014-01-30 15:12:53 +01:00
privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/4,1024.0f/768.0f,.1f,1000);
//privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
Oyster::Graphics::API::SetProjection(privData->proj);
2014-01-27 13:56:31 +01:00
camera->UpdateViewMatrix();
privData->view = camera->View();
privData->view = Oyster::Math3D::ViewMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
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
{
2014-01-27 13:56:31 +01:00
readKeyInput(KeyInput);
camera->UpdateViewMatrix();
2014-01-23 08:24:35 +01:00
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()
{
2014-01-27 13:56:31 +01:00
Oyster::Graphics::API::SetView(camera->View());
//Oyster::Graphics::API::SetProjection(camera->Proj());
//Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection(privData->proj);
Oyster::Graphics::API::NewFrame();
2014-01-27 13:56:31 +01:00
for (unsigned int i = 0; i < privData->object.size(); i++)
{
privData->object[i]->Render();
}
Oyster::Graphics::API::EndFrame();
return true;
}
bool GameState::Release()
{
2014-01-27 13:56:31 +01:00
for (unsigned 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
}
2014-01-27 13:56:31 +01:00
void GameState::readKeyInput(InputClass* KeyInput)
{
bool send = false;
GameLogic::Protocol_PlayerMovement movePlayer;
movePlayer.bForward = false;
movePlayer.bBackward = false;
movePlayer.bLeft = false;
movePlayer.bRight = false;
if(KeyInput->IsKeyPressed(DIK_W))
{
if(!key_forward)
{
movePlayer.bForward = true;
send = true;
key_forward = true;
}
}
else
key_forward = false;
if(KeyInput->IsKeyPressed(DIK_S))
{
if(!key_backward)
{
movePlayer.bBackward = true;
send = true;
key_backward = true;
}
}
else
key_backward = false;
if(KeyInput->IsKeyPressed(DIK_A))
{
if(!key_strafeLeft)
{
movePlayer.bLeft = true;
send = true;
key_strafeLeft = true;
}
}
else
key_strafeLeft = false;
if(KeyInput->IsKeyPressed(DIK_D))
{
if(!key_strafeRight)
{
movePlayer.bRight = true;
send = true;
key_strafeRight = true;
}
}
else
key_strafeRight = false;
if (privData->nwClient->IsConnected() && send)
{
privData->nwClient->Send(movePlayer);
}
//send delta mouse movement
if (KeyInput->IsMousePressed())
{
camera->Yaw(KeyInput->GetYaw());
camera->Pitch(KeyInput->GetPitch());
camera->UpdateViewMatrix();
GameLogic::Protocol_PlayerLook playerLookDir;
Oyster::Math::Float3 look = camera->GetLook();
playerLookDir.lookDirX = look.x;
playerLookDir.lookDirY = look.y;
playerLookDir.lookDirZ = look.z;
privData->nwClient->Send(playerLookDir);
}
2014-01-30 09:07:56 +01:00
// shoot
2014-01-27 13:56:31 +01:00
if(KeyInput->IsKeyPressed(DIK_Z))
{
if(!key_Shoot)
{
GameLogic::Protocol_PlayerShot playerShot;
playerShot.hasShot = true;
privData->nwClient->Send(playerShot);
key_Shoot = true;
}
}
else
key_Shoot = false;
2014-01-30 09:07:56 +01:00
// jump
if(KeyInput->IsKeyPressed(DIK_X))
{
if(!key_Jump)
{
GameLogic::Protocol_PlayerJump playerJump;
playerJump.hasJumped = true;
privData->nwClient->Send(playerJump);
key_Jump = true;
}
}
else
key_Jump = false;
2014-01-27 13:56:31 +01:00
// send event data
//
if(KeyInput->IsKeyPressed(DIK_L))
privData->state = GameState::gameStateState_end;
}
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];
}
2014-01-27 13:56:31 +01:00
for (unsigned int i = 0; i < privData->object.size(); i++)
2013-12-19 11:58:42 +01:00
{
if(privData->object[i]->GetId() == pos->object_ID)
2014-01-16 12:26:14 +01:00
{
2013-12-19 11:58:42 +01:00
privData->object[i]->setPos(world);
2014-01-27 13:56:31 +01:00
//camera->setRight((Oyster::Math::Float3(world[0], world[1], world[2])));
//camera->setUp((Oyster::Math::Float3(world[4], world[5], world[6])));
//camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10])));
if(i == 2) // playerobj
{
camera->SetPosition(Oyster::Math::Float3(world[12], world[13]+2.2f, world[14]-1));
camera->UpdateViewMatrix();
}
2014-01-16 12:26:14 +01:00
}
2013-12-19 11:58:42 +01:00
}
2013-12-16 11:08:10 +01:00
}
void GameState::Protocol( NewObj* newObj )
2013-12-18 12:18:01 +01:00
{
Oyster::Math::Float4x4 world;
for(int i = 0; i<16; i++)
{
world[i] = newObj->worldPos[i];
2013-12-18 12:18:01 +01:00
}
ModelInitData modelData;
modelData.world = world;
modelData.visible = true;
modelData.id = newObj->object_ID;
2013-12-18 15:28:47 +01:00
//not sure if this is good parsing rom char* to wstring
const char* path = newObj->path;
2013-12-18 12:18:01 +01:00
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
{
2014-01-27 13:56:31 +01:00
for (unsigned 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);