|
|
|
@ -4,6 +4,7 @@
|
|
|
|
|
#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"
|
|
|
|
@ -20,6 +21,7 @@ const float BASE_BOUNCE_DEG = 60;
|
|
|
|
|
const float BASE_BOUNCE_RAD = (BASE_BOUNCE_DEG / 180.0) * M_PI;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|
|
|
|
float paddle_mid_y = (paddle.getRect().y + paddle.getRect().GetHeight()) / 2.0; /* Middle y value of rectangle */
|
|
|
|
@ -44,14 +46,15 @@ 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. */
|
|
|
|
|
function as pointers. It returns an enum that indicates whether the game is in server,
|
|
|
|
|
client or single player mode.*/
|
|
|
|
|
|
|
|
|
|
void check_server_client(int argc, char** argv, Server* server, Client* client) {
|
|
|
|
|
Mode check_server_client(int argc, char** argv, Server* server, Client* client) {
|
|
|
|
|
std::string connect_code;
|
|
|
|
|
std::vector<std::string> addr_port; /* Vector to store (IPv4) address and port */
|
|
|
|
|
|
|
|
|
|
if (argc < 2) { /* Game was not started in client or server mode */
|
|
|
|
|
return;
|
|
|
|
|
return M_SINGLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GAME STARTED IN CLIENT MODE */
|
|
|
|
@ -63,6 +66,7 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
|
|
|
|
|
addr_port = connect_code::decode(connect_code);
|
|
|
|
|
try {
|
|
|
|
|
client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
|
|
|
|
|
return M_CLIENT;
|
|
|
|
|
} catch (int e) {
|
|
|
|
|
throw;
|
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
@ -90,10 +94,16 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
|
|
|
|
|
port = std::stoi(std::string(argv[3]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if IP is valid */
|
|
|
|
|
if (check_ip_ver(addr.data()) < 0) {
|
|
|
|
|
throw EXCEPT_INVALIDARGS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
} catch (int e) {
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
@ -106,23 +116,25 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
|
|
|
|
|
throw EXCEPT_INVALIDARGS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
Server server;
|
|
|
|
|
Client client;
|
|
|
|
|
try {
|
|
|
|
|
check_server_client(argc, argv, &server, &client);
|
|
|
|
|
mode = check_server_client(argc, argv, &server, &client);
|
|
|
|
|
} catch(int e) {
|
|
|
|
|
if (e == EXCEPT_TOOFEWARGS) {
|
|
|
|
|
std::cout << "Started in client mode, but no address was specified." << std::endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (e == EXCEPT_INVALIDARGS) {
|
|
|
|
|
std::cout << "Invalid argument. Optional arguments are -S for server mode, or -C for client mode." << std::endl;
|
|
|
|
|
std::cout << "Invalid argument.." << std::endl;
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (e == EXCEPT_CONNREFUSED) {
|
|
|
|
|