diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2024-01-07 12:24:38 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2024-01-07 12:24:38 +0100 |
| commit | 33f39cd8ef08db0007c9690992bad99edbaa1795 (patch) | |
| tree | e1e48263aaa124ca826b4c5984e3511e56bde70f | |
| parent | 1f3e6604cf2df6ae11944f99f939b8b697ebd4d9 (diff) | |
zombie hit player
| -rw-r--r-- | data/sounds/player_hurt1.wav | bin | 0 -> 62798 bytes | |||
| -rw-r--r-- | data/sounds/player_hurt2.wav | bin | 0 -> 112462 bytes | |||
| -rw-r--r-- | data/sounds/player_hurt3.wav | bin | 0 -> 81742 bytes | |||
| -rw-r--r-- | include/asset_defs.h | 3 | ||||
| -rw-r--r-- | include/audio.h | 1 | ||||
| -rw-r--r-- | include/players.h | 3 | ||||
| -rw-r--r-- | include/zombies.h | 8 | ||||
| -rw-r--r-- | src/asset_defs.c | 6 | ||||
| -rw-r--r-- | src/audio.c | 6 | ||||
| -rw-r--r-- | src/game.c | 1 | ||||
| -rw-r--r-- | src/players.c | 12 | ||||
| -rw-r--r-- | src/zombies.c | 56 |
12 files changed, 90 insertions, 6 deletions
diff --git a/data/sounds/player_hurt1.wav b/data/sounds/player_hurt1.wav Binary files differnew file mode 100644 index 0000000..e1e31a9 --- /dev/null +++ b/data/sounds/player_hurt1.wav diff --git a/data/sounds/player_hurt2.wav b/data/sounds/player_hurt2.wav Binary files differnew file mode 100644 index 0000000..b030642 --- /dev/null +++ b/data/sounds/player_hurt2.wav diff --git a/data/sounds/player_hurt3.wav b/data/sounds/player_hurt3.wav Binary files differnew file mode 100644 index 0000000..48ea7b0 --- /dev/null +++ b/data/sounds/player_hurt3.wav diff --git a/include/asset_defs.h b/include/asset_defs.h index 467837e..1f652d4 100644 --- a/include/asset_defs.h +++ b/include/asset_defs.h @@ -103,6 +103,9 @@ Mix_Chunk* wav_step_enraged; #define NUM_SCREECHES 11 Mix_Chunk* wav_screech[NUM_SCREECHES]; +#define NUM_PLAYER_HURT 3 +Mix_Chunk* wav_player_hurt[NUM_PLAYER_HURT]; + Mix_Music* music_inside1; void load_assets(); diff --git a/include/audio.h b/include/audio.h index b32148f..6414f5a 100644 --- a/include/audio.h +++ b/include/audio.h @@ -27,6 +27,7 @@ typedef enum t_audio_event_type { EVENT_FOOTSTEP, EVENT_ZOMBIESCREECH, EVENT_ZOMBIEROAR, + EVENT_PLAYERHURT, } audio_event_type; typedef struct t_audio_event { diff --git a/include/players.h b/include/players.h index 7607788..b9b5710 100644 --- a/include/players.h +++ b/include/players.h @@ -44,6 +44,8 @@ typedef struct t_point_animation typedef struct t_player { u32 id; bool active; + s32 health; + s32 max_health; float sec_since_last_shot; player_interact_state interact_state; float sec_since_interact_state_change; @@ -92,6 +94,7 @@ object check_if_player_collided_with_object(player p); float get_player_size(platform_window* window); void move_user(platform_window* window, u32 id, protocol_move_type move, float delta); void update_players_server(); +void hurt_player(u32 id, u32 damage); void spawn_player(u32 id, network_client client); bool player_has_old_session(u32 id); void rejoin_player(u32 id, network_client client); diff --git a/include/zombies.h b/include/zombies.h index b7c7889..ec80135 100644 --- a/include/zombies.h +++ b/include/zombies.h @@ -16,13 +16,17 @@ typedef enum t_zombie_type { typedef struct t_zombie { bool alive; - float health; - float max_health; + s32 health; + s32 max_health; vec3f position; vec3f size; array path; float speed; + float attack_range; + float attack_rate; + u32 attack_damage; float sec_since_last_step; + float sec_since_last_attack; array next_path; float time_since_last_path; pathfinding_request request; diff --git a/src/asset_defs.c b/src/asset_defs.c index 33e6d37..5dc18f8 100644 --- a/src/asset_defs.c +++ b/src/asset_defs.c @@ -104,6 +104,12 @@ void load_assets() { wav_screech[i-1] = Mix_LoadWAV(path); } + for (int i = 1; i <= NUM_PLAYER_HURT; i++) { + char path[100]; + sprintf(path, "data/sounds/player_hurt%d.wav", i); + wav_player_hurt[i-1] = Mix_LoadWAV(path); + } + // music music_inside1 = Mix_LoadMUS("data/sounds/music_inside1.mp3"); diff --git a/src/audio.c b/src/audio.c index 1f88d17..4059854 100644 --- a/src/audio.c +++ b/src/audio.c @@ -42,6 +42,7 @@ static int get_channel_from_audio_event_type(audio_event_type event) { return i; } } + log_info("Ran out of audio channels"); return 0; } @@ -120,6 +121,11 @@ static Mix_Chunk* get_sample_from_audio_event(audio_event event, u32 playerid) { int random_screech_index = rand() % NUM_SCREECHES; return wav_screech[random_screech_index]; } + case EVENT_PLAYERHURT: { + int random_hurt_index = rand() % NUM_PLAYER_HURT; + return wav_player_hurt[random_hurt_index]; + return wav_error; + } default: return wav_error; } @@ -40,6 +40,7 @@ void connect_to_server(char* ip, char* port) { global_state.client->on_message = client_on_message_received; if (global_state.server) { + player_id = 127001; spawn_player(player_id, (network_client){0, false, 0, "Host"}); global_state.network_state = CONNECTED; } diff --git a/src/players.c b/src/players.c index b2ebfae..6ea5205 100644 --- a/src/players.c +++ b/src/players.c @@ -61,6 +61,8 @@ void spawn_player(u32 id, network_client client) { players[i].client = client; players[i].sprite = create_sprite(img_gunner_blue_run, 6, 48, 48, 0.1f); players[i].sprite.zoom = 1.1f; + players[i].health = 500; + players[i].max_health = 500; players[i].gun_sprite = create_sprite(img_gun_mp5, 4, 256, 256, 0.0f); players[i].direction = DIRECTION_DOWN; @@ -353,6 +355,16 @@ void update_players_client() { } } +void hurt_player(u32 id, u32 damage) { + player* p = get_player_by_id(id); + if (p == 0) { + return; + } + + p->health -= damage; + add_audio_event_to_queue(EVENT_PLAYERHURT, p->id, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height}); +} + void update_players_server() { for (int i = 0; i < MAX_PLAYERS; i++) { if (!players[i].active) continue; diff --git a/src/zombies.c b/src/zombies.c index 8178f4f..8ebf1b1 100644 --- a/src/zombies.c +++ b/src/zombies.c @@ -1,6 +1,30 @@ #include "../include/zombies.h" -static player get_closest_player_to_tile(int x, int y) { +static player get_closest_player_to_tile_x(float x, float y, float* buf_length) { + float best_length = 99999; + int best_index = -1; + + for (int i = 0; i < MAX_PLAYERS; i++) { + if (!players[i].active) continue; + float dirx = (players[i].playerx - x); + float diry = (players[i].playery - y); + double length = sqrt(dirx * dirx + diry * diry); + + if (length < best_length) { + best_length = length; + best_index = i; + } + } + *buf_length = best_length; + if (best_index == -1) { + return (player){-1}; + } + else { + return players[best_index]; + } +} + +static player get_closest_player_to_tile(float x, float y) { float best_length = 99999; int best_index = -1; @@ -85,6 +109,9 @@ static void set_enraged_zombie_stats(zombie *zombie) { zombie->sprite_run = create_sprite(img_alien_run, 6, 32, 32, 0.1f); zombie->sprite_run.zoom = 1.20f; zombie->speed = 5.0f; + zombie->attack_range = 0.8f; + zombie->attack_rate = 0.7f; + zombie->attack_damage = 250; add_zombie_audio_event_to_queue(EVENT_ZOMBIEROAR, ZOMBIE_TYPE_ENRAGED, zombie->position); } @@ -99,6 +126,9 @@ static void set_normal_zombie_stats(zombie *zombie) { zombie->sprite_run = create_sprite(img_alien_run, 6, 32, 32, 0.1f); zombie->sprite_run.zoom = 1.20f; zombie->speed = 4.0f; + zombie->attack_range = 0.8f; + zombie->attack_rate = 0.7f; + zombie->attack_damage = 100; } int normal_zombie_spawn_counter = 0; @@ -115,6 +145,7 @@ void spawn_zombie(int x, int y) { zombies[i].type = (normal_zombie_spawn_counter % 5 == 0) ? ZOMBIE_TYPE_ENRAGED : ZOMBIE_TYPE_NORMAL; zombies[i].position = (vec3f){x,y, 0}; zombies[i].sec_since_last_step = 0.0f; + zombies[i].sec_since_last_attack = 0.0f; switch(zombies[i].type) { case ZOMBIE_TYPE_NORMAL: set_normal_zombie_stats(&zombies[i]); break; case ZOMBIE_TYPE_ENRAGED: set_enraged_zombie_stats(&zombies[i]); break; @@ -274,6 +305,19 @@ static vec2f get_random_point_around_player(player p, zombie o) { return (vec2f){x, y}; } +static void update_zombie_attacks_server(zombie *zombie) { + if (zombie->sec_since_last_attack >= zombie->attack_rate) { + //add_zombie_audio_event_to_queue(EVENT_FOOTSTEP, o.type, o.position); + + float dist; + player p = get_closest_player_to_tile_x(zombie->position.x, zombie->position.y, &dist); + if (p.id != -1 && dist <= zombie->attack_range) { + zombie->sec_since_last_attack = 0.0f; + hurt_player(p.id, zombie->attack_damage); + } + } +} + void update_zombies_server(platform_window* window) { for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; @@ -286,11 +330,12 @@ void update_zombies_server(platform_window* window) { zombies[i].sec_since_last_step = 0.0f; } zombies[i].sec_since_last_step += SERVER_TICK_RATE; + zombies[i].sec_since_last_attack += SERVER_TICK_RATE; // Update pathfinding zombies[i].time_since_last_path += SERVER_TICK_RATE; if (zombies[i].time_since_last_path > SERVER_PATHFINDING_INTERVAL) { - player closest_player = get_closest_player_to_tile((int)o.position.x, (int)o.position.y); + player closest_player = get_closest_player_to_tile(o.position.x, o.position.y); vec2f target_tile = (vec2f){closest_player.playerx, closest_player.playery+(get_player_size_in_tile()/2)}; array_clear(zombies[i].request.to_fill); @@ -306,7 +351,7 @@ void update_zombies_server(platform_window* window) { array_destroy(&zombies[i].path); zombies[i].path = array_copy(zombies[i].request.to_fill); - player closest_player = get_closest_player_to_tile((int)o.position.x, (int)o.position.y); + player closest_player = get_closest_player_to_tile(o.position.x, o.position.y); vec2f final_pos = get_random_point_around_player(closest_player, zombies[i]); array_push_at(&zombies[i].path, (u8*)&final_pos, 0); @@ -341,6 +386,9 @@ void update_zombies_server(platform_window* window) { } } } + + // Attacking + update_zombie_attacks_server(&zombies[i]); } } @@ -381,7 +429,7 @@ void draw_zombies(platform_window* window) { if (o.health < o.max_health) { int bar_h = zombie_size/8; int bar_w = zombie_size/2; - float percentage = o.health/o.max_health; + float percentage = o.health/(float)o.max_health; renderer->render_rectangle(zombie_pos.x + (zombie_size/2) - (bar_w/2), zombie_pos.y - bar_h, bar_w, bar_h, rgb(0,0,0)); renderer->render_rectangle(zombie_pos.x + (zombie_size/2) - (bar_w/2), zombie_pos.y - bar_h, bar_w*percentage, bar_h, rgb(100,0,0)); } |
