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

162 lines
3.4 KiB
C++
Raw Normal View History

2014-02-12 09:49:08 +01:00
#include "MainState.h"
2014-01-30 11:58:44 +01:00
#include "DllInterfaces/GFXAPI.h"
#include "OysterMath.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_StaticObj.h"
#include "C_obj/C_DynamicObj.h"
#include <GameServerAPI.h>
2014-02-12 10:43:06 +01:00
#include "NetworkClient.h"
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
using namespace ::DanBias::Client;
using namespace ::Oyster::Math3D;
using namespace ::Oyster::Network;
using namespace ::Utility::DynamicMemory;
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
struct MainState::MyData
2014-01-30 11:58:44 +01:00
{
2014-02-12 10:43:06 +01:00
MyData() {}
Float4x4 view;
Float4x4 proj;
UniquePointer<C_Object> object[2];
2014-01-30 11:58:44 +01:00
int modelCount;
2014-02-12 10:43:06 +01:00
NetworkClient *nwClient;
};
2014-01-30 11:58:44 +01:00
2014-02-12 09:49:08 +01:00
MainState::MainState(void) {}
2014-01-30 11:58:44 +01:00
2014-02-12 09:49:08 +01:00
MainState::~MainState(void) {}
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
bool MainState::Init( NetworkClient* nwClient )
2014-01-30 11:58:44 +01:00
{
2014-02-12 10:43:06 +01:00
this->privData = new MyData();
2014-01-30 11:58:44 +01:00
// load models
LoadModels(L"UImodels.txt");
2014-02-12 10:43:06 +01:00
InitCamera( Float3(0.0f, 0.0f, 5.4f) );
2014-01-30 11:58:44 +01:00
return true;
}
2014-02-12 09:49:08 +01:00
bool MainState::LoadModels(std::wstring file)
2014-01-30 11:58:44 +01:00
{
Oyster::Graphics::Definitions::Pointlight plight;
2014-02-12 10:43:06 +01:00
plight.Pos = Float3(0,0,5);
plight.Color = Float3(1,1,1);
plight.Radius = 100;
2014-01-30 11:58:44 +01:00
plight.Bright = 1;
Oyster::Graphics::API::AddLight(plight);
// open file
// read file
// init models
privData->modelCount = 2;
ModelInitData modelData;
2014-02-12 10:43:06 +01:00
modelData.rotation = Quaternion::identity;
modelData.scale = Float3(1,1,1);
2014-01-30 11:58:44 +01:00
modelData.visible = true;
modelData.modelPath = L"box.dan";
2014-02-05 15:54:48 +01:00
2014-02-12 10:43:06 +01:00
modelData.position = Float3(2,2,2);
privData->object[0] = new C_StaticObj();
2014-01-30 11:58:44 +01:00
privData->object[0]->Init(modelData);
2014-02-12 10:43:06 +01:00
modelData.position = Float3(-2,0,-2);
privData->object[1] = new C_StaticObj();
2014-01-30 11:58:44 +01:00
privData->object[1]->Init(modelData);
return true;
}
2014-02-12 10:43:06 +01:00
bool MainState::InitCamera(Float3 startPos)
2014-01-30 11:58:44 +01:00
{
2014-02-12 10:43:06 +01:00
privData->proj = ProjectionMatrix_Perspective(pi/2,1024.0f/768.0f,.1f,1000);
//privData->proj = ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
2014-01-30 11:58:44 +01:00
Oyster::Graphics::API::SetProjection(privData->proj);
2014-02-12 10:43:06 +01:00
privData->view = OrientationMatrix_LookAtDirection(Float3(0,0,-1),Float3(0,1,0),startPos);
privData->view = InverseOrientationMatrix(privData->view);
2014-01-30 11:58:44 +01:00
return true;
}
2014-02-12 09:49:08 +01:00
GameClientState::ClientState MainState::Update(float deltaTime, InputClass* KeyInput)
2014-01-30 11:58:44 +01:00
{
// picking
// mouse events
// different menus
// play sounds
// update animation
// send data to server
// check data from server
// create game
if( KeyInput->IsKeyPressed(DIK_C))
{
DanBias::GameServerAPI::ServerInitDesc desc;
DanBias::GameServerAPI::ServerInitiate(desc);
DanBias::GameServerAPI::ServerStart();
// my ip
2014-02-12 10:43:06 +01:00
this->privData->nwClient->Connect(15152, "127.0.0.1");
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
if (!this->privData->nwClient->IsConnected())
2014-01-30 11:58:44 +01:00
{
// failed to connect
return ClientState_Same;
}
return ClientState_LobbyCreated;
2014-01-30 11:58:44 +01:00
}
// join game
if( KeyInput->IsKeyPressed(DIK_J))
{
// game ip
2014-02-12 10:43:06 +01:00
this->privData->nwClient->Connect(15152, "127.0.0.1");
2014-02-10 22:25:31 +01:00
//nwClient->Connect(15152, "83.254.217.248");
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
if (!this->privData->nwClient->IsConnected())
2014-01-30 11:58:44 +01:00
{
// failed to connect
return ClientState_Same;
}
return ClientState_Lobby;
}
return ClientState_Same;
}
2014-02-12 09:49:08 +01:00
bool MainState::Render()
2014-01-30 11:58:44 +01:00
{
Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection( privData->proj);
Oyster::Graphics::API::NewFrame();
// render objects
for (int i = 0; i < privData->modelCount; i++)
{
privData->object[i]->Render();
}
// render effects
// render lights
Oyster::Graphics::API::EndFrame();
return true;
}
2014-02-12 09:49:08 +01:00
bool MainState::Release()
2014-01-30 11:58:44 +01:00
{
for (int i = 0; i < privData->modelCount; i++)
{
privData->object[i]->Release();
delete privData->object[i];
privData->object[i] = NULL;
}
delete privData;
privData = NULL;
return true;
}