|
|
|
@@ -63,27 +63,46 @@ void Client::ReadFromServer()
|
|
|
|
|
}
|
|
|
|
|
std::clock_t currentTime = std::clock();
|
|
|
|
|
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
|
|
|
|
SendToServer();
|
|
|
|
|
SendSnapshotToServer();
|
|
|
|
|
previousSnapshotMessage = currentTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Client::SendToServer()
|
|
|
|
|
void Client::SendSnapshotToServer()
|
|
|
|
|
{
|
|
|
|
|
if (m_NextSnapshot.inputForward != "") {
|
|
|
|
|
// Reset previouse key state in snapshot.
|
|
|
|
|
m_NextSnapshot.InputRight = "";
|
|
|
|
|
m_NextSnapshot.InputRight = "";
|
|
|
|
|
// See if any movement keys are down
|
|
|
|
|
// We dont care if it's overwritten by later
|
|
|
|
|
// if statement. Watcha gonna do, right!
|
|
|
|
|
if (m_IsWASDKeyDown.W) {
|
|
|
|
|
m_NextSnapshot.InputRight = "+Forward";
|
|
|
|
|
}
|
|
|
|
|
if (m_IsWASDKeyDown.A) {
|
|
|
|
|
m_NextSnapshot.InputRight = "-Right";
|
|
|
|
|
}
|
|
|
|
|
if (m_IsWASDKeyDown.S) {
|
|
|
|
|
m_NextSnapshot.InputRight = "-Forward";
|
|
|
|
|
}
|
|
|
|
|
if (m_IsWASDKeyDown.D) {
|
|
|
|
|
m_NextSnapshot.InputRight = "+Right";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_NextSnapshot.InputForward != "") {
|
|
|
|
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
|
|
|
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.inputForward, dataPackage);
|
|
|
|
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.InputForward, dataPackage);
|
|
|
|
|
m_Socket.send_to(boost::asio::buffer(
|
|
|
|
|
dataPackage,
|
|
|
|
|
len),
|
|
|
|
|
m_ReceiverEndpoint, 0);
|
|
|
|
|
delete[] dataPackage;
|
|
|
|
|
}
|
|
|
|
|
if (m_NextSnapshot.inputRight != "") {
|
|
|
|
|
if (m_NextSnapshot.InputRight != "") {
|
|
|
|
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
|
|
|
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.inputRight, dataPackage);
|
|
|
|
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.InputRight, dataPackage);
|
|
|
|
|
m_Socket.send_to(boost::asio::buffer(
|
|
|
|
|
dataPackage,
|
|
|
|
|
len),
|
|
|
|
@@ -159,8 +178,7 @@ void Client::ParseEventMessage(char* data, size_t length)
|
|
|
|
|
MoveMessageHead(data, length, sizeof(int));
|
|
|
|
|
// Sett Player name
|
|
|
|
|
m_PlayerDefinitions[Id].Name = command.erase(0, 7);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "Event message: " << std::string(data) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -190,12 +208,10 @@ void Client::ParseSnapshot(char* data, size_t length)
|
|
|
|
|
// New player connected on the server side
|
|
|
|
|
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
|
|
|
|
CreateNewPlayer(i);
|
|
|
|
|
}
|
|
|
|
|
else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
|
|
|
|
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
|
|
|
|
// Someone disconnected
|
|
|
|
|
// TODO: Insert code here
|
|
|
|
|
}
|
|
|
|
|
else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
|
|
|
|
|
} else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
|
|
|
|
|
// Not a connected player
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@@ -206,20 +222,16 @@ void Client::ParseSnapshot(char* data, size_t length)
|
|
|
|
|
|
|
|
|
|
int Client::Receive(char* data, size_t length)
|
|
|
|
|
{
|
|
|
|
|
boost::system::error_code error;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int bytesReceived = m_Socket.receive_from(boost
|
|
|
|
|
::asio::buffer((void*)data, length),
|
|
|
|
|
m_ReceiverEndpoint,
|
|
|
|
|
0);
|
|
|
|
|
0, error);
|
|
|
|
|
|
|
|
|
|
std::cout << "ReadFromServer crashed: " << error.message();
|
|
|
|
|
|
|
|
|
|
return bytesReceived;
|
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
|
// To not spam "socket closed messages"
|
|
|
|
|
//if (std::string(err.what()).find("forcefully closed") != std::string::npos) {
|
|
|
|
|
std::cout << "Read from client crashed: " << err.what();
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Client::CreateMessage(MessageType type, std::string message, char* data)
|
|
|
|
@@ -287,16 +299,20 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
|
|
|
|
{
|
|
|
|
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
|
|
|
|
if (event.KeyCode == GLFW_KEY_W) {
|
|
|
|
|
m_NextSnapshot.inputForward = "+Forward";
|
|
|
|
|
m_IsWASDKeyDown.W = true;
|
|
|
|
|
//m_NextSnapshot.inputForward = "+Forward";
|
|
|
|
|
}
|
|
|
|
|
if (event.KeyCode == GLFW_KEY_A) {
|
|
|
|
|
m_NextSnapshot.inputRight = "-Right";
|
|
|
|
|
m_IsWASDKeyDown.A = true;
|
|
|
|
|
//m_NextSnapshot.inputRight = "-Right";
|
|
|
|
|
}
|
|
|
|
|
if (event.KeyCode == GLFW_KEY_S) {
|
|
|
|
|
m_NextSnapshot.inputForward = "-Forward";
|
|
|
|
|
m_IsWASDKeyDown.S = true;
|
|
|
|
|
//m_NextSnapshot.inputForward = "-Forward";
|
|
|
|
|
}
|
|
|
|
|
if (event.KeyCode == GLFW_KEY_D) {
|
|
|
|
|
m_NextSnapshot.inputRight = "+Right";
|
|
|
|
|
m_IsWASDKeyDown.D = true;
|
|
|
|
|
//m_NextSnapshot.inputRight = "+Right";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.KeyCode == GLFW_KEY_V) {
|
|
|
|
@@ -315,12 +331,22 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
|
|
|
|
|
|
|
|
|
bool Client::OnKeyUp(const Events::KeyUp & e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_W || e.KeyCode == GLFW_KEY_S) {
|
|
|
|
|
m_NextSnapshot.inputForward = "";
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_W) {
|
|
|
|
|
m_IsWASDKeyDown.W = false;
|
|
|
|
|
//m_NextSnapshot.inputForward = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_A || e.KeyCode == GLFW_KEY_D) {
|
|
|
|
|
m_NextSnapshot.inputRight = "";
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_A){
|
|
|
|
|
m_IsWASDKeyDown.A = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_S){
|
|
|
|
|
m_IsWASDKeyDown.S = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (e.KeyCode == GLFW_KEY_D) {
|
|
|
|
|
m_IsWASDKeyDown.D = false;
|
|
|
|
|
//m_NextSnapshot.inputRight = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|