Changed include paths to relative paths, and added an agnostic struct.

The agnostic struct consists of a 'Mode' enum, and a 'Sock' type. The
'Sock' can be either a Server or Client, depending on the type of game.
This allows polymorphism, as I don't have to worry about whether the game
is being run in Server or Client mode, and I can call the same methods regardless.
master
Aadhavan Srinivasan 10 months ago
parent 26427fa257
commit 1b1dc4a3a2

@ -1,15 +1,14 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <ctime> #include <ctime>
#include "raylib-cpp/raylib-cpp.hpp" #include "includes/raylib-cpp/raylib-cpp.hpp"
#include "paddle.hpp" #include "includes/paddle.hpp"
#include "ball.hpp" #include "includes/ball.hpp"
#include "easysock.hpp" #include "includes/easysock.hpp"
#include "math-helpers.hpp" #include "includes/math-helpers.hpp"
#include "connect-helpers.hpp" #include "includes/connect-helpers.hpp"
#include "client.hpp" #include "includes/client.hpp"
#include "server.hpp" #include "includes/server.hpp"
/* Global variables used to instantiate structs */ /* Global variables used to instantiate structs */
const int WIDTH = 1500; const int WIDTH = 1500;
const int HEIGHT = 600; const int HEIGHT = 600;
@ -171,8 +170,12 @@ int main(int argc, char** argv) {
return -6; return -6;
} }
#ifdef DEBUG
type.mode = M_CLIENT;
#endif
/* Initialize window and other variables */ /* Initialize window and other variables */
SetTraceLogLevel(LOG_NONE); SetTraceLogLevel(LOG_INFO);
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
window.ClearBackground(BLACK); window.ClearBackground(BLACK);
SetTargetFPS(60); SetTargetFPS(60);
@ -202,45 +205,45 @@ int main(int argc, char** argv) {
} }
if (game_started) { if (game_started) {
/* Receive response from the other machine, if the game is in multiplayer mode */
std::string response = "";
if (type.mode != M_SINGLE) {
#ifdef DEBUG
response = "";
#else
std::string response = type.netsock->recvAll();
#endif
}
std::string to_send = "";
/* Update paddle velocity */ /* Update paddle velocity */
/* Left paddle - controlled by client */ /* Left paddle (controlled by client) */
/* Up motion */ /* Up motion */
if (type.mode == M_CLIENT) { if (IsKeyPressed(KEY_S) || response == "D1") {
if (IsKeyPressed(KEY_S)) { pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */
type.netsock->sendAll(std::string("D")); if (type.mode == M_CLIENT) { /* If this machine controls this paddle, Send the key press to the other machine */
pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */ to_send = "D1";
}
}
if (type.mode == M_SERVER) {
if (type.netsock->recvAll() == "U") {
pad1.velocity.y = PADDLE_SPEED;
} }
} }
/* Down motion */ /* Down motion */
if (type.mode == M_CLIENT) { if (IsKeyPressed(KEY_W) || response == "U1") {
if (IsKeyPressed(KEY_W)) { pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
type.netsock->sendAll(std::string("U")); if (type.mode == M_CLIENT) {
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */ to_send = "U1";
}
}
if (type.mode == M_SERVER) {
if (type.netsock->recvAll() == "D") {
pad1.velocity.y = (-1) * PADDLE_SPEED;
} }
} }
/* Stop */
if ((IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) || (response == "S1")) {
if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) {
if (type.mode == M_CLIENT) {
type.netsock->sendAll(std::string("S"));
pad1.velocity.y = 0; pad1.velocity.y = 0;
if (type.mode == M_CLIENT) {
to_send = "S1";
} }
} }
/* Right paddle */ /* Right paddle - controlled by server*/
if (IsKeyPressed(KEY_DOWN)) { if (IsKeyPressed(KEY_DOWN)) {
if (type.mode == M_SERVER) { if (type.mode == M_SERVER) {
type.netsock->sendAll(std::string("D")); type.netsock->sendAll(std::string("D"));

Loading…
Cancel
Save