Fixed client movement.

This commit is contained in:
Jocke
2015-12-10 14:14:36 +01:00
parent 663aca2641
commit f377d9ba70
4 changed files with 234 additions and 196 deletions
+2 -1
View File
@@ -27,7 +27,7 @@ public:
void Close(); void Close();
private: private:
void ReadFromServer(); void ReadFromServer();
void SendToServer(); void SendSnapshotToServer();
int Receive(char* data, size_t length); int Receive(char* data, size_t length);
int CreateMessage(MessageType type, std::string message, char* data); int CreateMessage(MessageType type, std::string message, char* data);
@@ -60,6 +60,7 @@ private:
// Use to check if we should send disconnect message // Use to check if we should send disconnect message
// if game is turned of by closing window. // if game is turned of by closing window.
bool m_WasStarted = false; bool m_WasStarted = false;
IsWASDKeyDown m_IsWASDKeyDown;
// Events // Events
EventBroker* m_EventBroker; EventBroker* m_EventBroker;
@@ -7,9 +7,12 @@
#define BOARDSIZE 16 #define BOARDSIZE 16
#define MAXCONNECTIONS 8 #define MAXCONNECTIONS 8
#define INPUTSIZE 128 #define INPUTSIZE 128
#define PLAYERSPEED 0.2f;
typedef boost::shared_ptr<boost::asio::ip::udp::socket> socket_ptr; typedef boost::shared_ptr<boost::asio::ip::udp::socket> socket_ptr;
typedef boost::shared_ptr<std::string> string_ptr; typedef boost::shared_ptr<std::string> string_ptr;
typedef boost::shared_ptr<std::queue<string_ptr>> messageQueue_ptr; typedef boost::shared_ptr<std::queue<string_ptr>> messageQueue_ptr;
#endif #endif
+10 -2
View File
@@ -4,9 +4,17 @@
struct SnapshotDefinitions struct SnapshotDefinitions
{ {
// "+Forward" is 8 characters * sizeof(char) = 8 // "+Forward" is 8 characters * sizeof(char) = 8
char* inputForward = new char[8]; char* InputForward = new char[8];
// "+Right" is 6 characters * sizeof(char) = 6 // "+Right" is 6 characters * sizeof(char) = 6
char* inputRight = new char[6]; char* InputRight = new char[6];
};
struct IsWASDKeyDown
{
bool W = false;
bool A = false;
bool S = false;
bool D = false;
}; };
#endif #endif
+55 -29
View File
@@ -63,27 +63,46 @@ void Client::ReadFromServer()
} }
std::clock_t currentTime = std::clock(); std::clock_t currentTime = std::clock();
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) { if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
SendToServer(); SendSnapshotToServer();
previousSnapshotMessage = currentTime; 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 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( m_Socket.send_to(boost::asio::buffer(
dataPackage, dataPackage,
len), len),
m_ReceiverEndpoint, 0); m_ReceiverEndpoint, 0);
delete[] dataPackage; 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 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( m_Socket.send_to(boost::asio::buffer(
dataPackage, dataPackage,
len), len),
@@ -159,8 +178,7 @@ void Client::ParseEventMessage(char* data, size_t length)
MoveMessageHead(data, length, sizeof(int)); MoveMessageHead(data, length, sizeof(int));
// Sett Player name // Sett Player name
m_PlayerDefinitions[Id].Name = command.erase(0, 7); m_PlayerDefinitions[Id].Name = command.erase(0, 7);
} } else {
else {
std::cout << "Event message: " << std::string(data) << std::endl; 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 // New player connected on the server side
if (m_PlayerDefinitions[i].Name == "" && tempName != "") { if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
CreateNewPlayer(i); CreateNewPlayer(i);
} } else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
// Someone disconnected // Someone disconnected
// TODO: Insert code here // TODO: Insert code here
} } else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
// Not a connected player // Not a connected player
break; break;
} }
@@ -206,20 +222,16 @@ void Client::ParseSnapshot(char* data, size_t length)
int Client::Receive(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 int bytesReceived = m_Socket.receive_from(boost
::asio::buffer((void*)data, length), ::asio::buffer((void*)data, length),
m_ReceiverEndpoint, m_ReceiverEndpoint,
0); 0, error);
std::cout << "ReadFromServer crashed: " << error.message();
return bytesReceived; 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) 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 char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
if (event.KeyCode == GLFW_KEY_W) { if (event.KeyCode == GLFW_KEY_W) {
m_NextSnapshot.inputForward = "+Forward"; m_IsWASDKeyDown.W = true;
//m_NextSnapshot.inputForward = "+Forward";
} }
if (event.KeyCode == GLFW_KEY_A) { if (event.KeyCode == GLFW_KEY_A) {
m_NextSnapshot.inputRight = "-Right"; m_IsWASDKeyDown.A = true;
//m_NextSnapshot.inputRight = "-Right";
} }
if (event.KeyCode == GLFW_KEY_S) { if (event.KeyCode == GLFW_KEY_S) {
m_NextSnapshot.inputForward = "-Forward"; m_IsWASDKeyDown.S = true;
//m_NextSnapshot.inputForward = "-Forward";
} }
if (event.KeyCode == GLFW_KEY_D) { if (event.KeyCode == GLFW_KEY_D) {
m_NextSnapshot.inputRight = "+Right"; m_IsWASDKeyDown.D = true;
//m_NextSnapshot.inputRight = "+Right";
} }
if (event.KeyCode == GLFW_KEY_V) { if (event.KeyCode == GLFW_KEY_V) {
@@ -315,12 +331,22 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
bool Client::OnKeyUp(const Events::KeyUp & e) bool Client::OnKeyUp(const Events::KeyUp & e)
{ {
if (e.KeyCode == GLFW_KEY_W || e.KeyCode == GLFW_KEY_S) { if (e.KeyCode == GLFW_KEY_W) {
m_NextSnapshot.inputForward = ""; m_IsWASDKeyDown.W = false;
//m_NextSnapshot.inputForward = "";
return true; return true;
} }
if (e.KeyCode == GLFW_KEY_A || e.KeyCode == GLFW_KEY_D) { if (e.KeyCode == GLFW_KEY_A){
m_NextSnapshot.inputRight = ""; 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 true;
} }
return false; return false;