|
|
@ -31,7 +31,7 @@ typedef enum {M_SINGLE, M_CLIENT, M_SERVER} Mode;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
typedef struct {
|
|
|
|
Mode mode;
|
|
|
|
Mode mode;
|
|
|
|
Sock netsock;
|
|
|
|
Sock* netsock;
|
|
|
|
} GameType;
|
|
|
|
} GameType;
|
|
|
|
|
|
|
|
|
|
|
|
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|
|
|
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|
|
@ -79,7 +79,7 @@ GameType check_server_client(int argc, char** argv) {
|
|
|
|
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. */
|
|
|
|
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 {
|
|
|
|
try {
|
|
|
|
addr_port = connect_code::decode(connect_code);
|
|
|
|
addr_port = connect_code::decode(connect_code);
|
|
|
|
Client client = Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
|
|
|
|
Client* client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
|
|
|
|
type.mode = M_CLIENT;
|
|
|
|
type.mode = M_CLIENT;
|
|
|
|
type.netsock = client;
|
|
|
|
type.netsock = client;
|
|
|
|
return type;
|
|
|
|
return type;
|
|
|
@ -118,7 +118,7 @@ GameType check_server_client(int argc, char** argv) {
|
|
|
|
std::string code = connect_code::encode(addr, std::to_string(port));
|
|
|
|
std::string code = connect_code::encode(addr, std::to_string(port));
|
|
|
|
std::cout << "Your code is " << code << std::endl;
|
|
|
|
std::cout << "Your code is " << code << std::endl;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Server server = Server(4, 'T', addr.data(), port);
|
|
|
|
Server* server = new Server(4, 'T', addr.data(), port);
|
|
|
|
type.mode = M_SERVER;
|
|
|
|
type.mode = M_SERVER;
|
|
|
|
type.netsock = server;
|
|
|
|
type.netsock = server;
|
|
|
|
return type;
|
|
|
|
return type;
|
|
|
@ -208,12 +208,12 @@ int main(int argc, char** argv) {
|
|
|
|
/* Up motion */
|
|
|
|
/* Up motion */
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
if (IsKeyPressed(KEY_S)) {
|
|
|
|
if (IsKeyPressed(KEY_S)) {
|
|
|
|
client.sendAll(std::string("D"));
|
|
|
|
type.netsock->sendAll(std::string("D"));
|
|
|
|
pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */
|
|
|
|
pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (server.recvAll() == "U") {
|
|
|
|
if (type.netsock->recvAll() == "U") {
|
|
|
|
pad1.velocity.y = PADDLE_SPEED;
|
|
|
|
pad1.velocity.y = PADDLE_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -221,12 +221,12 @@ int main(int argc, char** argv) {
|
|
|
|
/* Down motion */
|
|
|
|
/* Down motion */
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
if (IsKeyPressed(KEY_W)) {
|
|
|
|
if (IsKeyPressed(KEY_W)) {
|
|
|
|
client.sendAll(std::string("U"));
|
|
|
|
type.netsock->sendAll(std::string("U"));
|
|
|
|
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
|
|
|
|
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (server.recvAll() == "D") {
|
|
|
|
if (type.netsock->recvAll() == "D") {
|
|
|
|
pad1.velocity.y = (-1) * PADDLE_SPEED;
|
|
|
|
pad1.velocity.y = (-1) * PADDLE_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -235,26 +235,27 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
|
|
|
|
if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) {
|
|
|
|
if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) {
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
if (type.mode == M_CLIENT) {
|
|
|
|
client.sendAll(std::string("S"));
|
|
|
|
type.netsock->sendAll(std::string("S"));
|
|
|
|
pad1.velocity.y = 0;
|
|
|
|
pad1.velocity.y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Right paddle */
|
|
|
|
/* Right paddle */
|
|
|
|
if (IsKeyPressed(KEY_DOWN)) {
|
|
|
|
if (IsKeyPressed(KEY_DOWN)) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
server.sendAll(std::string("D"));
|
|
|
|
type.netsock->sendAll(std::string("D"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pad2.velocity.y = PADDLE_SPEED;
|
|
|
|
pad2.velocity.y = PADDLE_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IsKeyPressed(KEY_UP)) {
|
|
|
|
if (IsKeyPressed(KEY_UP)) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
server.sendAll(std::string("U"));
|
|
|
|
type.netsock->sendAll(std::string("U"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pad2.velocity.y = (-1) * PADDLE_SPEED;
|
|
|
|
pad2.velocity.y = (-1) * PADDLE_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) {
|
|
|
|
if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
if (type.mode == M_SERVER) {
|
|
|
|
server.sendAll(std::string("S"));
|
|
|
|
type.netsock->sendAll(std::string("S"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pad2.velocity.y = 0;
|
|
|
|
pad2.velocity.y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|