|
@@ -16,6 +16,7 @@
|
|
|
#include "MultiPlayer.h"
|
|
#include "MultiPlayer.h"
|
|
|
#include "game.h"
|
|
#include "game.h"
|
|
|
#include "game_common.h"
|
|
#include "game_common.h"
|
|
|
|
|
+#include "GameState_net.h"
|
|
|
|
|
|
|
|
MultiPlayer MultiPlayer_alloc(
|
|
MultiPlayer MultiPlayer_alloc(
|
|
|
const unsigned int screen_w,
|
|
const unsigned int screen_w,
|
|
@@ -28,7 +29,7 @@ MultiPlayer MultiPlayer_alloc(
|
|
|
mp.screen_w = screen_w;
|
|
mp.screen_w = screen_w;
|
|
|
mp.screen_h = screen_h;
|
|
mp.screen_h = screen_h;
|
|
|
|
|
|
|
|
- mp.state = GameState_new(game_default_params, false);
|
|
|
|
|
|
|
+ // mp.state = GameState_new(game_default_params, false);
|
|
|
mp.shapes = GameShapes_alloc(screen_w, screen_h, game_default_params);
|
|
mp.shapes = GameShapes_alloc(screen_w, screen_h, game_default_params);
|
|
|
|
|
|
|
|
struct protoent *proto = getprotobyname("tcp");
|
|
struct protoent *proto = getprotobyname("tcp");
|
|
@@ -80,7 +81,38 @@ void MultiPlayer_free(MultiPlayer *const mp) {
|
|
|
GameShapes_free(&mp->shapes);
|
|
GameShapes_free(&mp->shapes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static void handle_paddle_pos_msg(MultiPlayer *const mp) {
|
|
|
|
|
+ PaddlePosMsg msg;
|
|
|
|
|
+ recv_msg(mp->conn_s, NET_MSG_PADDLE_POS, (uint8_t *) &msg);
|
|
|
|
|
+ vec2 pos = { msg.pos_x, msg.pos_y };
|
|
|
|
|
+ const bool player1 = (bool) msg.player1;
|
|
|
|
|
+ printf("Got msg from server: player1: %d, pos: (%.2lf, %.2lf)\n", player1, pos[0], pos[1]);
|
|
|
|
|
+ glmc_vec2_copy(pos, mp->state.paddle_l.pos);
|
|
|
|
|
+ mp->state.paddle_l.pos[0] = pos[0];
|
|
|
|
|
+ mp->state.paddle_l.pos[1] = pos[1];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void handle_game_state_msg(MultiPlayer *const mp) {
|
|
|
|
|
+ GameStateMsg msg;
|
|
|
|
|
+ recv_msg(mp->conn_s, NET_MSG_GAME_STATE, (uint8_t *) &msg);
|
|
|
|
|
+ GameStateMsg_to_GameState(&msg, &mp->state);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
|
|
void MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
|
|
|
|
|
+ NetMsgType msg_type;
|
|
|
|
|
+ if (recv_msg_type(mp->conn_s, &msg_type)) {
|
|
|
|
|
+ switch (msg_type) {
|
|
|
|
|
+ case NET_MSG_PADDLE_CHANGE_DIR:
|
|
|
|
|
+ break;
|
|
|
|
|
+ case NET_MSG_PADDLE_POS:
|
|
|
|
|
+ handle_paddle_pos_msg(mp);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case NET_MSG_GAME_STATE:
|
|
|
|
|
+ handle_game_state_msg(mp);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
SEDirection new_dir = SE_NONE;
|
|
SEDirection new_dir = SE_NONE;
|
|
|
bool dir_changed = false;
|
|
bool dir_changed = false;
|
|
|
const SEDirection current_dir = GameState_paddle_l_dir_get(&mp->state);
|
|
const SEDirection current_dir = GameState_paddle_l_dir_get(&mp->state);
|
|
@@ -109,18 +141,6 @@ void MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
|
|
|
send_msg(mp->conn_s, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg);
|
|
send_msg(mp->conn_s, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- NetMsgType msg_type;
|
|
|
|
|
- if (recv_msg_type(mp->conn_s, &msg_type)) {
|
|
|
|
|
- PaddlePosMsg msg;
|
|
|
|
|
- recv_msg(mp->conn_s, msg_type, (uint8_t *) &msg);
|
|
|
|
|
- vec2 pos = { msg.pos_x, msg.pos_y };
|
|
|
|
|
- const bool player1 = (bool) msg.player1;
|
|
|
|
|
- printf("Got msg from server: player1: %d, pos: (%.2lf, %.2lf)\n", player1, pos[0], pos[1]);
|
|
|
|
|
- glmc_vec2_copy(pos, mp->state.paddle_l.pos);
|
|
|
|
|
- mp->state.paddle_l.pos[0] = pos[0];
|
|
|
|
|
- mp->state.paddle_l.pos[1] = pos[1];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
GameState_update(&mp->state, delta);
|
|
GameState_update(&mp->state, delta);
|
|
|
GameShapes_update(&mp->shapes, &mp->state, mp->screen_w, mp->screen_h);
|
|
GameShapes_update(&mp->shapes, &mp->state, mp->screen_w, mp->screen_h);
|
|
|
GameShapes_render(&mp->shapes);
|
|
GameShapes_render(&mp->shapes);
|