From 4e378fe6a3aacb398d88865b833b12e3dbd194a3 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 28 Oct 2023 11:12:22 +0200 Subject: player reloading delay --- build/data/sounds/shoot_mp5.wav | Bin 0 -> 12512 bytes build/zombies.exe | Bin 1975680 -> 1976258 bytes data/sounds/shoot_mp5.wav | Bin 0 -> 12512 bytes include/asset_defs.h | 1 + include/audio.h | 1 + include/guns.h | 7 ++++--- include/players.h | 7 +++++++ src/asset_defs.c | 1 + src/bullets.c | 13 +++++++++---- src/players.c | 12 ++++++++++++ 10 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 build/data/sounds/shoot_mp5.wav create mode 100644 data/sounds/shoot_mp5.wav diff --git a/build/data/sounds/shoot_mp5.wav b/build/data/sounds/shoot_mp5.wav new file mode 100644 index 0000000..808ddeb Binary files /dev/null and b/build/data/sounds/shoot_mp5.wav differ diff --git a/build/zombies.exe b/build/zombies.exe index 13c301b..8f92f23 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/data/sounds/shoot_mp5.wav b/data/sounds/shoot_mp5.wav new file mode 100644 index 0000000..808ddeb Binary files /dev/null and b/data/sounds/shoot_mp5.wav differ diff --git a/include/asset_defs.h b/include/asset_defs.h index 45df7b5..623c54d 100644 --- a/include/asset_defs.h +++ b/include/asset_defs.h @@ -36,6 +36,7 @@ image* img_tile_grass1; // Sounds Mix_Chunk* wav_throwable_bounce; +Mix_Chunk* wav_shoot_mp5; void load_assets(); diff --git a/include/audio.h b/include/audio.h index 98cb356..850e74e 100644 --- a/include/audio.h +++ b/include/audio.h @@ -4,6 +4,7 @@ #include #define CHANNEL_THROWABLES 0 +#define CHANNEL_SHOOTING 0 void play_sound(int channel, Mix_Chunk* wav); void play_positioned_sound(int channel, Mix_Chunk* wav, vec3f pos, float max_audible_dist); diff --git a/include/guns.h b/include/guns.h index 2c22c00..2ea0d1e 100644 --- a/include/guns.h +++ b/include/guns.h @@ -18,12 +18,13 @@ typedef struct t_gun { int bullets_per_shot; float shots_per_second; int damage; + float reload_time; } gun; gun guns[GUN_ALL] = { - {GUN_DESERTEAGLE, "Desert Eagle", 8, 64, 0.0f, 1, 4.0f, 350}, - {GUN_MP5, "MP5", 30, 120, 0.1f, 1, 10.0f, 150}, - {GUN_NOVA, "Nova", 12, 80, 0.2f, 3, 1.2f, 600}, + {GUN_DESERTEAGLE, "Desert Eagle", 8, 64, 0.0f, 1, 4.0f, 350, 1.0f}, + {GUN_MP5, "MP5", 30, 120, 0.1f, 1, 10.0f, 150, 1.0f}, + {GUN_NOVA, "Nova", 12, 80, 0.2f, 3, 1.2f, 600, 1.0f}, }; image* get_image_of_gun(gun_type type); diff --git a/include/players.h b/include/players.h index c4652f2..90fdc84 100644 --- a/include/players.h +++ b/include/players.h @@ -12,10 +12,17 @@ #define MAX_PLAYERS 10 +typedef enum t_player_interact_state { + INTERACT_IDLE, + INTERACT_RELOADING, +} player_interact_state; + typedef struct t_player { u32 id; bool active; float sec_since_last_shot; + player_interact_state interact_state; + float sec_since_interact_state_change; float playerx; float playery; float gunx; diff --git a/src/asset_defs.c b/src/asset_defs.c index 2c51369..bc3919c 100644 --- a/src/asset_defs.c +++ b/src/asset_defs.c @@ -34,4 +34,5 @@ void load_assets() { // sounds wav_throwable_bounce = Mix_LoadWAV("data/sounds/throwable_bounce.wav"); + wav_shoot_mp5 = Mix_LoadWAV("data/sounds/shoot_mp5.wav"); } \ No newline at end of file diff --git a/src/bullets.c b/src/bullets.c index dcdc02a..47171bb 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -5,6 +5,10 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) { if (!p) { log_info("User with unknown id shot"); } + if (p->interact_state != INTERACT_IDLE) { + return; + } + gun g = get_gun_by_type(p->guntype); float time_between_bullets = 1.0f/g.shots_per_second; @@ -17,12 +21,13 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) { if (bullets_to_shoot > p->ammo_in_mag) bullets_to_shoot = p->ammo_in_mag; p->ammo_in_mag -= bullets_to_shoot; if (p->ammo_in_mag == 0) { - int amount_to_reload = g.magazine_size; - if (amount_to_reload > p->total_ammo) amount_to_reload = p->total_ammo; - p->total_ammo -= amount_to_reload; - p->ammo_in_mag = amount_to_reload; + p->interact_state = INTERACT_RELOADING; + p->sec_since_interact_state_change = 0; + return; } + play_positioned_sound(CHANNEL_SHOOTING, wav_shoot_mp5, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height}, 20); + for (int i = 0; i < bullets_to_shoot; i++) { map_info info = get_map_info(window); diff --git a/src/players.c b/src/players.c index 4ea763d..03ee4e0 100644 --- a/src/players.c +++ b/src/players.c @@ -215,6 +215,18 @@ void update_players_server() { for (int i = 0; i < MAX_PLAYERS; i++) { if (!players[i].active) continue; players[i].sec_since_last_shot += SERVER_TICK_RATE; + players[i].sec_since_interact_state_change += SERVER_TICK_RATE; + + gun g = get_gun_by_type(players[i].guntype); + if (players[i].interact_state == INTERACT_RELOADING && players[i].sec_since_interact_state_change >= g.reload_time) { + int amount_to_reload = g.magazine_size; + if (amount_to_reload > players[i].total_ammo) amount_to_reload = players[i].total_ammo; + players[i].total_ammo -= amount_to_reload; + players[i].ammo_in_mag = amount_to_reload; + + players[i].interact_state = INTERACT_IDLE; + } + update_sprite(&players[i].sprite); } } -- cgit v1.2.3-70-g09d2