Defined a 'GameType' struct that can hold both the mode, and the relevant socket, to enable polymorphism when calling socket methods

master
Aadhavan Srinivasan 8 months ago
parent ee0c106236
commit cb0fe1af6b

@ -23,6 +23,17 @@ const float BASE_SPEED_COMPONENTS = 18;
const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2);
typedef enum {M_SINGLE, M_CLIENT, M_SERVER} Mode;
/* This struct contains a Mode enum, which indicates the type of game we are
playing (Single player, client mode or server mode). The netsock parameter is
a 'Sock' object - Client and Server classes inherit from this object, so this
parameter can be instantiated to either a client or server, depending on the
game type. */
typedef struct {
Mode mode;
Sock netsock;
} GameType;
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
float paddle_mid_y = (paddle.getRect().y + paddle.getRect().GetHeight()) / 2.0; /* Middle y value of rectangle */
float ball_y = ball.pos.y; /* Y co-ordinate of ball */
@ -46,15 +57,18 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
/* This function checks the command-line arguments passed to the program.
It then decides whether the game is in Server or Client mode (or neither), and
instantiates the appropriate object. The (uninitialized) objects are passed to the
function as pointers. It returns an enum that indicates whether the game is in server,
client or single player mode.*/
function as pointers. It returns a GameType struct, that indicates whether the game
is in server, client or single player mode, and contains the appropriate socket object. */
Mode check_server_client(int argc, char** argv, Server* server, Client* client) {
GameType check_server_client(int argc, char** argv) {
std::string connect_code;
std::vector<std::string> addr_port; /* Vector to store (IPv4) address and port */
GameType type;
if (argc < 2) { /* Game was not started in client or server mode */
return M_SINGLE;
type.mode = M_SINGLE;
type.netsock = nullptr;
return type;
}
/* GAME STARTED IN CLIENT MODE */
@ -65,8 +79,10 @@ Mode check_server_client(int argc, char** argv, Server* server, Client* client)
connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
try {
addr_port = connect_code::decode(connect_code);
client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
return M_CLIENT;
Client client = Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
type.mode = M_CLIENT;
type.netsock = client;
return type;
} catch (int e) {
throw;
} catch (std::exception& e) {
@ -102,8 +118,10 @@ Mode check_server_client(int argc, char** argv, Server* server, Client* client)
std::string code = connect_code::encode(addr, std::to_string(port));
std::cout << "Your code is " << code << std::endl;
try {
server = new Server(4, 'T', addr.data(), port);
return M_SERVER;
Server server = Server(4, 'T', addr.data(), port);
type.mode = M_SERVER;
type.netsock = server;
return type;
} catch (int e) {
throw;
}
@ -121,13 +139,12 @@ Mode check_server_client(int argc, char** argv, Server* server, Client* client)
int main(int argc, char** argv) {
/* Check if game was started in server or client mode, and set appropriate variables */
/* mode - M_CLIENT for client, M_SERVER for SERVER and M_SINGLE for single-player */
Mode mode;
/* GameType struct, to define whether the game is in single or muilti-player mode, and
to hold the appropriate socket */
GameType type;
Server server;
Client client;
try {
mode = check_server_client(argc, argv, &server, &client);
type = check_server_client(argc, argv);
} catch(int e) {
if (e == EXCEPT_TOOFEWARGS) {
std::cout << "Started in client mode, but no address was specified." << std::endl;
@ -163,7 +180,6 @@ int main(int argc, char** argv) {
std::string points_str = std::string("0\t\t0");
bool game_started = false;
srand(std::time(NULL));
bool in_server_mode = false;
/* Instantiate Paddle and Ball objects */
Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H);
@ -187,32 +203,58 @@ int main(int argc, char** argv) {
if (game_started) {
/* Update paddle velocity */
if (IsKeyPressed(KEY_S)) {
pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */
/* Left paddle - controlled by client */
/* Up motion */
if (type.mode == M_CLIENT) {
if (IsKeyPressed(KEY_S)) {
client.sendAll(std::string("D"));
pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */
}
}
if (IsKeyPressed(KEY_W)) {
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
if (type.mode == M_SERVER) {
if (server.recvAll() == "U") {
pad1.velocity.y = PADDLE_SPEED;
}
}
/* Down motion */
if (type.mode == M_CLIENT) {
if (IsKeyPressed(KEY_W)) {
client.sendAll(std::string("U"));
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
}
}
if (type.mode == M_SERVER) {
if (server.recvAll() == "D") {
pad1.velocity.y = (-1) * PADDLE_SPEED;
}
}
if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) {
if (type.mode == M_CLIENT) {
client.sendAll(std::string("S"));
pad1.velocity.y = 0;
}
if (IsKeyPressed(KEY_UP)) {
if(in_server_mode) {
client.sendAll(std::string("U"));
}
pad2.velocity.y = (-1) * PADDLE_SPEED;
}
/* Right paddle */
if (IsKeyPressed(KEY_DOWN)) {
if (in_server_mode) {
client.sendAll(std::string("D"));
if (type.mode == M_SERVER) {
server.sendAll(std::string("D"));
}
pad2.velocity.y = PADDLE_SPEED;
}
if (IsKeyPressed(KEY_UP)) {
if (type.mode == M_SERVER) {
server.sendAll(std::string("U"));
}
pad2.velocity.y = (-1) * PADDLE_SPEED;
}
if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) {
if (in_server_mode) {
client.sendAll(std::string("S"));
if (type.mode == M_SERVER) {
server.sendAll(std::string("S"));
}
pad2.velocity.y = 0;
}

Loading…
Cancel
Save