#include "render.h" #include "common.h" #include "items.h" #include "raylib.h" #include "settings.h" #include #include #include 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}; switch (map->tiles[y][x]) { case TILE_WALL: DrawRectangleRec(rect, DARKGRAY); break; case TILE_FLOOR: DrawRectangleRec(rect, BLACK); 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); break; } } } } void render_player(const Player *p) { Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; DrawRectangleRec(rect, BLUE); } void render_enemies(const Enemy *enemies, int count) { for (int i = 0; i < count; i++) { if (!enemies[i].alive) continue; Rectangle rect = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; // Different colors based on enemy type Color enemy_color; switch (enemies[i].type) { case ENEMY_GOBLIN: enemy_color = (Color){150, 50, 50, 255}; // dark red break; case ENEMY_SKELETON: enemy_color = (Color){200, 200, 200, 255}; // light gray break; case ENEMY_ORC: enemy_color = (Color){50, 150, 50, 255}; // dark green break; default: enemy_color = RED; break; } DrawRectangleRec(rect, enemy_color); // Draw hp bar above enemy, color-coded by health remaining int hp_pixels = (enemies[i].hp * TILE_SIZE) / enemies[i].max_hp; if (hp_pixels > 0) { float hp_ratio = (float)enemies[i].hp / (float)enemies[i].max_hp; Color bar_color; if (hp_ratio > 0.5f) bar_color = (Color){60, 180, 60, 255}; // green else if (hp_ratio > 0.25f) bar_color = (Color){200, 180, 40, 255}; // yellow else bar_color = (Color){200, 60, 60, 255}; // red Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE - 4), (float)hp_pixels, 3}; DrawRectangleRec(hp_bar, bar_color); } } } void render_items(const Item *items, int count) { for (int i = 0; i < count; i++) { if (items[i].picked_up) continue; Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; // Different colors based on item type Color item_color; switch (items[i].type) { case ITEM_POTION: item_color = (Color){255, 100, 100, 255}; // red/pink break; case ITEM_WEAPON: item_color = (Color){255, 255, 100, 255}; // yellow break; case ITEM_ARMOR: item_color = (Color){100, 100, 255, 255}; // blue break; default: item_color = GREEN; break; } DrawRectangleRec(rect, item_color); } } void render_ui(const Player *p) { // UI background bar (taller for two rows) Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH, 60}; DrawRectangleRec(ui_bg, (Color){15, 15, 15, 255}); // Draw dividing line DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, (Color){50, 50, 50, 255}); // 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]; snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp); int hp_text_w = MeasureText(hp_text, 14); DrawText(hp_text, bar_x + (bar_width - hp_text_w) / 2, bar_y + 2, 14, WHITE); // Status effect indicators next to HP bar int effect_x = bar_x + bar_width + 5; for (int i = 0; i < p->effect_count && i < MAX_EFFECTS; i++) { Color eff_color; const char *eff_label = ""; switch (p->effects[i].type) { case EFFECT_POISON: eff_color = (Color){50, 200, 50, 255}; eff_label = "PSN"; break; case EFFECT_BLEED: eff_color = (Color){200, 50, 50, 255}; eff_label = "BLD"; break; case EFFECT_STUN: eff_color = (Color){200, 200, 50, 255}; eff_label = "STN"; break; case EFFECT_WEAKEN: eff_color = (Color){120, 120, 120, 255}; eff_label = "WKN"; break; case EFFECT_BURN: eff_color = (Color){230, 130, 30, 255}; eff_label = "BRN"; break; default: continue; } if (p->effects[i].duration > 0) { char eff_text[16]; snprintf(eff_text, sizeof(eff_text), "%s%d", eff_label, p->effects[i].duration); DrawText(eff_text, effect_x, bar_y, 12, eff_color); effect_x += 40; } } // Stats row 1: Floor, ATK, DEF, Inv int stats_x_start = (effect_x > bar_x + bar_width + 15) ? effect_x + 10 : bar_x + bar_width + 15; int stats_y = bar_y; DrawText("F1", stats_x_start, stats_y, 14, WHITE); DrawText("ATK", stats_x_start + 35, stats_y, 14, YELLOW); DrawText("DEF", stats_x_start + 85, stats_y, 14, BLUE); DrawText("INV", stats_x_start + 130, stats_y, 14, GREEN); // Row 2: equipment slots and controls int row2_y = stats_y + 24; // Equipment (left side of row 2) if (p->has_weapon) { char weapon_text[64]; snprintf(weapon_text, sizeof(weapon_text), "Wpn:%s +%d [%s]", item_get_name(&p->equipped_weapon), p->equipped_weapon.power, dmg_class_get_short(p->equipped_weapon.dmg_class)); DrawText(weapon_text, 10, row2_y, 12, YELLOW); } else { DrawText("Wpn:--- [IMP]", 10, row2_y, 12, (Color){60, 60, 60, 255}); } 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, 150, row2_y, 12, BLUE); } else { DrawText("Arm:---", 150, row2_y, 12, (Color){60, 60, 60, 255}); } // Controls hint (right side) 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) { // Overlay dimensions int ov_width = 360; int ov_height = 300; 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 const char *title = "INVENTORY"; int title_w = MeasureText(title, 24); DrawText(title, overlay.x + (overlay.width - title_w) / 2, overlay.y + 12, 24, WHITE); // Draw each inventory slot char slot_text[64]; int row_height = 26; int start_y = overlay.y + 50; for (int i = 0; i < MAX_INVENTORY; i++) { int y_pos = start_y + (i * row_height); if (i < p->inventory_count && !p->inventory[i].picked_up) { const Item *item = &p->inventory[i]; // 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, (Color){180, 160, 80, 255}); } // Slot number snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){80, 80, 80, 255}); // Item name const char *name = item_get_name(item); Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255} : (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255} : (Color){140, 140, 255, 255}; DrawText(name, overlay.x + 45, y_pos + 4, 14, name_color); // Power snprintf(slot_text, sizeof(slot_text), "+%d", item->power); DrawText(slot_text, overlay.x + 150, y_pos + 4, 14, YELLOW); // Action if (item->type == ITEM_POTION) { DrawText("[U]se", overlay.x + 200, y_pos + 4, 14, GREEN); } else { DrawText("[E]quip [D]rop", overlay.x + 200, y_pos + 4, 14, GOLD); } } else { // Empty slot snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){40, 40, 40, 255}); } } // Instructions at bottom const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close"; int hint_w = MeasureText(hint, 12); DrawText(hint, overlay.x + (overlay.width - hint_w) / 2, overlay.y + overlay.height - 22, 12, (Color){65, 65, 65, 255}); } static Color label_color(FloatingText *ft, int alpha) { if (ft->label[0] == '\0') return (Color){255, 100, 100, alpha}; // numeric damage default if (strcmp(ft->label, "DODGE") == 0) return (Color){160, 160, 160, alpha}; if (strcmp(ft->label, "BLOCK") == 0) return (Color){80, 130, 220, alpha}; if (strcmp(ft->label, "CRIT!") == 0) return (Color){255, 200, 50, alpha}; if (strcmp(ft->label, "SLAIN") == 0) return (Color){220, 50, 50, alpha}; // Proc label, color driven by effect_type stored in the struct switch (ft->effect_type) { case EFFECT_POISON: return (Color){50, 200, 50, alpha}; case EFFECT_BLEED: return (Color){200, 50, 50, alpha}; case EFFECT_BURN: return (Color){230, 130, 30, alpha}; case EFFECT_STUN: return (Color){200, 200, 50, alpha}; case EFFECT_WEAKEN: return (Color){120, 120, 120, alpha}; default: return (Color){200, 200, 200, alpha}; } } 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; int a = (int)(255 * alpha); if (texts[i].label[0] != '\0') { // Label text (DODGE, BLOCK, CRIT!, proc name, SLAIN) int font_size = (texts[i].label[0] == 'C') ? 16 : 14; // CRIT! slightly larger Color color = label_color(&texts[i], a); int text_w = MeasureText(texts[i].label, font_size); DrawText(texts[i].label, x - text_w / 2, y, font_size, color); } else { // Numeric damage Color color = texts[i].is_critical ? (Color){255, 200, 50, a} : (Color){255, 100, 100, a}; 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_end_screen(int is_victory, int kills, int items, int damage_dealt, int damage_taken, int crits, int times_hit, int potions, int floors, int turns, int score) { // Semi-transparent overlay Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT}; DrawRectangleRec(overlay, (Color){0, 0, 0, 210}); // Title const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER"; int title_font_size = is_victory ? 60 : 60; Color title_color = is_victory ? GOLD : RED; int title_width = MeasureText(title, title_font_size); DrawText(title, (SCREEN_WIDTH - title_width) / 2, 30, title_font_size, title_color); // Stats box int box_x = SCREEN_WIDTH / 2 - 200; int box_y = 110; int box_w = 400; int box_h = 320; DrawRectangle(box_x, box_y, box_w, box_h, (Color){20, 20, 20, 240}); DrawRectangleLines(box_x, box_y, box_w, box_h, (Color){100, 100, 100, 255}); // Stats content char line[64]; int col1_x = box_x + 20; int col2_x = box_x + 210; int row_y = box_y + 20; int line_height = 24; Color label_color = LIGHTGRAY; Color value_color = WHITE; // Column 1 DrawText("Kills:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", kills); DrawText(line, col1_x + 80, row_y, 18, value_color); row_y += line_height; DrawText("Items:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", items); DrawText(line, col1_x + 80, row_y, 18, value_color); row_y += line_height; DrawText("Damage Dealt:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", damage_dealt); DrawText(line, col1_x + 140, row_y, 18, value_color); row_y += line_height; DrawText("Damage Taken:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", damage_taken); DrawText(line, col1_x + 140, row_y, 18, value_color); row_y += line_height; DrawText("Crits:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", crits); DrawText(line, col1_x + 80, row_y, 18, value_color); row_y += line_height; DrawText("Times Hit:", col1_x, row_y, 18, label_color); snprintf(line, sizeof(line), "%d", times_hit); DrawText(line, col1_x + 80, row_y, 18, value_color); row_y += line_height; // Column 2 int col2_row_y = box_y + 20; DrawText("Potions:", col2_x, col2_row_y, 18, label_color); snprintf(line, sizeof(line), "%d", potions); DrawText(line, col2_x + 80, col2_row_y, 18, value_color); col2_row_y += line_height; DrawText("Floors:", col2_x, col2_row_y, 18, label_color); snprintf(line, sizeof(line), "%d", floors); DrawText(line, col2_x + 80, col2_row_y, 18, value_color); col2_row_y += line_height; DrawText("Turns:", col2_x, col2_row_y, 18, label_color); snprintf(line, sizeof(line), "%d", turns); DrawText(line, col2_x + 80, col2_row_y, 18, value_color); col2_row_y += line_height; // Score: placed below the last row of the longer column (6 items, row_y is already there) row_y += 10; DrawText("SCORE:", col1_x, row_y, 22, GOLD); snprintf(line, sizeof(line), "%d", score); DrawText(line, col1_x + 90, row_y, 22, GOLD); if (is_victory) { const char *subtitle = "Press R to play again or Q to quit"; int sub_width = MeasureText(subtitle, 20); DrawText(subtitle, (SCREEN_WIDTH - sub_width) / 2, SCREEN_HEIGHT - 50, 20, LIGHTGRAY); } else { const char *subtitle = "Press R to restart or Q to quit"; int sub_width = MeasureText(subtitle, 20); DrawText(subtitle, (SCREEN_WIDTH - sub_width) / 2, SCREEN_HEIGHT - 50, 20, LIGHTGRAY); } } void render_message(const char *message) { if (message == NULL) return; const int font_size = 20; const int line_height = font_size + 4; const int padding_x = 20; const int padding_y = 15; const int max_box_width = (int)(SCREEN_WIDTH * 0.75f); const int max_line_width = max_box_width - (padding_x * 2); // Calculate line breaks by iterating through message int line_count = 1; int current_line_width = 0; int longest_line_width = 0; const char *msg_ptr = message; while (*msg_ptr && line_count <= 10) { // Estimate character width (average ~10px for 20pt font) int char_width = 10; current_line_width += char_width; if (current_line_width > max_line_width && *msg_ptr == ' ') { if (current_line_width > longest_line_width) longest_line_width = current_line_width; line_count++; current_line_width = 0; } msg_ptr++; } if (current_line_width > longest_line_width) longest_line_width = current_line_width; // Measure full message int total_msg_width = MeasureText(message, font_size); int box_width = total_msg_width + (padding_x * 2); // If message is too long, use wrapped width if (box_width > max_box_width) { box_width = max_box_width; } // Ensure minimum width if (box_width < 200) box_width = 200; // Calculate box height based on line count int box_height = (line_count * line_height) + (padding_y * 2); // Center the box float box_x = (SCREEN_WIDTH - box_width) / 2.0f; float box_y = (SCREEN_HEIGHT - box_height) / 2.0f; // Draw message box background Rectangle msg_bg = {box_x, box_y, (float)box_width, (float)box_height}; 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, (Color){180, 180, 180, 255}); // Draw text centered int text_x = (SCREEN_WIDTH - total_msg_width) / 2; int text_y = (SCREEN_HEIGHT - font_size) / 2; // For wrapped text, draw at box center with padding if (line_count > 1) { text_x = (int)box_x + padding_x; text_y = (int)box_y + padding_y; } DrawText(message, text_x, text_y, font_size, WHITE); }