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