diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-12-15 17:16:06 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-12-15 17:16:06 +0100 |
| commit | f646069ca3baab4021e85de2ba19ce5f57e204fd (patch) | |
| tree | 69fc58df107d5c6d5af276a511db05104193bf01 | |
| parent | 66b82a20506b53ce8dc5dde8e6ea0979a22610f0 (diff) | |
bullet sync fix
| -rw-r--r-- | build/zombies.exe | bin | 1733882 -> 1734476 bytes | |||
| -rw-r--r-- | include/bullets.h | 1 | ||||
| -rw-r--r-- | src/bullets.c | 8 | ||||
| -rw-r--r-- | src/game.c | 32 |
4 files changed, 28 insertions, 13 deletions
diff --git a/build/zombies.exe b/build/zombies.exe Binary files differindex 47da708..e786e24 100644 --- a/build/zombies.exe +++ b/build/zombies.exe diff --git a/include/bullets.h b/include/bullets.h index be3f466..1828f38 100644 --- a/include/bullets.h +++ b/include/bullets.h @@ -20,6 +20,7 @@ typedef struct t_bullet { bullet bullets[500] = {0}; int max_bullets = 500; +void clear_bullets(); void shoot(platform_window* window, u32 id, float dirx, float diry); void draw_bullets(platform_window* window); diff --git a/src/bullets.c b/src/bullets.c index 4431eff..7397e9f 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -180,6 +180,12 @@ static bool check_if_bullet_collided_with_ground(bullet *b, platform_window* win return false; } +void clear_bullets() { + for (int i = 0; i < max_bullets; i++) { + bullets[i].active = false; + } +} + void update_bullets(platform_window* window) { for (int i = 0; i < max_bullets; i++) { bullet b = bullets[i]; @@ -229,7 +235,5 @@ void draw_bullets(platform_window* window) { float bullet_render_y_end = b.endy*info.tile_height - (b.position.z*info.px_raised_per_h); renderer->render_line(bullet_render_x, bullet_render_y, bullet_render_x_end, bullet_render_y_end, 5, rgb(255, 51, 51)); - - bullets[i].active = false; } }
\ No newline at end of file @@ -113,6 +113,8 @@ static void set_ping_for_player(protocol_generic_message* msg) { float update_timer = 0.0f; void update_server(platform_window* window) { + clear_bullets(); + for (int i = 0; i < messages_received_on_server.length; i++) { protocol_generic_message* msg = *(protocol_generic_message**)array_at(&messages_received_on_server, i); set_ping_for_player(msg); @@ -157,15 +159,10 @@ void update_server(platform_window* window) { update_players_server(); update_zombies_server(window); - if (update_timer > 0.0f) { - broadcast_to_clients(create_protocol_user_list()); - broadcast_to_clients(create_protocol_zombie_list()); - broadcast_to_clients(create_protocol_bullets_list()); - broadcast_to_clients(create_protocol_drop_list()); - update_timer = 0.0f; - } - - update_timer += update_delta; + broadcast_to_clients(create_protocol_user_list()); + broadcast_to_clients(create_protocol_zombie_list()); + broadcast_to_clients(create_protocol_bullets_list()); + broadcast_to_clients(create_protocol_drop_list()); } static void apply_user_list(protocol_user_list* msg_players) { @@ -184,7 +181,20 @@ static void apply_user_list(protocol_user_list* msg_players) { } } +static void load_bullets_into_existing_list(protocol_bullets_list* msg_bullets) { + for (int i = 0; i < max_bullets; i++) { + if (bullets[i].active) continue; + for (int x = 0; x < max_bullets; x++) { + if (!msg_bullets->bullets[x].active) continue; + bullets[i] = msg_bullets->bullets[x]; + msg_bullets->bullets[x].active = false; + } + } +} + void update_client(platform_window* window) { + clear_bullets(); + for (int i = 0; i < messages_received_on_client.length; i++) { protocol_generic_client_message* msg = *(protocol_generic_client_message**)array_at(&messages_received_on_client, i); @@ -194,7 +204,7 @@ void update_client(platform_window* window) { protocol_get_id_downstream* msg_id = (protocol_get_id_downstream*)msg; player_id = msg_id->id; global_state.network_state = CONNECTED; - log_infox("Id received: %d", player_id); + log_infox("Id received: %u", player_id); } break; case MESSAGE_USER_LIST: { @@ -212,7 +222,7 @@ void update_client(platform_window* window) { case MESSAGE_BULLET_LIST: { if (global_state.server) break; // bullets are simulated on server so dont overwrite data. protocol_bullets_list* msg_bullets = (protocol_bullets_list*)msg; - memcpy(bullets, msg_bullets->bullets, sizeof(bullets)); + load_bullets_into_existing_list(msg_bullets); } break; case MESSAGE_DROP_LIST: { |
