From 79efc8a8e9976bbdd41d4a47b58c71d3be9d4ff7 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Mon, 19 Dec 2022 16:42:15 +0100 Subject: zombie chunks --- src/asset_defs.c | 4 ++- src/bullets.c | 44 +++++++++++++++++++--------- src/game.c | 5 +++- src/map.c | 6 ++-- src/math_helper.c | 3 ++ src/players.c | 2 +- src/zombie_chunk.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/zombies.c | 4 +-- 8 files changed, 131 insertions(+), 22 deletions(-) create mode 100644 src/zombie_chunk.c (limited to 'src') diff --git a/src/asset_defs.c b/src/asset_defs.c index d122b6b..c978da2 100644 --- a/src/asset_defs.c +++ b/src/asset_defs.c @@ -5,6 +5,8 @@ void load_assets() { fnt_20 = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 20); img_icon_bullets = assets_load_image_from_file("data/imgs/bullets.png"); - img_icon_nova = assets_load_image_from_file("data/imgs/nova.png"); + + img_zombie_chunk_hand = assets_load_image_from_file("data/imgs/zombie_chunk_hand.png"); + img_zombie_chunk_foot = assets_load_image_from_file("data/imgs/zombie_chunk_foot.png"); } \ No newline at end of file diff --git a/src/bullets.c b/src/bullets.c index fea2a90..1449f93 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -28,7 +28,7 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) { map_info info = get_map_info(window); float bullet_range = 100.0f; - float hh = get_height_of_tile_under_coords(window, p->playerx, p->playery); + float hh = get_height_of_tile_under_coords(p->playerx, p->playery); dirx += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2)); diry += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2)); @@ -41,7 +41,7 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) { bullet b = bullets[i]; if (b.active) continue; - bullets[i] = (bullet){p->id, true, bulletx, bullety, hh + 0.5, bullet_end_point_x, bullet_end_point_y}; + bullets[i] = (bullet){p->id, true, bulletx, bullety, hh + 0.5,.endx = bullet_end_point_x,.endy = bullet_end_point_y, .damage = g.damage}; break; } } @@ -104,22 +104,25 @@ bool check_if_bullet_collided_with_object(bullet* b, platform_window* window) { return result; } -bool check_if_bullet_collided_with_zombie(bullet b, platform_window* window, bool kill_if_collided) { +bool check_if_bullet_collided_with_zombie(bullet* b, platform_window* window, player *p) { + if (b->damage == 0) return false; + map_info info = get_map_info(window); float size = get_bullet_size_in_tile(window); bool result = false; float dist_of_closest_intersect = __FLT_MAX__; int index_of_closest_zombie = -1; + vec2f intersect_point_of_closest_zombie; for (int i = 0; i < MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; - vec2f bstart = (vec2f){b.position.x, b.position.y}; - vec2f bend = (vec2f){b.endx, b.endy}; + vec2f bstart = (vec2f){b->position.x, b->position.y}; + vec2f bend = (vec2f){b->endx, b->endy}; - if (b.position.z <= o.position.z + o.size.z && b.position.z >= o.position.z) { + if (b->position.z <= o.position.z + o.size.z && b->position.z >= o.position.z) { vec2f intersect_point; box obj_box = get_box_of_square((vec3f){o.position.x, o.position.y, o.position.z}, o.size); bool this_zombie_collided = false; @@ -142,14 +145,24 @@ bool check_if_bullet_collided_with_zombie(bullet b, platform_window* window, boo if (this_zombie_collided) { result = true; + intersect_point_of_closest_zombie = intersect_point; } } } - if (kill_if_collided && result) { - spawn_drop(zombies[index_of_closest_zombie].position); - zombies[index_of_closest_zombie].alive = false; - return result; + if (result) { + b->endy = intersect_point_of_closest_zombie.y; + b->endx = intersect_point_of_closest_zombie.x; + zombies[index_of_closest_zombie].health -= b->damage; + b->damage = 0; + + int chunk_count = rand() % 4 + 1; + for (int c = 0; c < chunk_count; c++) spawn_zombie_chunk(get_center_of_square(zombies[index_of_closest_zombie].position, zombies[index_of_closest_zombie].size)); + if (zombies[index_of_closest_zombie].health <= 0) { + zombies[index_of_closest_zombie].alive = false; + spawn_drop(zombies[index_of_closest_zombie].position); + p->kills++; + } } return result; @@ -168,9 +181,9 @@ static bool check_if_bullet_collided_with_ground(bullet *b, platform_window* win float xtocheck = b->position.x + (dirx*i/4); float ytocheck = b->position.y + (diry*i/4); if (!is_in_bounds(xtocheck, ytocheck)) break; - tile tile = get_tile_under_coords(window, xtocheck, ytocheck); + tile tile = get_tile_under_coords(xtocheck, ytocheck); - float h = get_height_of_tile_under_coords(window, xtocheck, ytocheck); + float h = get_height_of_tile_under_coords(xtocheck, ytocheck); if (b->position.z <= h) { b->endx = xtocheck; b->endy = ytocheck; @@ -212,8 +225,11 @@ void update_bullets_server(platform_window* window) { b = bullets[i]; } - if (check_if_bullet_collided_with_zombie(b, window, true)) { - p->kills++; + if (check_if_bullet_collided_with_zombie(&b, window, p)) { + bullets[i].endy = b.endy; + bullets[i].endx = b.endx; + bullets[i].damage = b.damage; + b = bullets[i]; } } } diff --git a/src/game.c b/src/game.c index 94adacb..7cd0b0b 100644 --- a/src/game.c +++ b/src/game.c @@ -208,7 +208,7 @@ void update_server(platform_window* window) { logic_update_time = platform_get_time(TIME_FULL, TIME_NS) - logic_update_time; u64 server_tick = platform_get_time(TIME_FULL, TIME_NS) - handle_messages2; broadcast_stamp = platform_get_time(TIME_FULL, TIME_NS) - broadcast_stamp; - if ((logic_update_time/1000000.0f) > 1.0f) { + if ((logic_update_time/1000000.0f) > 10.0f) { log_infox("Server update took %.2fms:\n\tmessages: %.2fms\n\ttick: %.2fms\n\t\tbroadcast: %.2fms\n\t\tplayers: %.2fms\n\t\tzombies: %.2fms\n", (logic_update_time/1000000.0f), (handle_messages/1000000.0f), (server_tick/1000000.0f), (broadcast_stamp/1000000.0f), (broadcast_players/1000000.0f), (broadcast_zombies/1000000.0f)); @@ -307,6 +307,8 @@ void update_game(platform_window* window) { else { update_client(window); } + + update_zombie_chunks(); if (global_state.network_state == CONNECTED) { take_player_input(window); @@ -319,6 +321,7 @@ void update_game(platform_window* window) { draw_players(window); draw_spawners(window); draw_overlay(window); + draw_zombie_chunks(window); _global_camera.x = (int)_next_camera_pos.x; _global_camera.y = (int)_next_camera_pos.y; diff --git a/src/map.c b/src/map.c index 1d805c5..a5bc96a 100644 --- a/src/map.c +++ b/src/map.c @@ -107,12 +107,12 @@ void load_map_from_data() { } } -tile get_tile_under_coords(platform_window* window, float x, float y) { +tile get_tile_under_coords(float x, float y) { return map_loaded[(int)(y)][(int)(x)]; } -float get_height_of_tile_under_coords(platform_window* window, float tocheckx, float tochecky) { - tile tile_under_coords = get_tile_under_coords(window, tocheckx, tochecky); +float get_height_of_tile_under_coords(float tocheckx, float tochecky) { + tile tile_under_coords = get_tile_under_coords(tocheckx, tochecky); int ty = (int)(tochecky); int tx = (int)(tocheckx); diff --git a/src/math_helper.c b/src/math_helper.c index 436c81b..6bb89e2 100644 --- a/src/math_helper.c +++ b/src/math_helper.c @@ -37,6 +37,9 @@ bool pos2(float p1, float p2) { return (p1 >= 0.0f && p2 >= 0.0f) || abs(p1-p2) < 1.0f; } +vec3f get_center_of_square(vec3f position, vec3f size) { + return (vec3f){position.x + size.x/2, position.y + size.y/2, position.z + size.z/2}; +} bool lines_intersect(vec2f p1, vec2f q1, vec2f p2, vec2f q2) { diff --git a/src/players.c b/src/players.c index 9be2534..886c782 100644 --- a/src/players.c +++ b/src/players.c @@ -218,7 +218,7 @@ void draw_players(platform_window* window) { OBJECT_RENDER_DEPTH((int)(players[i].playery+size)); - float height = get_height_of_tile_under_coords(window, players[i].playerx, players[i].playery); + float height = get_height_of_tile_under_coords(players[i].playerx, players[i].playery); players[i].height = height; box box = get_render_box_of_square(window, (vec3f){players[i].playerx, players[i].playery, height}, (vec3f){size,size,0.8f}); diff --git a/src/zombie_chunk.c b/src/zombie_chunk.c new file mode 100644 index 0000000..f248474 --- /dev/null +++ b/src/zombie_chunk.c @@ -0,0 +1,85 @@ +#include "../include/zombie_chunk.h" +#include "../include/math_helper.h" + +static vec2f get_random_point_around_point(vec3f center, float distance) { + float angleInDegrees = (rand() % 360); + vec2f origin = (vec2f){center.x, center.y}; + float x = (float)(distance * cos(angleInDegrees * M_PI / 180.0f)) + center.x; + float y = (float)(distance * sin(angleInDegrees * M_PI / 180.0f)) + center.y; + + return (vec2f){x, y}; +} + +static image* get_chunk_image() { + #define AVAILABLE_ZOMBIE_CHUNK_COUNT 2 + image* available_zombie_chunks[AVAILABLE_ZOMBIE_CHUNK_COUNT] = { + img_zombie_chunk_hand, + img_zombie_chunk_foot, + }; + return available_zombie_chunks[rand() % AVAILABLE_ZOMBIE_CHUNK_COUNT]; +} + +void spawn_zombie_chunk(vec3f center) { + for (int i = 0; i < MAX_ZOMBIE_CHUNKS; i++) + { + if (zombie_chunks[i].active) continue; + + vec2f target = get_random_point_around_point(center, (float)rand()/(float)(RAND_MAX)); + vec2f dir = get_dir_of_line((vec2f){center.x, center.y}, (vec2f){target.x, target.y}); + float height = get_height_of_tile_under_coords(target.x, target.y); + + zombie_chunk chunk = {.active = true, .start_position = center, .direction = dir, + .position = center, .duration = 0.0f, .target_position = (vec3f){target.x, target.y, height}, + .img = get_chunk_image(), .rotation = (float)rand()/(float)(RAND_MAX)}; + zombie_chunks[i] = chunk; + return; + } +} + +void update_zombie_chunks() { + for (int i = 0; i < MAX_ZOMBIE_CHUNKS; i++) + { + if (!zombie_chunks[i].active) continue; + + zombie_chunks[i].duration += update_delta; + if (zombie_chunks[i].duration >= CHUNK_MAX_DURATION) { + zombie_chunks[i].active = false; + continue; + } + if (zombie_chunks[i].duration >= CHUNK_DURATION_OF_DROP) { + continue; + } + + zombie_chunks[i].rotation += zombie_chunks[i].direction.x < 0.0f ? update_delta : -update_delta; + + float x = zombie_chunks[i].start_position.x + ((zombie_chunks[i].start_position.x - zombie_chunks[i].target_position.x) / CHUNK_DURATION_OF_DROP) * zombie_chunks[i].duration; + float y = zombie_chunks[i].start_position.y + ((zombie_chunks[i].start_position.y - zombie_chunks[i].target_position.y) / CHUNK_DURATION_OF_DROP) * zombie_chunks[i].duration; + float z = zombie_chunks[i].start_position.z + ((zombie_chunks[i].start_position.z - zombie_chunks[i].target_position.z) / CHUNK_DURATION_OF_DROP) * zombie_chunks[i].duration; + + zombie_chunks[i].position.x = x; + zombie_chunks[i].position.y = y; + zombie_chunks[i].position.z = z; + } +} + +void draw_zombie_chunks(platform_window* window) { + for (int i = 0; i < MAX_ZOMBIE_CHUNKS; i++) + { + if (!zombie_chunks[i].active) continue; + + DROP_RENDER_DEPTH((int)(zombie_chunks[i].position.y)); + + int alpha = 255; + if (zombie_chunks[i].duration >= CHUNK_MAX_DURATION - CHUNK_FADE_TIME) { + alpha = 255 - ((zombie_chunks[i].duration - (CHUNK_MAX_DURATION - CHUNK_FADE_TIME)) / CHUNK_FADE_TIME)*255; + } + if (alpha < 0) alpha = 0; + + box box = get_render_box_of_square(window, zombie_chunks[i].position, (vec3f){0.6f, 0.6f, 0.6f}); + //render_box_with_outline(box, rgba(200, 50, 50, alpha)); + + renderer->render_set_rotation(zombie_chunks[i].rotation); + renderer->render_image_tint(zombie_chunks[i].img, box.tl_d.x, box.tl_d.y, box.tr_d.x - box.tl_d.x, box.br_d.y - box.tr_d.y, rgba(255,255,255,alpha)); + renderer->render_set_rotation(0.0f); + } +} \ No newline at end of file diff --git a/src/zombies.c b/src/zombies.c index d44c46d..a5a9f38 100644 --- a/src/zombies.c +++ b/src/zombies.c @@ -143,7 +143,7 @@ void update_zombies_client(platform_window* window) { if (!o.alive) continue; if (o.next2tiles[0].x == -1 || o.next2tiles[0].y == -1) continue; // ran out of stored path. - float height = get_height_of_tile_under_coords(window, zombies[i].position.x, zombies[i].position.y); + float height = get_height_of_tile_under_coords(zombies[i].position.x, zombies[i].position.y); vec2f dir = get_direction_to_tile(o, o.next2tiles[0]); zombies[i].position.x += dir.x*speed; @@ -208,7 +208,7 @@ void update_zombies_server(platform_window* window) { } vec2f dir = get_direction_to_next_tile(o); - float height = get_height_of_tile_under_coords(window, zombies[i].position.x, zombies[i].position.y); + float height = get_height_of_tile_under_coords(zombies[i].position.x, zombies[i].position.y); zombies[i].position.x += dir.x*speed; zombies[i].position.y += dir.y*speed; zombies[i].position.z = height; -- cgit v1.2.3-70-g09d2