From de2ce8ba43222b218739d95961a1fb74fc856c02 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 3 Apr 2026 15:40:55 +0300 Subject: [PATCH] player: add equipment slots and inventory overlay - Weapon/armor equip slots persist until replaced - Numbered inventory overlay with I key - E to equip from inventory, U to use potions" Signed-off-by: NotAShelf Change-Id: I474acd676da3b768c1aac24f75f303e86a6a6964 --- src/items.c | 6 +-- src/items.h | 11 ++++- src/render.c | 119 ++++++++++++++++++++++++++++++++++++++++----------- src/render.h | 3 ++ 4 files changed, 110 insertions(+), 29 deletions(-) diff --git a/src/items.c b/src/items.c index b5d3e6b..c758955 100644 --- a/src/items.c +++ b/src/items.c @@ -57,7 +57,7 @@ void item_spawn(Item items[], int *count, Map *map, int floor) { } // Get item name for display -const char *item_get_name(Item *i) { +const char *item_get_name(const Item *i) { if (i == NULL) return ""; @@ -74,7 +74,7 @@ const char *item_get_name(Item *i) { } // Get item description -const char *item_get_description(Item *i) { +const char *item_get_description(const Item *i) { if (i == NULL) return ""; @@ -91,7 +91,7 @@ const char *item_get_description(Item *i) { } // Get item power value -int item_get_power(Item *i) { +int item_get_power(const Item *i) { if (i == NULL) return 0; return i->power; diff --git a/src/items.h b/src/items.h index 7798323..fa913f2 100644 --- a/src/items.h +++ b/src/items.h @@ -8,7 +8,16 @@ // Spawn items for a floor void item_spawn(Item items[], int *count, Map *map, int floor); -// Use an item +// Use an item (apply effect, only potions are consumed) void item_use(Player *p, Item *i); +// Get item name for display +const char *item_get_name(const Item *i); + +// Get item description +const char *item_get_description(const Item *i); + +// Get item power value +int item_get_power(const Item *i); + #endif // ITEMS_H diff --git a/src/render.c b/src/render.c index ef71f96..62c3cca 100644 --- a/src/render.c +++ b/src/render.c @@ -1,5 +1,6 @@ #include "render.h" #include "common.h" +#include "items.h" #include "raylib.h" #include "settings.h" #include @@ -114,47 +115,115 @@ void render_ui(const Player *p) { // Player attack char atk_text[32]; snprintf(atk_text, sizeof(atk_text), "ATK: %d", p->attack); - DrawText(atk_text, 150, MAP_HEIGHT * TILE_SIZE + 10, 20, YELLOW); + DrawText(atk_text, 120, MAP_HEIGHT * TILE_SIZE + 10, 20, YELLOW); // Floor number char floor_text[32]; snprintf(floor_text, sizeof(floor_text), "Floor: %d", p->floor); - DrawText(floor_text, 280, MAP_HEIGHT * TILE_SIZE + 10, 20, WHITE); + DrawText(floor_text, 220, MAP_HEIGHT * TILE_SIZE + 10, 20, WHITE); // Defense stat char def_text[32]; snprintf(def_text, sizeof(def_text), "DEF: %d", p->defense); - DrawText(def_text, 420, MAP_HEIGHT * TILE_SIZE + 10, 20, BLUE); + 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, 530, MAP_HEIGHT * TILE_SIZE + 10, 16, GREEN); + DrawText(inv_text, 440, MAP_HEIGHT * TILE_SIZE + 10, 16, GREEN); - // Show first item in inventory if any - if (p->inventory_count > 0 && !p->inventory[0].picked_up) { - const char *item_name = ""; - switch (p->inventory[0].type) { - case ITEM_POTION: - item_name = "Potion"; - break; - case ITEM_WEAPON: - item_name = "Weapon"; - break; - case ITEM_ARMOR: - item_name = "Armor"; - break; - default: - item_name = "?"; - break; - } - char first_item[48]; - snprintf(first_item, sizeof(first_item), "[%s +%d]", item_name, p->inventory[0].power); - DrawText(first_item, 10, MAP_HEIGHT * TILE_SIZE + 35, 14, LIGHTGRAY); + // Equipment display - second row + int eq_y = MAP_HEIGHT * TILE_SIZE + 10; + + // Weapon slot + if (p->has_weapon) { + char weapon_text[48]; + snprintf(weapon_text, sizeof(weapon_text), "Wpn:[%s +%d]", item_get_name(&p->equipped_weapon), + p->equipped_weapon.power); + DrawText(weapon_text, 10, eq_y + 25, 14, YELLOW); + } else { + DrawText("Wpn:---", 10, eq_y + 25, 14, DARKGRAY); + } + + // Armor slot + if (p->has_armor) { + char armor_text[48]; + snprintf(armor_text, sizeof(armor_text), "Arm:[%s +%d]", item_get_name(&p->equipped_armor), + p->equipped_armor.power); + DrawText(armor_text, 170, eq_y + 25, 14, BLUE); + } else { + DrawText("Arm:---", 170, eq_y + 25, 14, DARKGRAY); } // Controls hint - DrawText("WASD: Move | U: Use Item | Q: Quit", 280, MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY); + DrawText("WASD:Move G:Pickup I:Inventory U:Use E:Equip Q:Quit", 330, eq_y + 25, 12, GRAY); +} + +void render_inventory_overlay(const Player *p, int selected) { + // Semi-transparent overlay + Rectangle overlay = {(float)(SCREEN_WIDTH / 2 - 200), 80, 400, 320}; + DrawRectangleRec(overlay, (Color){20, 20, 20, 240}); + DrawRectangleLines((int)overlay.x, (int)overlay.y, (int)overlay.width, (int)overlay.height, GOLD); + + // Title + const char *title = "INVENTORY"; + int title_w = MeasureText(title, 20); + DrawText(title, overlay.x + (overlay.width - title_w) / 2, overlay.y + 15, 20, GOLD); + + // 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 + char slot_text[64]; + for (int i = 0; i < MAX_INVENTORY; i++) { + int y_pos = overlay.y + 75 + (i * 22); + + if (i < p->inventory_count && !p->inventory[i].picked_up) { + // Occupied slot + const Item *item = &p->inventory[i]; + + // Highlight selected + if (i == selected) { + DrawRectangle((int)overlay.x + 10, y_pos - 2, (int)overlay.width - 20, 20, (Color){60, 60, 60, 255}); + } + + // Slot number + snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); + DrawText(slot_text, overlay.x + 20, y_pos, 14, WHITE); + + // Item name + const char *name = item_get_name(item); + DrawText(name, overlay.x + 50, y_pos, 14, WHITE); + + // Type + const char *type = (item->type == ITEM_POTION) ? "Potion" : (item->type == ITEM_WEAPON) ? "Weapon" : "Armor"; + 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 + snprintf(slot_text, sizeof(slot_text), "+%d", item->power); + DrawText(slot_text, overlay.x + 210, y_pos, 14, YELLOW); + + // Action hint + if (item->type == ITEM_POTION) { + DrawText("[U]se", overlay.x + 280, y_pos, 14, GREEN); + } else { + DrawText("[E]quip", overlay.x + 280, y_pos, 14, GOLD); + } + } else { + // Empty slot + snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); + DrawText(slot_text, overlay.x + 20, y_pos, 14, (Color){60, 60, 60, 255}); + } + } + + // Instructions + const char *hint = "WASD: Select | ENTER/U: Use | E: Equip | ESC: Close"; + int hint_w = MeasureText(hint, 12); + DrawText(hint, overlay.x + (overlay.width - hint_w) / 2, overlay.y + 290, 12, GRAY); } void render_game_over(void) { diff --git a/src/render.h b/src/render.h index 6ec6469..a172828 100644 --- a/src/render.h +++ b/src/render.h @@ -18,6 +18,9 @@ void render_items(const Item *items, int count); // Render UI overlay void render_ui(const Player *p); +// Render inventory selection overlay +void render_inventory_overlay(const Player *p, int selected); + // Render game over screen void render_game_over(void);