summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/asset_defs.c6
-rw-r--r--src/audio.c6
-rw-r--r--src/game.c1
-rw-r--r--src/players.c12
-rw-r--r--src/zombies.c56
5 files changed, 77 insertions, 4 deletions
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;
}
diff --git a/src/game.c b/src/game.c
index e5a34be..60540a8 100644
--- a/src/game.c
+++ b/src/game.c
@@ -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));
}