From c2412ac4b1f73fefb0327a688638d2a6e9287bae Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 9 Apr 2026 09:35:52 +0300 Subject: [PATCH 1/5] various: implement fog of war; make enemy AI slightly more intelligent Signed-off-by: NotAShelf Change-Id: I3e22dbc5e10690871255980c52a24c226a6a6964 --- src/common.h | 2 ++ src/enemy.c | 47 +++++++++++++++++++++++++++-------- src/main.c | 11 +++++++-- src/map.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/map.h | 6 +++++ src/render.c | 31 ++++++++++++++++++------ src/render.h | 4 +-- src/settings.h | 5 ++++ 8 files changed, 151 insertions(+), 21 deletions(-) diff --git a/src/common.h b/src/common.h index 61ab044..2f4021f 100644 --- a/src/common.h +++ b/src/common.h @@ -30,6 +30,8 @@ typedef struct { TileType tiles[MAP_HEIGHT][MAP_WIDTH]; Room rooms[MAX_ROOMS]; int room_count; + unsigned char visible[MAP_HEIGHT][MAP_WIDTH]; + unsigned char remembered[MAP_HEIGHT][MAP_WIDTH]; } Map; // Dungeon diff --git a/src/enemy.c b/src/enemy.c index 9f72670..764d2d9 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -3,6 +3,7 @@ #include "common.h" #include "map.h" #include "rng.h" +#include "settings.h" #include // Forward declaration @@ -134,11 +135,9 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) { return 0; } -// Check if enemy can see player (adjacent) -static int can_see_player(Enemy *e, Player *p) { - int dx = p->x - e->x; - int dy = p->y - e->y; - return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1); +// Check if enemy can see player (within view range and line of sight) +static int can_see_player(Enemy *e, Player *p, Map *map) { + return can_see_entity(map, e->x, e->y, p->x, p->y, ENEMY_VIEW_RANGE); } // Check if position is occupied by player @@ -178,7 +177,27 @@ static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_e } } -// Perform a single action for an enemy (attack if adjacent, otherwise move) +// Move enemy in a random direction (patrol) +static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) { + if (rng_int(0, 100) > ENEMY_PATROL_MOVE_CHANCE) + return; + + int dx = rng_int(-1, 1); + int dy = rng_int(-1, 1); + + if (dx == 0 && dy == 0) + return; + + int new_x = e->x + dx; + int new_y = e->y + dy; + + if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->x = new_x; + e->y = new_y; + } +} + +// Perform a single action for an enemy (attack if visible, otherwise patrol) void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) { if (!e->alive) return; @@ -187,14 +206,22 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun if (combat_has_effect(e->effects, e->effect_count, EFFECT_STUN)) return; - // Check if adjacent to player - attack - if (can_see_player(e, p)) { + int can_see = can_see_player(e, p, map); + + // Attack if adjacent to player + if (can_see && can_see_entity(map, e->x, e->y, p->x, p->y, 1)) { combat_enemy_attack(e, p); return; } - // Otherwise, move toward player - enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + // Move toward player if visible + if (can_see) { + enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + return; + } + + // Player not visible - patrol randomly + enemy_patrol(e, map, all_enemies, enemy_count); } void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) { diff --git a/src/main.c b/src/main.c index d450d75..aa367ab 100644 --- a/src/main.c +++ b/src/main.c @@ -122,6 +122,9 @@ static void init_floor(GameState *gs, int floor_num) { } gs->player.floor = floor_num; + // Calculate initial visibility + calculate_visibility(&gs->map, gs->player.x, gs->player.y); + // Spawn enemies enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num); @@ -215,6 +218,9 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { } } + // Update visibility based on player's new position + calculate_visibility(&gs->map, gs->player.x, gs->player.y); + // Enemy turns - uses speed/cooldown system enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); @@ -246,6 +252,7 @@ static int handle_stun_turn(GameState *gs) { if (gs->game_over) return 1; enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); + calculate_visibility(&gs->map, gs->player.x, gs->player.y); if (gs->player.hp <= 0) gs->game_over = 1; gs->last_message = "You are stunned!"; @@ -541,8 +548,8 @@ static void game_loop(void) { cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y}; BeginMode2D(cam); render_map(&gs.map); - render_items(gs.items, gs.item_count); - render_enemies(gs.enemies, gs.enemy_count); + render_items(gs.items, gs.item_count, gs.map.visible); + render_enemies(gs.enemies, gs.enemy_count, gs.map.visible); render_player(&gs.player); EndMode2D(); diff --git a/src/map.c b/src/map.c index 24016c9..a29889b 100644 --- a/src/map.c +++ b/src/map.c @@ -1,6 +1,8 @@ #include "map.h" #include "rng.h" +#include "settings.h" #include "utils.h" +#include #include void map_init(Map *map) { @@ -186,3 +188,67 @@ void dungeon_generate(Dungeon *d, Map *map, int floor_num) { d->room_count = map->room_count; memcpy(d->rooms, map->rooms, sizeof(Room) * map->room_count); } + +int is_in_view_range(int x, int y, int view_x, int view_y, int range) { + int dx = x - view_x; + int dy = y - view_y; + return (dx * dx + dy * dy) <= (range * range); +} + +static int trace_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) { + int dx = abs(x2 - x1); + int dy = abs(y2 - y1); + int sx = (x1 < x2) ? 1 : -1; + int sy = (y1 < y2) ? 1 : -1; + int err = dx - dy; + int x = x1; + int y = y1; + + while (1) { + if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) + return 0; + + if (x == x2 && y == y2) + return 1; + + if (map->tiles[y][x] == TILE_WALL && !(x == x1 && y == y1)) + return 0; + + int e2 = 2 * err; + if (e2 > -dy) { + err -= dy; + x += sx; + } + if (e2 < dx) { + err += dx; + y += sy; + } + } +} + +int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) { + if (!in_bounds(x1, y1, MAP_WIDTH, MAP_HEIGHT) || !in_bounds(x2, y2, MAP_WIDTH, MAP_HEIGHT)) + return 0; + return trace_line_of_sight(map, x1, y1, x2, y2); +} + +int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range) { + if (!is_in_view_range(to_x, to_y, from_x, from_y, range)) + return 0; + return has_line_of_sight(map, from_x, from_y, to_x, to_y); +} + +void calculate_visibility(Map *map, int x, int y) { + memset(map->visible, 0, sizeof(map->visible)); + + for (int ty = 0; ty < MAP_HEIGHT; ty++) { + for (int tx = 0; tx < MAP_WIDTH; tx++) { + if (is_in_view_range(tx, ty, x, y, PLAYER_VIEW_RANGE)) { + if (has_line_of_sight(map, x, y, tx, ty)) { + map->visible[ty][tx] = 1; + map->remembered[ty][tx] = 1; + } + } + } + } +} diff --git a/src/map.h b/src/map.h index 3cf6337..84f1119 100644 --- a/src/map.h +++ b/src/map.h @@ -18,4 +18,10 @@ void map_init(Map *map); // Get a random floor tile position void get_random_floor_tile(Map *map, int *x, int *y, int attempts); +// Visibility / Fog of War +int is_in_view_range(int x, int y, int view_x, int view_y, int range); +int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2); +void calculate_visibility(Map *map, int x, int y); +int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range); + #endif // MAP_H diff --git a/src/render.c b/src/render.c index 3c24e15..206843d 100644 --- a/src/render.c +++ b/src/render.c @@ -11,18 +11,31 @@ void render_map(const Map *map) { for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { Rectangle rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; + int visible = map->visible[y][x]; + int remembered = map->remembered[y][x]; + + if (!visible && !remembered) { + DrawRectangleRec(rect, (Color){5, 5, 10, 255}); + continue; + } + + Color wall_color = visible ? DARKGRAY : (Color){25, 25, 30, 255}; + Color floor_color = visible ? BLACK : (Color){15, 15, 20, 255}; + Color stairs_color = visible ? (Color){100, 100, 100, 255} : (Color){40, 40, 45, 255}; switch (map->tiles[y][x]) { case TILE_WALL: - DrawRectangleRec(rect, DARKGRAY); + DrawRectangleRec(rect, wall_color); break; case TILE_FLOOR: - DrawRectangleRec(rect, BLACK); + DrawRectangleRec(rect, floor_color); break; case TILE_STAIRS: - DrawRectangleRec(rect, (Color){100, 100, 100, 255}); - // Draw stairs marker - DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE); + DrawRectangleRec(rect, stairs_color); + if (visible) + DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE); + else + DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, (Color){60, 60, 65, 255}); break; } } @@ -34,10 +47,12 @@ void render_player(const Player *p) { DrawRectangleRec(rect, BLUE); } -void render_enemies(const Enemy *enemies, int count) { +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]) { for (int i = 0; i < count; i++) { if (!enemies[i].alive) continue; + if (!visible[enemies[i].y][enemies[i].x]) + continue; Rectangle rect = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; @@ -79,10 +94,12 @@ void render_enemies(const Enemy *enemies, int count) { } } -void render_items(const Item *items, int count) { +void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]) { for (int i = 0; i < count; i++) { if (items[i].picked_up) continue; + if (!visible[items[i].y][items[i].x]) + continue; Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; diff --git a/src/render.h b/src/render.h index 016291f..d934ffc 100644 --- a/src/render.h +++ b/src/render.h @@ -80,10 +80,10 @@ void render_map(const Map *map); void render_player(const Player *p); // Render all enemies -void render_enemies(const Enemy *enemies, int count); +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); // Render all items -void render_items(const Item *items, int count); +void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); // Render UI overlay void render_ui(const Player *p); diff --git a/src/settings.h b/src/settings.h index 8b8ac54..1b96056 100644 --- a/src/settings.h +++ b/src/settings.h @@ -63,4 +63,9 @@ // Message timer #define MESSAGE_TIMER_DURATION 60 +// Visibility / Fog of War +#define PLAYER_VIEW_RANGE 8 +#define ENEMY_VIEW_RANGE 6 +#define ENEMY_PATROL_MOVE_CHANCE 30 + #endif // SETTINGS_H From dbf8d4886c671912b547f62e145177c1ed542516 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 9 Apr 2026 12:31:52 +0300 Subject: [PATCH 2/5] enemy: add alert memory; vision variance based on type Signed-off-by: NotAShelf Change-Id: I2f5c7cac72c8772e5871b99026d106b46a6a6964 --- src/common.h | 5 +++ src/enemy.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/map.c | 2 ++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/common.h b/src/common.h index 2f4021f..08761c0 100644 --- a/src/common.h +++ b/src/common.h @@ -100,6 +100,11 @@ typedef struct { int status_chance; int crit_chance; // crit chance percentage (0-100) int crit_mult; // crit damage multiplier percentage (e.g. 150 = 1.5x) + // vision + int vision_range; + int alert; // 1 = aware of player, searching + int last_known_x; // last position where enemy saw player + int last_known_y; // status effects StatusEffect effects[MAX_EFFECTS]; int effect_count; diff --git a/src/enemy.c b/src/enemy.c index 764d2d9..0169096 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -69,6 +69,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = 0; e.resistance[DMG_FIRE] = -25; e.resistance[DMG_POISON] = 50; + e.vision_range = 7; break; case ENEMY_SKELETON: e.max_hp = ENEMY_BASE_HP + floor + 2; @@ -86,6 +87,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = 50; e.resistance[DMG_FIRE] = 25; e.resistance[DMG_POISON] = 75; + e.vision_range = 6; break; case ENEMY_ORC: e.max_hp = ENEMY_BASE_HP + floor + 4; @@ -103,6 +105,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = -25; e.resistance[DMG_FIRE] = 0; e.resistance[DMG_POISON] = 0; + e.vision_range = 5; break; default: e.max_hp = ENEMY_BASE_HP; @@ -116,6 +119,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.crit_chance = ENEMY_CRIT_CHANCE; e.crit_mult = ENEMY_CRIT_MULT; memset(e.resistance, 0, sizeof(e.resistance)); + e.vision_range = ENEMY_VIEW_RANGE; break; } e.cooldown = e.speed; @@ -137,7 +141,7 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) { // Check if enemy can see player (within view range and line of sight) static int can_see_player(Enemy *e, Player *p, Map *map) { - return can_see_entity(map, e->x, e->y, p->x, p->y, ENEMY_VIEW_RANGE); + return can_see_entity(map, e->x, e->y, p->x, p->y, e->vision_range); } // Check if position is occupied by player @@ -197,7 +201,68 @@ static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count } } -// Perform a single action for an enemy (attack if visible, otherwise patrol) +// Move enemy toward last known player position +static void enemy_move_to_last_known(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) { + int dx = 0, dy = 0; + + if (e->last_known_x > e->x) + dx = 1; + else if (e->last_known_x < e->x) + dx = -1; + + if (e->last_known_y > e->y) + dy = 1; + else if (e->last_known_y < e->y) + dy = -1; + + int new_x = e->x + dx; + int new_y = e->y; + + if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->x = new_x; + } else if (dy != 0) { + new_x = e->x; + new_y = e->y + dy; + if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->x = new_x; + e->y = new_y; + } + } + + if (e->x == e->last_known_x && e->y == e->last_known_y) + e->alert = 0; +} + +// Check if position is within alert radius of another enemy +static int is_nearby_enemy(const Enemy *enemies, int count, int x, int y, int radius) { + for (int i = 0; i < count; i++) { + if (!enemies[i].alive) + continue; + int dx = enemies[i].x - x; + int dy = enemies[i].y - y; + if (dx * dx + dy * dy <= radius * radius) + return 1; + } + return 0; +} + +// Propagate alert to nearby enemies +static void propagate_alert(Enemy *trigger_enemy, Enemy *all_enemies, int enemy_count) { + for (int i = 0; i < enemy_count; i++) { + Enemy *e = &all_enemies[i]; + if (!e->alive || e == trigger_enemy) + continue; + if (e->alert) + continue; + if (is_nearby_enemy(all_enemies, enemy_count, e->x, e->y, 5)) { + e->alert = 1; + e->last_known_x = trigger_enemy->last_known_x; + e->last_known_y = trigger_enemy->last_known_y; + } + } +} + +// Perform a single action for an enemy (attack if visible, otherwise patrol or search) void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) { if (!e->alive) return; @@ -208,19 +273,34 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun int can_see = can_see_player(e, p, map); + // If we can see the player, update alert state and last known position + if (can_see) { + e->alert = 1; + e->last_known_x = p->x; + e->last_known_y = p->y; + } + // Attack if adjacent to player if (can_see && can_see_entity(map, e->x, e->y, p->x, p->y, 1)) { combat_enemy_attack(e, p); + propagate_alert(e, all_enemies, enemy_count); return; } // Move toward player if visible if (can_see) { enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + propagate_alert(e, all_enemies, enemy_count); return; } - // Player not visible - patrol randomly + // If alert but can't see player, move toward last known position + if (e->alert) { + enemy_move_to_last_known(e, map, all_enemies, enemy_count); + return; + } + + // Not alert - patrol randomly enemy_patrol(e, map, all_enemies, enemy_count); } diff --git a/src/map.c b/src/map.c index a29889b..b9d5bbc 100644 --- a/src/map.c +++ b/src/map.c @@ -12,6 +12,8 @@ void map_init(Map *map) { map->tiles[y][x] = TILE_WALL; } } + memset(map->visible, 0, sizeof(map->visible)); + memset(map->remembered, 0, sizeof(map->remembered)); map->room_count = 0; } From 5de711cdafb0c7f2496c6dcf8fc71d73c9fa7da1 Mon Sep 17 00:00:00 2001 From: PapaMilky Date: Thu, 9 Apr 2026 21:32:51 +1000 Subject: [PATCH 3/5] Refactored some UI renderer code for consistency and readability. --- src/render.c | 211 +++++++++++++++++++++++++++++---------------------- 1 file changed, 122 insertions(+), 89 deletions(-) diff --git a/src/render.c b/src/render.c index 3c24e15..6275e4a 100644 --- a/src/render.c +++ b/src/render.c @@ -108,36 +108,33 @@ void render_items(const Item *items, int count) { } } -void render_ui(const Player *p) { - // HUD Panel - const int hud_y = MAP_HEIGHT * TILE_SIZE; - const int hud_height = 60; - const Color hud_bg = {25, 20, 15, 255}; // dark parchment - const Color hud_border = {139, 119, 89, 255}; // bronze/brown border - const Color text_dim = {160, 150, 140, 255}; // dimmed text - const Color text_bright = {240, 230, 220, 255}; // bright text +static void draw_hud_background(int hud_y, int hud_height, Color hud_bg, Color hud_border, Color magic_a, Color magic_b ) { + // Main HUD background with border + Rectangle ui_bg = {0, (float)hud_y, (float)SCREEN_WIDTH, (float)hud_height}; + DrawRectangleRec(ui_bg, hud_bg); + DrawRectangleLines(0, hud_y, SCREEN_WIDTH, hud_height, hud_border); + DrawLine(0, hud_y + 1, SCREEN_WIDTH, hud_y + 1, magic_a); + DrawLine(0, hud_y + hud_height - 2, SCREEN_WIDTH, hud_y + hud_height - 2, magic_b); // Magic number 2, probably offset from window bottom +} - // Main HUD background with border - Rectangle ui_bg = {0, (float)hud_y, (float)SCREEN_WIDTH, (float)hud_height}; - DrawRectangleRec(ui_bg, hud_bg); - DrawRectangleLines(0, hud_y, SCREEN_WIDTH, hud_height, hud_border); - DrawLine(0, hud_y + 1, SCREEN_WIDTH, hud_y + 1, (Color){60, 55, 50, 255}); - DrawLine(0, hud_y + hud_height - 2, SCREEN_WIDTH, hud_y + hud_height - 2, (Color){15, 12, 10, 255}); - - // Section dividers - int section1_end = 180; // after portrait + HP bar - int section2_end = 310; // after stats - int section3_end = 480; // after equipment - - DrawLine(section1_end, hud_y + 5, section1_end, hud_y + hud_height - 5, (Color){60, 55, 50, 255}); - DrawLine(section1_end + 1, hud_y + 5, section1_end + 1, hud_y + hud_height - 5, (Color){15, 12, 10, 255}); - - DrawLine(section2_end, hud_y + 5, section2_end, hud_y + hud_height - 5, (Color){60, 55, 50, 255}); - DrawLine(section2_end + 1, hud_y + 5, section2_end + 1, hud_y + hud_height - 5, (Color){15, 12, 10, 255}); +static void draw_section_divider(int hud_y, int section_x, int border_gap, int hud_height, Color magic_a, Color magic_b) { + DrawLine(section_x, hud_y + border_gap, section_x, hud_y + hud_height - border_gap, magic_a); + DrawLine(section_x + 1, hud_y + border_gap, section_x + 1, hud_y + hud_height - border_gap, magic_b); +} +static void draw_player_life( + const Player *p, int hud_y, Color text_bright, Color text_dim, Color full_health, + Color low_health, Color crit_health, Color hp_background, Color hp_outline + ) { + // HP Bar, to the right of portrait int portrait_x = 8; int portrait_y = hud_y + 8; int portrait_size = 44; + int bar_x = portrait_x + portrait_size + 8; + int bar_y = hud_y + 22; + int bar_width = 100; + int bar_height = 16; + // FIXME: for now this is just a blue square indicating the player. Once we // model the player, add classes, sprites, etc. this will need to be revisited. @@ -145,29 +142,23 @@ void render_ui(const Player *p) { DrawRectangle(portrait_x + 2, portrait_y + 2, portrait_size - 4, portrait_size - 4, BLUE); DrawRectangleLines(portrait_x, portrait_y, portrait_size, portrait_size, (Color){139, 119, 89, 255}); - // HP Bar, to the right of portrait - int bar_x = portrait_x + portrait_size + 8; - int bar_y = hud_y + 20; - int bar_width = 100; - int bar_height = 16; - // HP Label, above bar DrawText("HP", bar_x, bar_y - 11, 9, text_dim); // HP Bar background - DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){20, 15, 15, 255}); - DrawRectangleLines(bar_x, bar_y, bar_width, bar_height, (Color){80, 70, 60, 255}); + DrawRectangle(bar_x, bar_y, bar_width, bar_height, hp_background); + DrawRectangleLines(bar_x, bar_y, bar_width, bar_height, hp_outline); // HP Bar fill float hp_percent = (float)p->hp / p->max_hp; int fill_width = (int)(bar_width * hp_percent); Color hp_color; if (hp_percent > 0.6f) { - hp_color = (Color){60, 180, 60, 255}; + hp_color = full_health; } else if (hp_percent > 0.3f) { - hp_color = (Color){200, 180, 40, 255}; + hp_color = low_health; } else { - hp_color = (Color){200, 60, 60, 255}; + hp_color = crit_health; } if (fill_width > 0) { @@ -178,7 +169,7 @@ void render_ui(const Player *p) { char hp_text[32]; snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp); int hp_text_w = MeasureText(hp_text, 10); - DrawText(hp_text, bar_x + (bar_width - hp_text_w) / 2, bar_y + 2, 10, WHITE); + DrawText(hp_text, bar_x + (bar_width - hp_text_w) / 2, bar_y + 3, 10, WHITE); // Status effects int effect_x = bar_x; @@ -218,69 +209,110 @@ void render_ui(const Player *p) { } } - int stats_x = section1_end + 15; - int stats_y = hud_y + 12; - int stat_spacing = 40; +} - // Floor - char floor_text[16]; - snprintf(floor_text, sizeof(floor_text), "F%d", p->floor); - DrawText(floor_text, stats_x, stats_y, 14, text_bright); - DrawText("Floor", stats_x, stats_y + 16, 9, text_dim); +static void draw_player_stats( + const Player *p, int hud_y, int stats_x, Color text_bright, Color text_dim +) { + int stats_y = hud_y + 12; + int stat_spacing = 40; - // ATK - char atk_text[16]; - snprintf(atk_text, sizeof(atk_text), "%d", p->attack); - DrawText(atk_text, stats_x + stat_spacing, stats_y, 14, YELLOW); - DrawText("ATK", stats_x + stat_spacing, stats_y + 16, 9, text_dim); + char floor_text[16]; + snprintf(floor_text, sizeof(floor_text), "F%d", p->floor); + DrawText(floor_text, stats_x, stats_y, 14, text_bright); + DrawText("Floor", stats_x, stats_y + 16, 9, text_dim); - // DEF - char def_text[16]; - snprintf(def_text, sizeof(def_text), "%d", p->defense); - DrawText(def_text, stats_x + stat_spacing * 2, stats_y, 14, (Color){100, 150, 255, 255}); - DrawText("DEF", stats_x + stat_spacing * 2, stats_y + 16, 9, text_dim); + char atk_text[16]; + snprintf(atk_text, sizeof(atk_text), "%d", p->attack); + DrawText(atk_text, stats_x + stat_spacing, stats_y, 14, YELLOW); + DrawText("ATK", stats_x + stat_spacing, stats_y + 16, 9, text_dim); - int equip_x = section2_end + 15; - int equip_y = hud_y + 8; + char def_text[16]; + snprintf(def_text, sizeof(def_text), "%d", p->defense); + DrawText(def_text, stats_x + stat_spacing * 2, stats_y, 14, (Color){100, 150, 255, 255}); + DrawText("DEF", stats_x + stat_spacing * 2, stats_y + 16, 9, text_dim); +} - // Weapon slot - DrawText("WEAPON", equip_x, equip_y, 9, text_dim); - if (p->has_weapon) { - const char *weapon_name = item_get_name(&p->equipped_weapon); - if (weapon_name) { - char weapon_text[64]; - snprintf(weapon_text, sizeof(weapon_text), "%s +%d [%s]", weapon_name, p->equipped_weapon.power, - dmg_class_get_short(p->equipped_weapon.dmg_class)); - DrawText(weapon_text, equip_x, equip_y + 11, 10, (Color){255, 220, 100, 255}); +static void draw_equipment( + const Player *p, int hud_y, int equip_x, Color text_dim +) { + int equip_y = hud_y + 8; + + DrawText("WEAPON", equip_x, equip_y, 9, text_dim); + if (p->has_weapon) { + const char *weapon_name = item_get_name(&p->equipped_weapon); + if (weapon_name) { + char weapon_text[64]; + snprintf(weapon_text, sizeof(weapon_text), "%s +%d [%s]", + weapon_name, p->equipped_weapon.power, + dmg_class_get_short(p->equipped_weapon.dmg_class)); + DrawText(weapon_text, equip_x, equip_y + 11, 10, (Color){255, 220, 100, 255}); + } + } else { + DrawText("None [IMP]", equip_x, equip_y + 11, 10, (Color){80, 75, 70, 255}); } - } else { - DrawText("None [IMP]", equip_x, equip_y + 11, 10, (Color){80, 75, 70, 255}); - } - // Armor slot - DrawText("ARMOR", equip_x, equip_y + 26, 9, text_dim); - if (p->has_armor) { - const char *armor_name = item_get_name(&p->equipped_armor); - if (armor_name) { - char armor_text[48]; - snprintf(armor_text, sizeof(armor_text), "%s +%d", armor_name, p->equipped_armor.power); - DrawText(armor_text, equip_x, equip_y + 37, 10, (Color){100, 150, 255, 255}); + DrawText("ARMOR", equip_x, equip_y + 26, 9, text_dim); + if (p->has_armor) { + const char *armor_name = item_get_name(&p->equipped_armor); + if (armor_name) { + char armor_text[48]; + snprintf(armor_text, sizeof(armor_text), "%s +%d", + armor_name, p->equipped_armor.power); + DrawText(armor_text, equip_x, equip_y + 37, 10, (Color){100, 150, 255, 255}); + } + } else { + DrawText("None", equip_x, equip_y + 37, 10, (Color){80, 75, 70, 255}); } - } else { - DrawText("None", equip_x, equip_y + 37, 10, (Color){80, 75, 70, 255}); - } +} - int ctrl_x = section3_end + 20; - int ctrl_y = hud_y + 14; +static void draw_controls_inv(const Player *p, int hud_y, int ctrl_x, Color controls_color) { + int ctrl_y = hud_y + 14; + DrawText("[WASD] Move [G] Pickup [I] Inventory [U] Use", + ctrl_x, ctrl_y, 11, controls_color); + DrawText("[E] Equip [D] Drop [Q] Quit", + ctrl_x, ctrl_y + 16, 11, controls_color); - DrawText("[WASD] Move [G] Pickup [I] Inventory [U] Use", ctrl_x, ctrl_y, 11, (Color){139, 119, 89, 255}); - DrawText("[E] Equip [D] Drop [Q] Quit", ctrl_x, ctrl_y + 16, 11, (Color){139, 119, 89, 255}); + char inv_text[16]; + snprintf(inv_text, sizeof(inv_text), "INV: %d/%d", p->inventory_count, MAX_INVENTORY); + int inv_width = MeasureText(inv_text, 10); + DrawText(inv_text, SCREEN_WIDTH - inv_width - 10, hud_y + 5, 10, GREEN); +} - // INV count in top-right corner of HUD - char inv_text[16]; - snprintf(inv_text, sizeof(inv_text), "INV: %d/%d", p->inventory_count, MAX_INVENTORY); - int inv_width = MeasureText(inv_text, 10); - DrawText(inv_text, SCREEN_WIDTH - inv_width - 10, hud_y + 5, 10, GREEN); +void render_ui(const Player *p) { + // HUD Panel + const int hud_y = MAP_HEIGHT * TILE_SIZE; + const int hud_height = 60; + const Color hud_bg = {25, 20, 15, 255}; // dark parchment + const Color hud_border = {139, 119, 89, 255}; // bronze/brown border + const Color text_dim = {160, 150, 140, 255}; // dimmed text + const Color text_bright = {240, 230, 220, 255}; // bright text + const Color magic_color_a = {60, 55, 50, 255}; // I don't know what exactly this is (may be top/left colouring) + const Color magic_color_b = {15, 12, 10, 255}; // See above (may be bottom/right colouring) + const Color full_health = {60, 180, 60, 255}; + const Color low_health = {200, 180, 40, 255}; + const Color crit_health = {200, 60, 60, 255}; + const Color health_bar_background = {20, 15, 15, 255}; + const Color health_bar_outline = {80, 70, 60, 255}; + + draw_hud_background(hud_y, hud_height, hud_bg, hud_border, magic_color_a, magic_color_b); + + // Section dividers + int section1_end = 180; // after portrait + HP bar + int section2_end = 310; // after stats + int section3_end = 480; // after equipment + + draw_section_divider(hud_y, section1_end, 5, hud_height, magic_color_a, magic_color_b); + draw_section_divider(hud_y, section2_end, 5, hud_height, magic_color_a, magic_color_b); + + draw_player_life(p, hud_y, text_bright, text_dim, full_health, low_health, + crit_health, health_bar_background, health_bar_outline); + + draw_player_stats(p, hud_y, section1_end + 15, text_bright, text_dim); + + draw_equipment(p, hud_y, section2_end + 15, text_dim); + + draw_controls_inv(p, hud_y, section3_end + 20, hud_border); } void render_action_log(const char log[5][128], int count, int head) { @@ -583,6 +615,7 @@ void render_end_screen(int is_victory, int kills, int items, int damage_dealt, i } } +// Floating message popup void render_message(const char *message) { if (message == NULL) return; From af14fd258bf2ba15a68536a8b5eb591034a28c5f Mon Sep 17 00:00:00 2001 From: PapaMilky Date: Thu, 9 Apr 2026 21:34:30 +1000 Subject: [PATCH 4/5] Fixed an off by 1 UI inconsistency. --- src/render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/render.c b/src/render.c index 6275e4a..f77839f 100644 --- a/src/render.c +++ b/src/render.c @@ -340,7 +340,7 @@ void render_action_log(const char log[5][128], int count, int head) { DrawText("MESSAGE LOG", log_x + 8, log_y + 6, 10, (Color){180, 160, 130, 255}); // Separator line under title - DrawLine(log_x + 4, log_y + 22, log_x + log_width - 5, log_y + 22, log_border_dark); + DrawLine(log_x + 4, log_y + 22, log_x + log_width - 4, log_y + 22, log_border_dark); // Log entries int text_x = log_x + 8; From dab3e1554c0c1f478f547eb855a43f67abf9a096 Mon Sep 17 00:00:00 2001 From: PapaMilky Date: Thu, 9 Apr 2026 21:58:44 +1000 Subject: [PATCH 5/5] Fixed a UI inconsistency in the inventory overlay --- src/render.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/render.c b/src/render.c index f77839f..350901c 100644 --- a/src/render.c +++ b/src/render.c @@ -370,7 +370,7 @@ void render_action_log(const char log[5][128], int count, int head) { void render_inventory_overlay(const Player *p, int selected) { // Overlay dimensions int ov_width = 360; - int ov_height = 300; + int ov_height = 337; Rectangle overlay = {(float)(SCREEN_WIDTH - ov_width) / 2, (float)(SCREEN_HEIGHT - ov_height) / 2 - 60, (float)ov_width, (float)ov_height}; DrawRectangleRec(overlay, (Color){12, 12, 12, 252}); @@ -394,8 +394,8 @@ void render_inventory_overlay(const Player *p, int selected) { // Selection highlight if (i == selected) { - DrawRectangle((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2, (Color){45, 45, 45, 255}); - DrawRectangleLines((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2, + DrawRectangle((int)overlay.x + 6, y_pos - 2, (int)overlay.width - 12, row_height - 2, (Color){45, 45, 45, 255}); + DrawRectangleLines((int)overlay.x + 6, y_pos - 2, (int)overlay.width - 12, row_height - 2, (Color){180, 160, 80, 255}); }