1
0
Fork 0
forked from NotAShelf/rogged

ui: add floating damage text and shake screen; streamline action log

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7a85e496beaf78150c6c1f7ddbb343d96a6a6964
This commit is contained in:
raf 2026-04-03 16:32:32 +03:00
commit a89d3684ef
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 218 additions and 84 deletions

View file

@ -10,8 +10,51 @@
#include "rng.h" #include "rng.h"
#include "settings.h" #include "settings.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include <string.h> #include <string.h>
// Add message to action log
static void add_log(GameState *gs, const char *msg) {
strncpy(gs->action_log[gs->log_head], msg, 127);
gs->action_log[gs->log_head][127] = '\0';
gs->log_head = (gs->log_head + 1) % 5;
if (gs->log_count < 5) {
gs->log_count++;
}
}
// spawn floating damage text
static void spawn_floating_text(GameState *gs, int x, int y, int value, int is_critical) {
if (gs->floating_count < 8) {
gs->floating_texts[gs->floating_count].x = x;
gs->floating_texts[gs->floating_count].y = y;
gs->floating_texts[gs->floating_count].value = value;
gs->floating_texts[gs->floating_count].lifetime = 60;
gs->floating_texts[gs->floating_count].is_critical = is_critical;
gs->floating_count++;
}
}
// update floating texts and screen shake
static void update_effects(GameState *gs) {
// update floating texts
for (int i = 0; i < 8; i++) {
if (gs->floating_texts[i].lifetime > 0) {
gs->floating_texts[i].lifetime--;
}
}
// update screen shake
if (gs->screen_shake > 0) {
gs->screen_shake--;
gs->shake_x = rng_int(-4, 4);
gs->shake_y = rng_int(-4, 4);
} else {
gs->shake_x = 0;
gs->shake_y = 0;
}
}
// Initialize a new floor // Initialize a new floor
static void init_floor(GameState *gs, int floor_num) { static void init_floor(GameState *gs, int floor_num) {
// Generate dungeon // Generate dungeon
@ -108,11 +151,11 @@ static int handle_input(GameState *gs) {
if (player_equip_item(&gs->player, gs->inv_selected)) { if (player_equip_item(&gs->player, gs->inv_selected)) {
gs->last_message = "Item equipped!"; gs->last_message = "Item equipped!";
gs->message_timer = 60; gs->message_timer = 60;
// Adjust selection if needed add_log(gs, "Equipped item");
if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) { if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) {
gs->inv_selected--; gs->inv_selected--;
} }
return 1; // Consume turn return 1;
} else { } else {
gs->last_message = "Cannot equip that!"; gs->last_message = "Cannot equip that!";
gs->message_timer = 60; gs->message_timer = 60;
@ -129,8 +172,9 @@ static int handle_input(GameState *gs) {
player_remove_inventory_item(&gs->player, gs->inv_selected); player_remove_inventory_item(&gs->player, gs->inv_selected);
gs->last_message = "Used potion!"; gs->last_message = "Used potion!";
gs->message_timer = 60; gs->message_timer = 60;
add_log(gs, "Used potion");
gs->show_inventory = 0; gs->show_inventory = 0;
return 1; // Consume turn return 1;
} else { } else {
gs->last_message = "Equip weapons/armor with E!"; gs->last_message = "Equip weapons/armor with E!";
gs->message_timer = 60; gs->message_timer = 60;
@ -139,6 +183,29 @@ static int handle_input(GameState *gs) {
} }
} }
// D to drop selected item
if (IsKeyPressed(KEY_D)) {
if (gs->player.inventory_count > 0) {
Item *item = player_get_inventory_item(&gs->player, gs->inv_selected);
if (item != NULL) {
char drop_msg[64];
snprintf(drop_msg, sizeof(drop_msg), "Dropped %s", item_get_name(item));
if (player_drop_item(&gs->player, gs->inv_selected, gs->items, gs->item_count)) {
add_log(gs, drop_msg);
gs->last_message = "Item dropped!";
gs->message_timer = 60;
if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) {
gs->inv_selected--;
}
return 1;
} else {
gs->last_message = "Cannot drop!";
gs->message_timer = 60;
}
}
}
}
return 0; return 0;
} }
@ -151,6 +218,7 @@ static int handle_input(GameState *gs) {
init_floor(gs, gs->player.floor + 1); init_floor(gs, gs->player.floor + 1);
gs->last_message = "Descended to next floor!"; gs->last_message = "Descended to next floor!";
gs->message_timer = 60; gs->message_timer = 60;
add_log(gs, "Descended stairs");
gs->awaiting_descend = 0; gs->awaiting_descend = 0;
return 1; return 1;
} else { } else {
@ -166,14 +234,14 @@ static int handle_input(GameState *gs) {
gs->message_timer = 60; gs->message_timer = 60;
return 1; return 1;
} }
return 0; // Waiting for Y/N return 0;
} }
// Check for inventory toggle (I key) // Check for inventory toggle (I key)
if (IsKeyPressed(KEY_I) && !gs->game_over) { if (IsKeyPressed(KEY_I) && !gs->game_over) {
gs->show_inventory = 1; gs->show_inventory = 1;
gs->inv_selected = 0; gs->inv_selected = 0;
return 0; // Don't consume turn return 0; // don't consume turn
} }
// Check for manual item pickup (G key) // Check for manual item pickup (G key)
@ -181,10 +249,13 @@ static int handle_input(GameState *gs) {
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y); Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y);
if (item != NULL) { if (item != NULL) {
if (player_pickup(&gs->player, item)) { if (player_pickup(&gs->player, item)) {
char pickup_msg[64];
snprintf(pickup_msg, sizeof(pickup_msg), "Picked up %s", item_get_name(item));
add_log(gs, pickup_msg);
gs->last_message = "Picked up item!"; gs->last_message = "Picked up item!";
gs->message_timer = 60; gs->message_timer = 60;
audio_play_item_pickup(); audio_play_item_pickup();
return 1; // Consume a turn return 1;
} else { } else {
gs->last_message = "Inventory full!"; gs->last_message = "Inventory full!";
gs->message_timer = 60; gs->message_timer = 60;
@ -235,12 +306,14 @@ static int handle_input(GameState *gs) {
return 1; return 1;
} }
// Check if killed enemy // combat feedback - player attacked enemy
if (combat_get_last_message() != NULL && !combat_was_player_damage()) { if (combat_get_last_damage() > 0 && !combat_was_player_damage()) {
// Check if enemy died // find the enemy we attacked
for (int i = 0; i < gs->enemy_count; i++) { for (int i = 0; i < gs->enemy_count; i++) {
if (!gs->enemies[i].alive) { if (!gs->enemies[i].alive && combat_get_last_damage() > 0) {
audio_play_enemy_death(); spawn_floating_text(gs, gs->enemies[i].x * TILE_SIZE + 8, gs->enemies[i].y * TILE_SIZE,
combat_get_last_damage(), combat_was_critical());
break;
} }
} }
} }
@ -251,6 +324,9 @@ static int handle_input(GameState *gs) {
// Check if player took damage // Check if player took damage
if (combat_was_player_damage() && combat_get_last_damage() > 0) { if (combat_was_player_damage() && combat_get_last_damage() > 0) {
audio_play_player_damage(); audio_play_player_damage();
gs->screen_shake = 8;
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, combat_get_last_damage(),
combat_was_critical());
} }
// Set message // Set message
@ -301,17 +377,28 @@ static void game_loop(void) {
if (gs.message_timer > 0) if (gs.message_timer > 0)
gs.message_timer--; gs.message_timer--;
// Update effects
update_effects(&gs);
// Render // Render
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
// Draw game elements // Draw game elements (with screen shake offset)
if (gs.screen_shake > 0) {
// Apply shake offset to drawing
}
render_map(&gs.map); render_map(&gs.map);
render_items(gs.items, gs.item_count); render_items(gs.items, gs.item_count);
render_enemies(gs.enemies, gs.enemy_count); render_enemies(gs.enemies, gs.enemy_count);
render_player(&gs.player); render_player(&gs.player);
render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y);
render_ui(&gs.player); render_ui(&gs.player);
// Draw action log
render_action_log(gs.action_log, gs.log_count, gs.log_head);
// Draw inventory overlay if active // Draw inventory overlay if active
if (gs.show_inventory) { if (gs.show_inventory) {
render_inventory_overlay(&gs.player, gs.inv_selected); render_inventory_overlay(&gs.player, gs.inv_selected);

View file

@ -100,136 +100,177 @@ void render_items(const Item *items, int count) {
} }
void render_ui(const Player *p) { void render_ui(const Player *p) {
// UI background bar at bottom of screen // UI background bar (taller for two rows)
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH, 60}; Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH, 60};
DrawRectangleRec(ui_bg, (Color){30, 30, 30, 255}); DrawRectangleRec(ui_bg, (Color){15, 15, 15, 255});
// Draw dividing line // Draw dividing line
DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, GRAY); DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, (Color){50, 50, 50, 255});
// Player hp // HP Bar (row 1, left)
int bar_x = 10;
int bar_y = MAP_HEIGHT * TILE_SIZE + 10;
int bar_width = 140;
int bar_height = 18;
// Bar background
DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){30, 30, 30, 255});
// Bar fill based on HP percentage
float hp_percent = (float)p->hp / p->max_hp;
int fill_width = (int)(bar_width * hp_percent);
// Color gradient: green > yellow > red
Color hp_color;
if (hp_percent > 0.6f) {
hp_color = (Color){60, 180, 60, 255};
} else if (hp_percent > 0.3f) {
hp_color = (Color){200, 180, 40, 255};
} else {
hp_color = (Color){200, 60, 60, 255};
}
DrawRectangle(bar_x, bar_y, fill_width, bar_height, hp_color);
// HP text inside bar
char hp_text[32]; char hp_text[32];
snprintf(hp_text, sizeof(hp_text), "HP: %d/%d", p->hp, p->max_hp); snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp);
DrawText(hp_text, 10, MAP_HEIGHT * TILE_SIZE + 10, 20, RED); int hp_text_w = MeasureText(hp_text, 14);
DrawText(hp_text, bar_x + (bar_width - hp_text_w) / 2, bar_y + 2, 14, WHITE);
// Player attack // Stats row 1: Floor, ATK, DEF, Inv
char atk_text[32]; int stats_y = bar_y;
snprintf(atk_text, sizeof(atk_text), "ATK: %d", p->attack); DrawText("F1", bar_x + bar_width + 15, stats_y, 14, WHITE);
DrawText(atk_text, 120, MAP_HEIGHT * TILE_SIZE + 10, 20, YELLOW); DrawText("ATK", bar_x + bar_width + 50, stats_y, 14, YELLOW);
DrawText("DEF", bar_x + bar_width + 100, stats_y, 14, BLUE);
DrawText("INV", bar_x + bar_width + 145, stats_y, 14, GREEN);
// Floor number // Row 2: equipment slots and controls
char floor_text[32]; int row2_y = stats_y + 24;
snprintf(floor_text, sizeof(floor_text), "Floor: %d", p->floor);
DrawText(floor_text, 220, MAP_HEIGHT * TILE_SIZE + 10, 20, WHITE);
// Defense stat // Equipment (left side of row 2)
char def_text[32];
snprintf(def_text, sizeof(def_text), "DEF: %d", p->defense);
DrawText(def_text, 340, MAP_HEIGHT * TILE_SIZE + 10, 20, BLUE);
// Inventory count
char inv_text[32];
snprintf(inv_text, sizeof(inv_text), "Inv: %d/%d", p->inventory_count, MAX_INVENTORY);
DrawText(inv_text, 440, MAP_HEIGHT * TILE_SIZE + 10, 16, GREEN);
// Equipment display - second row
int eq_y = MAP_HEIGHT * TILE_SIZE + 10;
// Weapon slot
if (p->has_weapon) { if (p->has_weapon) {
char weapon_text[48]; char weapon_text[48];
snprintf(weapon_text, sizeof(weapon_text), "Wpn:[%s +%d]", item_get_name(&p->equipped_weapon), snprintf(weapon_text, sizeof(weapon_text), "Wpn:%s +%d", item_get_name(&p->equipped_weapon),
p->equipped_weapon.power); p->equipped_weapon.power);
DrawText(weapon_text, 10, eq_y + 25, 14, YELLOW); DrawText(weapon_text, 10, row2_y, 12, YELLOW);
} else { } else {
DrawText("Wpn:---", 10, eq_y + 25, 14, DARKGRAY); DrawText("Wpn:---", 10, row2_y, 12, (Color){60, 60, 60, 255});
} }
// Armor slot
if (p->has_armor) { if (p->has_armor) {
char armor_text[48]; char armor_text[48];
snprintf(armor_text, sizeof(armor_text), "Arm:[%s +%d]", item_get_name(&p->equipped_armor), snprintf(armor_text, sizeof(armor_text), "Arm:%s +%d", item_get_name(&p->equipped_armor), p->equipped_armor.power);
p->equipped_armor.power); DrawText(armor_text, 150, row2_y, 12, BLUE);
DrawText(armor_text, 170, eq_y + 25, 14, BLUE);
} else { } else {
DrawText("Arm:---", 170, eq_y + 25, 14, DARKGRAY); DrawText("Arm:---", 150, row2_y, 12, (Color){60, 60, 60, 255});
} }
// Controls hint // Controls hint (right side)
DrawText("WASD:Move G:Pickup I:Inventory U:Use E:Equip Q:Quit", 330, eq_y + 25, 12, GRAY); DrawText("[G] Pickup [I] Inventory [E] Equip [D] Drop", 350, row2_y, 12, (Color){70, 70, 70, 255});
}
void render_action_log(const char log[5][128], int count, int head) {
int log_x = 10;
int log_y = MAP_HEIGHT * TILE_SIZE - 75;
for (int i = 0; i < count && i < 5; i++) {
int idx = (head - count + i + 5) % 5;
if (log[idx][0] != '\0') {
DrawText(log[idx], log_x, log_y + i * 14, 12, (Color){140, 140, 140, 255});
}
}
} }
void render_inventory_overlay(const Player *p, int selected) { void render_inventory_overlay(const Player *p, int selected) {
// Semi-transparent overlay // Overlay dimensions
Rectangle overlay = {(float)(SCREEN_WIDTH / 2 - 200), 80, 400, 320}; int ov_width = 360;
DrawRectangleRec(overlay, (Color){20, 20, 20, 240}); int ov_height = 300;
DrawRectangleLines((int)overlay.x, (int)overlay.y, (int)overlay.width, (int)overlay.height, GOLD); 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});
DrawRectangleLines((int)overlay.x, (int)overlay.y, (int)overlay.width, (int)overlay.height, (Color){70, 70, 70, 255});
// Title // Title
const char *title = "INVENTORY"; const char *title = "INVENTORY";
int title_w = MeasureText(title, 20); int title_w = MeasureText(title, 24);
DrawText(title, overlay.x + (overlay.width - title_w) / 2, overlay.y + 15, 20, GOLD); DrawText(title, overlay.x + (overlay.width - title_w) / 2, overlay.y + 12, 24, WHITE);
// Column headers
DrawText("# Item", overlay.x + 20, overlay.y + 50, 14, GRAY);
DrawText("Type", overlay.x + 130, overlay.y + 50, 14, GRAY);
DrawText("Power", overlay.x + 210, overlay.y + 50, 14, GRAY);
DrawText("Action", overlay.x + 280, overlay.y + 50, 14, GRAY);
// Draw each inventory slot // Draw each inventory slot
char slot_text[64]; char slot_text[64];
int row_height = 26;
int start_y = overlay.y + 50;
for (int i = 0; i < MAX_INVENTORY; i++) { for (int i = 0; i < MAX_INVENTORY; i++) {
int y_pos = overlay.y + 75 + (i * 22); int y_pos = start_y + (i * row_height);
if (i < p->inventory_count && !p->inventory[i].picked_up) { if (i < p->inventory_count && !p->inventory[i].picked_up) {
// Occupied slot
const Item *item = &p->inventory[i]; const Item *item = &p->inventory[i];
// Highlight selected // Selection highlight
if (i == selected) { if (i == selected) {
DrawRectangle((int)overlay.x + 10, y_pos - 2, (int)overlay.width - 20, 20, (Color){60, 60, 60, 255}); 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,
(Color){180, 160, 80, 255});
} }
// Slot number // Slot number
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawText(slot_text, overlay.x + 20, y_pos, 14, WHITE); DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){80, 80, 80, 255});
// Item name // Item name
const char *name = item_get_name(item); const char *name = item_get_name(item);
DrawText(name, overlay.x + 50, y_pos, 14, WHITE); Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255}
: (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255}
// Type : (Color){140, 140, 255, 255};
const char *type = (item->type == ITEM_POTION) ? "Potion" : (item->type == ITEM_WEAPON) ? "Weapon" : "Armor"; DrawText(name, overlay.x + 45, y_pos + 4, 14, name_color);
Color type_color = (item->type == ITEM_POTION) ? PINK : (item->type == ITEM_WEAPON) ? YELLOW : BLUE;
DrawText(type, overlay.x + 130, y_pos, 14, type_color);
// Power // Power
snprintf(slot_text, sizeof(slot_text), "+%d", item->power); snprintf(slot_text, sizeof(slot_text), "+%d", item->power);
DrawText(slot_text, overlay.x + 210, y_pos, 14, YELLOW); DrawText(slot_text, overlay.x + 150, y_pos + 4, 14, YELLOW);
// Action hint // Action
if (item->type == ITEM_POTION) { if (item->type == ITEM_POTION) {
DrawText("[U]se", overlay.x + 280, y_pos, 14, GREEN); DrawText("[U]se", overlay.x + 200, y_pos + 4, 14, GREEN);
} else { } else {
DrawText("[E]quip", overlay.x + 280, y_pos, 14, GOLD); DrawText("[E]quip [D]rop", overlay.x + 200, y_pos + 4, 14, GOLD);
} }
} else { } else {
// Empty slot // Empty slot
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawText(slot_text, overlay.x + 20, y_pos, 14, (Color){60, 60, 60, 255}); DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){40, 40, 40, 255});
} }
} }
// Instructions // Instructions at bottom
const char *hint = "WASD: Select | ENTER/U: Use | E: Equip | ESC: Close"; const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close";
int hint_w = MeasureText(hint, 12); int hint_w = MeasureText(hint, 12);
DrawText(hint, overlay.x + (overlay.width - hint_w) / 2, overlay.y + 290, 12, GRAY); DrawText(hint, overlay.x + (overlay.width - hint_w) / 2, overlay.y + overlay.height - 22, 12,
(Color){65, 65, 65, 255});
}
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y) {
for (int i = 0; i < count; i++) {
if (texts[i].lifetime <= 0)
continue;
int x = texts[i].x + shake_x;
int y = texts[i].y + shake_y - (60 - texts[i].lifetime); // rise over time
float alpha = (float)texts[i].lifetime / 60.0f;
Color color =
texts[i].is_critical ? (Color){255, 200, 50, (int)(255 * alpha)} : (Color){255, 100, 100, (int)(255 * alpha)};
char text[16];
snprintf(text, sizeof(text), "%d", texts[i].value);
int text_w = MeasureText(text, 18);
DrawText(text, x - text_w / 2, y, 18, color);
}
} }
void render_game_over(void) { void render_game_over(void) {
// Semi-transparent overlay // Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT}; Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 200}); DrawRectangleRec(overlay, (Color){0, 0, 0, 210});
// Game over text // Game over text
const char *title = "GAME OVER"; const char *title = "GAME OVER";
@ -247,8 +288,8 @@ void render_message(const char *message) {
// Draw message box // Draw message box
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150), (float)(SCREEN_HEIGHT / 2 - 30), 300, 60}; Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150), (float)(SCREEN_HEIGHT / 2 - 30), 300, 60};
DrawRectangleRec(msg_bg, (Color){50, 50, 50, 230}); DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, WHITE); DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
int msg_width = MeasureText(message, 20); int msg_width = MeasureText(message, 20);
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20, WHITE); DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20, WHITE);

View file

@ -18,9 +18,15 @@ void render_items(const Item *items, int count);
// Render UI overlay // Render UI overlay
void render_ui(const Player *p); void render_ui(const Player *p);
// Render action log (bottom left corner)
void render_action_log(const char log[5][128], int count, int head);
// Render inventory selection overlay // Render inventory selection overlay
void render_inventory_overlay(const Player *p, int selected); void render_inventory_overlay(const Player *p, int selected);
// Render floating damage text
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y);
// Render game over screen // Render game over screen
void render_game_over(void); void render_game_over(void);