From 2b9b3e78d9d448e4dfb4eaaa87b5f290ebee9cff Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Thu, 2 Nov 2023 18:42:02 +0100 Subject: zombie chunks on server side --- build/zombies.exe | Bin 1994567 -> 1995181 bytes include/protocol.h | 9 +++++++++ include/zombie_chunk.h | 10 +++++++++- src/game.c | 6 ++++++ src/protocol.c | 8 ++++++++ src/zombie_chunk.c | 23 ++++++++++++++++------- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/build/zombies.exe b/build/zombies.exe index 07db52c..646a03c 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/include/protocol.h b/include/protocol.h index e0c47c6..732ea86 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -14,6 +14,7 @@ typedef enum t_network_message_type MESSAGE_USER_MOVED, MESSAGE_USER_LOOK, MESSAGE_USER_THROW, + MESSAGE_ZOMBIE_CHUNK_LIST, MESSAGE_ZOMBIE_LIST, MESSAGE_USER_SHOOT, MESSAGE_BULLET_LIST, @@ -66,6 +67,13 @@ typedef struct t_protocol_zombie_list zombie zombies[SERVER_MAX_ZOMBIES]; } protocol_zombie_list; +#include "zombie_chunk.h" +typedef struct t_protocol_zombie_chunk_list +{ + network_message_type type; + zombie_chunk zombie_chunks[MAX_ZOMBIE_CHUNKS]; +} protocol_zombie_chunk_list; + #include "drops.h" typedef struct t_protocol_drop_list { @@ -150,6 +158,7 @@ network_message create_protocol_user_look(u32 id, float gunx, float guny); network_message create_protocol_user_shoot(u32 id, float dirx, float diry); network_message create_protocol_user_throw(u32 id, float dirx, float diry, throwable_type type); network_message create_protocol_zombie_list(); +network_message create_protocol_zombie_chunk_list(); network_message create_protocol_bullets_list(); network_message create_protocol_drop_list(); network_message create_protocol_throwables_list(); diff --git a/include/zombie_chunk.h b/include/zombie_chunk.h index f0ad5b1..d774026 100644 --- a/include/zombie_chunk.h +++ b/include/zombie_chunk.h @@ -5,6 +5,13 @@ #include "objects.h" +typedef enum t_zombie_chunk_type +{ + CHUNK_FOOT, + CHUNK_HAND, + CHUNK_SPLATTER, +} zombie_chunk_type; + typedef struct t_zombie_chunk { bool active; vec3f start_position; @@ -12,7 +19,8 @@ typedef struct t_zombie_chunk { vec3f target_position; float duration; vec2f direction; - image* img; + zombie_chunk_type type; + int random_chunk; float rotation; float rotation_speed; } zombie_chunk; diff --git a/src/game.c b/src/game.c index df65282..d32b023 100644 --- a/src/game.c +++ b/src/game.c @@ -231,6 +231,7 @@ void update_server(platform_window* window) { broadcast_to_clients(create_protocol_bullets_list()); broadcast_to_clients(create_protocol_drop_list()); broadcast_to_clients(create_protocol_throwables_list()); + broadcast_to_clients(create_protocol_zombie_chunk_list()); // play sounds locally and send them to clients. play_sounds_in_queue(); @@ -292,6 +293,11 @@ void update_client(platform_window* window) { memcpy(zombies, msg_zombies->zombies, sizeof(zombies)); } break; + case MESSAGE_ZOMBIE_CHUNK_LIST: { + protocol_zombie_chunk_list* msg_zombies = (protocol_zombie_chunk_list*)msg; + memcpy(zombie_chunks, msg_zombies->zombie_chunks, sizeof(zombie_chunks)); + } break; + case MESSAGE_THROWABLES_LIST: { protocol_throwables_list* msg_throwables = (protocol_throwables_list*)msg; memcpy(throwables, msg_throwables->throwables, sizeof(throwables)); diff --git a/src/protocol.c b/src/protocol.c index a33f421..d8506b7 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -51,6 +51,14 @@ network_message create_protocol_zombie_list() return network_create_message((u8*)buf, sizeof(protocol_zombie_list), MAX_NETWORK_BUFFER_SIZE); } +network_message create_protocol_zombie_chunk_list() +{ + protocol_zombie_chunk_list *buf = alloc_network_message(protocol_zombie_chunk_list); + buf->type = MESSAGE_ZOMBIE_CHUNK_LIST; + memcpy(buf->zombie_chunks, zombie_chunks, sizeof(zombie_chunks)); + return network_create_message((u8*)buf, sizeof(protocol_zombie_chunk_list), MAX_NETWORK_BUFFER_SIZE); +} + network_message create_protocol_bullets_list() { protocol_bullets_list *buf = alloc_network_message(protocol_bullets_list); diff --git a/src/zombie_chunk.c b/src/zombie_chunk.c index 9a10e00..02e7027 100644 --- a/src/zombie_chunk.c +++ b/src/zombie_chunk.c @@ -10,11 +10,20 @@ static vec2f get_random_point_around_point(vec3f center, float distance) { return (vec2f){x, y}; } -static image* get_chunk_image() { +static image* get_chunk_image_from_type(zombie_chunk_type type) { + switch(type) { + default: + case CHUNK_HAND: return img_zombie_chunk_hand; + case CHUNK_FOOT: return img_zombie_chunk_foot; + case CHUNK_SPLATTER: return img_zombie_chunk_blood; + } +} + +static zombie_chunk_type get_random_chunk_type() { #define AVAILABLE_ZOMBIE_CHUNK_COUNT 2 - image* available_zombie_chunks[AVAILABLE_ZOMBIE_CHUNK_COUNT] = { - img_zombie_chunk_hand, - img_zombie_chunk_foot, + zombie_chunk_type available_zombie_chunks[AVAILABLE_ZOMBIE_CHUNK_COUNT] = { + CHUNK_HAND, + CHUNK_FOOT, }; return available_zombie_chunks[rand() % AVAILABLE_ZOMBIE_CHUNK_COUNT]; } @@ -30,7 +39,7 @@ void spawn_zombie_splatter(vec3f center) { zombie_chunk chunk = {.active = true, .start_position = center, .direction = dir, .position = center, .duration = 0.0f, .target_position = center, - .img = img_zombie_chunk_blood, .rotation = (float)rand()/(float)(RAND_MAX), .rotation_speed = 0.0f}; + .type = CHUNK_SPLATTER, .rotation = (float)rand()/(float)(RAND_MAX), .rotation_speed = 0.0f}; zombie_chunks[i] = chunk; return; } @@ -47,7 +56,7 @@ void spawn_zombie_chunk(vec3f center) { 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), .rotation_speed = dir.x < 0.0f ? update_delta : -update_delta}; + .type = get_random_chunk_type(), .rotation = (float)rand()/(float)(RAND_MAX), .rotation_speed = dir.x < 0.0f ? update_delta : -update_delta}; zombie_chunks[i] = chunk; return; } @@ -97,7 +106,7 @@ void draw_zombie_chunks(platform_window* window) { //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_image_tint(get_chunk_image_from_type(zombie_chunks[i].type), 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 -- cgit v1.2.3-70-g09d2