2014-01-13 12:44:33 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by [Dennis Andersen] [2013]
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
2014-01-28 09:00:02 +01:00
|
|
|
#include "..\GameSession.h"
|
|
|
|
#include "..\GameClient.h"
|
2014-02-09 16:42:26 +01:00
|
|
|
#include <WinTimer.h>
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-01-14 09:25:22 +01:00
|
|
|
#include <Protocols.h>
|
2014-01-13 12:44:33 +01:00
|
|
|
#include <PostBox\PostBox.h>
|
|
|
|
#include <GameLogicStates.h>
|
|
|
|
#include <OysterMath.h>
|
2014-02-09 16:42:26 +01:00
|
|
|
|
2014-01-29 10:18:01 +01:00
|
|
|
#define NOMINMAX
|
2014-01-13 12:44:33 +01:00
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Utility::DynamicMemory;
|
|
|
|
using namespace Oyster;
|
|
|
|
using namespace Oyster::Network;
|
|
|
|
using namespace Oyster::Thread;
|
|
|
|
using namespace GameLogic;
|
|
|
|
|
|
|
|
namespace DanBias
|
|
|
|
{
|
2014-02-09 16:42:26 +01:00
|
|
|
Utility::WinTimer testTimer;
|
|
|
|
int testID = -1;
|
|
|
|
|
2014-01-29 10:18:01 +01:00
|
|
|
bool GameSession::DoWork( )
|
2014-01-13 12:44:33 +01:00
|
|
|
{
|
2014-01-29 10:18:01 +01:00
|
|
|
if(this->isRunning)
|
2014-01-13 12:44:33 +01:00
|
|
|
{
|
2014-01-31 22:52:52 +01:00
|
|
|
float dt = (float)this->logicTimer.getElapsedSeconds();
|
2014-02-09 16:42:26 +01:00
|
|
|
if( dt >= this->logicFrameTime )
|
2014-01-29 10:18:01 +01:00
|
|
|
{
|
|
|
|
this->ProcessClients();
|
|
|
|
this->gameInstance.NewFrame();
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-02-09 16:42:26 +01:00
|
|
|
this->logicTimer.reset();
|
2014-01-29 10:18:01 +01:00
|
|
|
}
|
2014-01-13 12:44:33 +01:00
|
|
|
}
|
2014-01-29 10:18:01 +01:00
|
|
|
|
|
|
|
return this->isRunning;
|
2014-01-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
void GameSession::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
|
|
|
|
{
|
2014-01-31 08:41:08 +01:00
|
|
|
int temp = -1;
|
|
|
|
//Find the idiot
|
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
|
|
|
{
|
|
|
|
if(this->clients[i]->Equals(e.sender))
|
|
|
|
{
|
|
|
|
temp = i;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 15:01:14 +01:00
|
|
|
|
2014-01-31 08:41:08 +01:00
|
|
|
if(temp == -1)
|
|
|
|
{
|
|
|
|
this->Detach(e.sender)->Disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SmartPointer<GameClient> cl = this->clients[temp];
|
|
|
|
|
|
|
|
switch (e.args.type)
|
|
|
|
{
|
|
|
|
case NetworkClient::ClientEventArgs::EventType_Disconnect:
|
|
|
|
break;
|
|
|
|
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
|
|
|
|
break;
|
|
|
|
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
|
2014-02-09 16:42:26 +01:00
|
|
|
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str());
|
2014-01-31 22:52:52 +01:00
|
|
|
this->Detach(e.sender);
|
2014-01-31 08:41:08 +01:00
|
|
|
break;
|
|
|
|
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
|
2014-02-09 16:42:26 +01:00
|
|
|
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str());
|
|
|
|
testID = 2;
|
|
|
|
if(cl->GetPlayer()->GetID() == testID)//TODO: TEST
|
|
|
|
{
|
|
|
|
testTimer.reset();
|
|
|
|
}
|
2014-01-31 08:41:08 +01:00
|
|
|
this->ParseProtocol(e.args.data.protocol, cl);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameSession::ObjectMove(GameLogic::IObjectData* movedObject)
|
|
|
|
{
|
2014-02-11 10:21:47 +01:00
|
|
|
//float dt = (float)GameSession::gameSession->networkTimer.getElapsedSeconds();
|
2014-02-09 16:42:26 +01:00
|
|
|
//Duh... This was causing alot of problems, it's in the wrong place...
|
|
|
|
//Need to figure out where to put this frame locker.
|
|
|
|
//We only need to send network packages when necessary, ie not 120 times per frame.
|
|
|
|
//I think it would be enough with 60-70 packages per second due to the nature of
|
|
|
|
//graphics update (60 fps) on the client side. To send more than this would be lost
|
|
|
|
//bandwidth.
|
|
|
|
//if( dt >= GameSession::gameSession->networkFrameTime )
|
2014-02-11 10:21:47 +01:00
|
|
|
//{
|
2014-02-11 13:03:37 +01:00
|
|
|
// GameSession::gameSession->networkTimer.reset();
|
2014-02-11 10:21:47 +01:00
|
|
|
int id = movedObject->GetID();
|
2014-02-11 13:03:37 +01:00
|
|
|
//Protocol_ObjectPosition p(movedObject->GetPosition(), id);
|
|
|
|
Protocol_ObjectPositionRotation p(movedObject->GetPosition(), movedObject->GetRotation(), id);
|
2014-02-09 16:42:26 +01:00
|
|
|
GameSession::gameSession->Send(p.GetProtocol());
|
2014-02-11 10:21:47 +01:00
|
|
|
//}
|
2014-01-29 15:01:14 +01:00
|
|
|
|
|
|
|
}
|
2014-02-09 16:42:26 +01:00
|
|
|
void GameSession::ObjectDisabled( GameLogic::IObjectData* movedObject, float seconds )
|
|
|
|
{
|
|
|
|
GameSession::gameSession->Send(Protocol_ObjectDisable(movedObject->GetID(), seconds).GetProtocol());
|
|
|
|
}
|
2014-01-29 15:01:14 +01:00
|
|
|
|
|
|
|
//*****************************************************//
|
|
|
|
//****************** Protocol methods *****************//
|
|
|
|
//******************************************************************************************************************//
|
2014-01-29 10:18:01 +01:00
|
|
|
|
|
|
|
void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c)
|
2014-01-13 12:44:33 +01:00
|
|
|
{
|
2014-02-04 16:07:10 +01:00
|
|
|
//TODO: Update response timer
|
|
|
|
|
2014-01-30 14:15:25 +01:00
|
|
|
switch (p[0].value.netShort)
|
2014-01-13 12:44:33 +01:00
|
|
|
{
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerMovementBackward: this->Gameplay_PlayerMovementBack ( c );
|
2014-01-13 12:44:33 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerMovementForward: this->Gameplay_PlayerMovementForth ( c );
|
2014-01-13 12:44:33 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerMovementLeft: this->Gameplay_PlayerMovementLeft ( c );
|
2014-01-13 12:44:33 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerMovementRight: this->Gameplay_PlayerMovementRight ( c );
|
2014-01-13 12:44:33 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerJump: this->Gameplay_PlayerJump ( c );
|
2014-01-30 09:40:58 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerLookDir: this->Gameplay_PlayerLookDir ( Protocol_PlayerLook (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerChangeWeapon: this->Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_PlayerShot: this->Gameplay_PlayerShot ( Protocol_PlayerShot (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
|
|
|
|
case protocol_Gameplay_ObjectPickup: this->Gameplay_ObjectPickup ( Protocol_ObjectPickup (p), c );
|
|
|
|
break;
|
|
|
|
case protocol_Gameplay_ObjectDamage: this->Gameplay_ObjectDamage ( Protocol_ObjectDamage (p), c );
|
|
|
|
break;
|
|
|
|
case protocol_Gameplay_ObjectPosition: this->Gameplay_ObjectPosition ( Protocol_ObjectPosition (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_ObjectEnabled: this->Gameplay_ObjectEnabled ( Protocol_ObjectEnable (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_ObjectDisabled: this->Gameplay_ObjectDisabled ( Protocol_ObjectDisable (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_Gameplay_ObjectCreate: this->Gameplay_ObjectCreate ( Protocol_ObjectCreate (p), c );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case protocol_General_Status: this->General_Status ( Protocol_General_Status (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
2014-02-11 10:21:47 +01:00
|
|
|
case protocol_General_Text: this->General_Text ( Protocol_General_Text (p), c );
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_PlayerMovementBack ( DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-02-11 10:21:47 +01:00
|
|
|
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD);
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_PlayerMovementForth ( DanBias::GameClient* c )
|
|
|
|
{
|
|
|
|
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD);
|
|
|
|
}
|
|
|
|
void GameSession::Gameplay_PlayerMovementLeft ( DanBias::GameClient* c )
|
|
|
|
{
|
|
|
|
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT);
|
|
|
|
}
|
|
|
|
void GameSession::Gameplay_PlayerMovementRight ( DanBias::GameClient* c )
|
|
|
|
{
|
|
|
|
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT);
|
|
|
|
}
|
|
|
|
void GameSession::Gameplay_PlayerJump ( DanBias::GameClient* c )
|
|
|
|
{
|
|
|
|
c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_JUMP);
|
|
|
|
}
|
|
|
|
void GameSession::Gameplay_PlayerLookDir ( Protocol_PlayerLook& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-02-03 15:52:00 +01:00
|
|
|
Oyster::Math3D::Float4 lookDir;
|
2014-01-29 15:01:14 +01:00
|
|
|
lookDir.x = p.lookDirX;
|
|
|
|
lookDir.y = p.lookDirY;
|
|
|
|
lookDir.z = p.lookDirZ;
|
2014-02-03 15:52:00 +01:00
|
|
|
lookDir.w = p.deltaX;
|
2014-01-29 15:01:14 +01:00
|
|
|
c->GetPlayer()->Rotate(lookDir);
|
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_PlayerShot ( Protocol_PlayerShot& p, DanBias::GameClient* c )
|
2014-02-05 15:54:48 +01:00
|
|
|
{
|
|
|
|
if(p.secondaryPressed) c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_SECONDARY_PRESS);
|
|
|
|
if(p.primaryPressed) c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_PRIMARY_PRESS);
|
|
|
|
|
|
|
|
if(p.utilityPressed) c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_UTILLITY_PRESS);
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
void GameSession::Gameplay_ObjectPickup ( Protocol_ObjectPickup& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_ObjectDamage ( Protocol_ObjectDamage& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-22 15:22:52 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_ObjectPosition ( Protocol_ObjectPosition& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-22 15:22:52 +01:00
|
|
|
|
2014-01-13 12:44:33 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_ObjectEnabled ( Protocol_ObjectEnable& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-13 12:44:33 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_ObjectDisabled ( Protocol_ObjectDisable& p, DanBias::GameClient* c )
|
2014-01-20 15:47:52 +01:00
|
|
|
{
|
2014-01-29 15:01:14 +01:00
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::Gameplay_ObjectCreate ( Protocol_ObjectCreate& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-01-20 15:47:52 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::General_Status ( Protocol_General_Status& p, DanBias::GameClient* c )
|
2014-01-29 10:18:01 +01:00
|
|
|
{
|
2014-01-29 15:01:14 +01:00
|
|
|
switch (p.status)
|
|
|
|
{
|
|
|
|
case GameLogic::Protocol_General_Status::States_disconected:
|
|
|
|
printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID());
|
2014-02-04 16:07:10 +01:00
|
|
|
//TODO: Tell other clients
|
|
|
|
//Protocol_
|
2014-01-29 15:01:14 +01:00
|
|
|
this->Detach(c->GetClient()->GetID());
|
|
|
|
break;
|
2014-01-29 10:18:01 +01:00
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
case GameLogic::Protocol_General_Status::States_idle:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GameLogic::Protocol_General_Status::States_ready:
|
2014-02-04 16:07:10 +01:00
|
|
|
c->SetReadyState(true);
|
2014-01-29 15:01:14 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GameLogic::Protocol_General_Status::States_leave:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-01-29 10:18:01 +01:00
|
|
|
}
|
2014-02-11 10:21:47 +01:00
|
|
|
void GameSession::General_Text ( Protocol_General_Text& p, DanBias::GameClient* c )
|
2014-01-29 15:01:14 +01:00
|
|
|
{
|
2014-02-09 16:42:26 +01:00
|
|
|
printf("Message recieved from (%i):\t %s\n", c->GetClient()->GetID(), p.text.c_str());
|
2014-01-29 15:01:14 +01:00
|
|
|
}
|
|
|
|
|
2014-01-13 12:44:33 +01:00
|
|
|
}//End namespace DanBias
|
|
|
|
|
2014-01-29 15:01:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|