Danbias/Code/Game/GameClient/GameClientState/LanMenuState.cpp

226 lines
6.3 KiB
C++
Raw Permalink Normal View History

2014-01-30 23:23:37 +01:00
#include "LanMenuState.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_StaticObj.h"
#include "C_obj/C_DynamicObj.h"
#include "DllInterfaces/GFXAPI.h"
2014-01-31 14:14:20 +01:00
#include "LobbyState.h"
#include "GameState.h"
2014-02-12 10:43:06 +01:00
#include "../Network/NetworkAPI/NetworkClient.h"
2014-01-31 14:14:20 +01:00
#include <Protocols.h>
2014-02-13 10:14:44 +01:00
#include "EventHandler\EventHandler.h"
#include "Buttons\ButtonRectangle.h"
2014-02-14 09:40:53 +01:00
#include "Buttons\TextField.h"
2014-02-13 10:14:44 +01:00
2014-01-30 23:23:37 +01:00
#include <GameServerAPI.h>
2014-02-13 10:14:44 +01:00
#include <string>
2014-01-30 23:23:37 +01:00
2014-02-12 10:43:06 +01:00
using namespace ::DanBias::Client;
2014-02-12 16:31:15 +01:00
using namespace ::Oyster;
2014-02-12 10:43:06 +01:00
using namespace ::Oyster::Network;
2014-02-13 10:14:44 +01:00
using namespace ::Oyster::Event;
using namespace ::Oyster::Math3D;
using namespace ::GameLogic;
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
struct LanMenuState::MyData
2014-01-30 23:23:37 +01:00
{
2014-02-12 16:31:15 +01:00
MyData(){}
2014-01-31 14:14:20 +01:00
2014-02-12 16:31:15 +01:00
GameClientState::ClientState nextState;
NetworkClient *nwClient;
2014-02-20 16:15:47 +01:00
::Input::Mouse *mouseInput;
2014-02-21 12:09:38 +01:00
::Input::Keyboard *keyboardInput;
2014-02-24 19:45:13 +01:00
Float3 mousePos;
Graphics::API::Texture background, mouseCursor;
2014-02-14 09:40:53 +01:00
EventButtonCollection guiElements;
TextField<LanMenuState*> *connectIP;
2014-02-13 10:14:44 +01:00
unsigned short connectPort;
std::string ip;
2014-02-12 09:49:08 +01:00
} privData;
2014-01-30 23:23:37 +01:00
2014-02-14 09:40:53 +01:00
void OnButtonInteract_Connect( Oyster::Event::ButtonEvent<LanMenuState*>& e );
2014-02-14 14:31:01 +01:00
void OnButtonInteract_Exit( Oyster::Event::ButtonEvent<LanMenuState*>& e );
2014-02-13 10:14:44 +01:00
2014-02-12 09:49:08 +01:00
LanMenuState::LanMenuState() {}
2014-01-30 23:23:37 +01:00
2014-02-13 10:14:44 +01:00
LanMenuState::~LanMenuState()
{
if( this->privData )
this->Release();
}
2014-01-30 23:23:37 +01:00
2014-02-17 11:27:43 +01:00
bool LanMenuState::Init( SharedStateContent &shared )
2014-01-30 23:23:37 +01:00
{
2014-02-13 10:14:44 +01:00
this->privData = new MyData();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
this->privData->nextState = GameClientState::ClientState_Same;
2014-02-17 11:27:43 +01:00
this->privData->nwClient = shared.network;
2014-02-20 16:15:47 +01:00
this->privData->mouseInput = shared.mouseDevice;
2014-02-21 12:09:38 +01:00
this->privData->keyboardInput = shared.keyboardDevice;
2014-01-30 23:23:37 +01:00
this->privData->background = Graphics::API::CreateTexture( L"color_white.png" );
2014-02-25 15:52:15 +01:00
this->privData->mouseCursor = Graphics::API::CreateTexture( L"cursor.png" );
2014-02-13 10:14:44 +01:00
2014-02-14 09:40:53 +01:00
// create guiElements
this->privData->connectIP = new TextField<LanMenuState*>( L"noedge-btn-ipfield.png", Float4(1.0f), Float4(1.0f), this, Float3(0.5f, 0.2f, 0.9f), Float2(0.5f, 0.05f), ResizeAspectRatio_Height );
2014-02-14 09:40:53 +01:00
this->privData->connectIP->ReserveLines( 1 );
2014-02-19 10:55:59 +01:00
this->privData->connectIP->AppendText( L"127.0.0.1" );
//this->privData->connectIP->AppendText( L"194.47.150.206" ); // HACK: connecting to Dennis's server
2014-02-21 12:59:04 +01:00
//this->privData->connectIP->AppendText( L"194.47.150.189" ); // HACK: connecting to Robins server
this->privData->connectIP->SetFontHeight( 0.035f );
2014-02-14 14:31:01 +01:00
this->privData->connectIP->SetLineSpacing( 0.005f );
this->privData->connectIP->SetBottomAligned();
2014-02-13 10:14:44 +01:00
2014-02-14 09:40:53 +01:00
this->privData->guiElements.AddButton( this->privData->connectIP );
2014-02-13 10:14:44 +01:00
2014-02-14 14:31:01 +01:00
ButtonRectangle<LanMenuState*> *guiElements;
guiElements = new ButtonRectangle<LanMenuState*>( L"noedge-btn-join.png", L"", Float4(1.0f),Float4(1.0f),Float4(1.2f),Float4(1.5f), OnButtonInteract_Connect, this, Float3(0.5f, 0.4f, 0.5f), Float2(0.5f, 0.18f), ResizeAspectRatio_None );
2014-02-14 14:31:01 +01:00
this->privData->guiElements.AddButton( guiElements );
guiElements = new ButtonRectangle<LanMenuState*>( L"noedge-btn-back.png", L"", Float4(1.0f),Float4(1.0f),Float4(1.2f),Float4(1.5f), OnButtonInteract_Exit, this, Float3(0.5f, 0.8f, 0.5f), Float2(0.5f, 0.18f), ResizeAspectRatio_None );
2014-02-14 14:31:01 +01:00
this->privData->guiElements.AddButton( guiElements );
2014-02-14 09:40:53 +01:00
// bind guiElements collection to the singleton eventhandler
EventHandler::Instance().AddCollection( &this->privData->guiElements );
2014-02-13 10:14:44 +01:00
this->privData->connectPort = 15151;
2014-02-20 16:15:47 +01:00
this->privData->keyboardInput->BindTextTarget( &(*this->privData->connectIP)[0] );
this->privData->keyboardInput->Activate();
if(!this->privData->nwClient->StartListeningForBroadcasting(this->privData->connectPort))
{
return false;
}
2014-01-30 23:23:37 +01:00
return true;
}
2014-02-17 11:27:43 +01:00
GameClientState::ClientState LanMenuState::Update( float deltaTime )
2014-01-31 14:14:20 +01:00
{
2014-02-13 10:14:44 +01:00
MouseInput mouseState;
2014-01-31 14:14:20 +01:00
{
2014-02-24 19:45:13 +01:00
::Input::Struct::SAIPointFloat2D pos;
this->privData->mouseInput->GetNormalizedPosition( pos );
2014-01-31 14:14:20 +01:00
2014-02-24 19:45:13 +01:00
this->privData->mousePos.x = mouseState.x = pos.x;
this->privData->mousePos.y = mouseState.y = pos.y;
2014-02-20 16:15:47 +01:00
mouseState.mouseButtonPressed = this->privData->mouseInput->IsBtnDown( ::Input::Enum::SAMI_MouseLeftBtn );
}
2014-02-13 10:14:44 +01:00
EventHandler::Instance().Update( mouseState );
2014-01-31 14:14:20 +01:00
this->privData->nwClient->Update();
2014-02-12 16:31:15 +01:00
return this->privData->nextState;
2014-01-31 14:14:20 +01:00
}
2014-02-12 14:33:56 +01:00
bool LanMenuState::Render( )
2014-01-30 23:23:37 +01:00
{
2014-02-12 16:31:15 +01:00
Graphics::API::NewFrame();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
Graphics::API::StartGuiRender();
2014-01-30 23:23:37 +01:00
2014-02-25 15:52:15 +01:00
Graphics::API::RenderGuiElement( this->privData->mouseCursor, this->privData->mousePos, Float2(0.15f, 0.24), Float4(1.0f) );
2014-02-14 12:09:59 +01:00
Graphics::API::RenderGuiElement( this->privData->background, Float3(0.5f, 0.5f, 1.0f), Float2(1.0f) );
2014-02-14 09:40:53 +01:00
this->privData->guiElements.RenderTexture();
Graphics::API::StartTextRender();
this->privData->guiElements.RenderText();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
Graphics::API::EndFrame();
2014-01-30 23:23:37 +01:00
return true;
}
bool LanMenuState::Release()
{
if(privData)
{
this->privData->nwClient->StopListeningForBroadcasting();
}
2014-01-30 23:23:37 +01:00
privData = NULL;
return true;
2014-02-12 16:31:15 +01:00
}
void LanMenuState::ChangeState( ClientState next )
{
2014-02-13 10:14:44 +01:00
switch( next )
{
2014-02-25 15:52:15 +01:00
case GameClientState::ClientState_NetLoad:
2014-02-13 10:14:44 +01:00
// attempt to connect to lobby
2014-02-17 14:33:11 +01:00
if( !this->privData->nwClient->Connect(this->privData->connectPort, (*this->privData->connectIP)[0]) )
return;
2014-02-13 10:14:44 +01:00
break;
default: break;
}
2014-02-25 15:52:15 +01:00
this->privData->keyboardInput->ReleaseTextTarget();
2014-02-12 16:31:15 +01:00
this->privData->nextState = next;
2014-02-13 10:14:44 +01:00
}
2014-02-14 09:40:53 +01:00
void OnButtonInteract_Connect( Oyster::Event::ButtonEvent<LanMenuState*>& e )
2014-02-13 10:14:44 +01:00
{
switch( e.state )
{
case ButtonState_Released:
2014-02-25 15:52:15 +01:00
e.owner->ChangeState( GameClientState::ClientState_NetLoad );
2014-02-13 10:14:44 +01:00
break;
default: break;
}
2014-02-14 14:31:01 +01:00
}
void OnButtonInteract_Exit( Oyster::Event::ButtonEvent<LanMenuState*>& e )
{
switch( e.state )
{
case ButtonState_Released:
e.owner->ChangeState( GameClientState::ClientState_Main );
break;
default: break;
}
}
const GameClientState::NetEvent& LanMenuState::DataRecieved( const NetEvent &message )
{
if( message.args.type == NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend )
{ // TODO: Reconnect
const char *breakpoint = "temp trap";
this->privData->nwClient->Disconnect();
this->ChangeState( GameClientState::ClientState_Main );
}
// fetching the id data.
short ID = message.args.data.protocol[0].value.netShort;
CustomNetProtocol data = message.args.data.protocol;
switch(ID)
{
case protocol_Broadcast_Test:
{
Protocol_Broadcast_Test decoded(data);
2014-02-23 20:40:29 +01:00
unsigned short port = decoded.port;
std::string ip = decoded.ip;
std::string name = decoded.name;
printf("Broadcast message: %d: %s: %s\n", port, ip.c_str(), name.c_str());
2014-02-24 16:18:47 +01:00
//this->privData->connectPort = port;
//this->privData->ip = ip;
}
break;
default:
break;
}
return message;
}