cntrl/src/render/ui.cpp

298 lines
15 KiB
C++

#include "render/ui.hpp"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include "raylib.h"
#include "raygui.h"
#include "render/ui_font.hpp"
#include "render/clay_ui.hpp"
namespace {
void draw_bar(float x, float y, float w, float h, float frac, Color fill, const char* label) {
frac = std::max(0.0f, std::min(1.0f, frac));
DrawRectangle(x, y, w, h, Color{20, 20, 22, 210});
DrawRectangle(x, y, w * frac, h, fill);
DrawRectangleLines(x, y, w, h, Color{210, 210, 215, 60});
if (label != nullptr) ui_font::draw_text(label, x + 8, y + h / 2 - 8, 16, Color{235, 232, 224, 235});
}
void draw_hud(const Game& game) {
// Top left: agent vitals.
char hp_label[64];
std::snprintf(hp_label, sizeof(hp_label), "SECURITY AGENT HP %d / %d",
static_cast<int>(game.player.hp), static_cast<int>(game.player.stats.max_hp));
draw_bar(20, 20, 300, 22, game.player.hp / game.player.stats.max_hp,
Color{193, 18, 31, 235}, hp_label);
// Top right: the corruption status table. The shift clock is exposed for
// now — it is the RoR2 clock the whole difficulty curve hangs off, and
// without it the tier jumps read as arbitrary.
const int tier = game.corruption.tier();
const Color tier_color = Color{
static_cast<unsigned char>(std::min(255, 120 + tier * 24)),
static_cast<unsigned char>(std::max(0, 180 - tier * 32)),
static_cast<unsigned char>(std::max(0, 60 - tier * 12)), 245};
const int panel_w = 300;
const int panel_x = GetScreenWidth() - 20 - panel_w;
DrawRectangle(panel_x, 16, panel_w, 124, Color{14, 14, 16, 215});
DrawRectangleLines(panel_x, 16, panel_w, 124, Color{245, 197, 24, 120});
auto clock = [](float seconds, char* out, std::size_t size) {
const int total = static_cast<int>(std::max(0.0f, seconds));
std::snprintf(out, size, "%02d:%02d", total / 60, total % 60);
};
auto row = [panel_x, panel_w](const char* label, const char* value, int y, Color color) {
ui_font::draw_text(label, panel_x + 12, y, 14, Color{170, 168, 162, 220});
const int value_w = ui_font::measure_text(value, 15);
ui_font::draw_text(value, panel_x + panel_w - 12 - value_w, y, 15, color);
};
char shift[16], next_tier[16], cascade[16];
clock(game.run_time, shift, sizeof(shift));
clock(game.corruption.seconds_to_next_tier(), next_tier, sizeof(next_tier));
clock(game.corruption.seconds_to_max_tier(), cascade, sizeof(cascade));
int y = 24;
ui_font::draw_text("CONTAINMENT STATUS", panel_x + 12, y, 12, Color{200, 198, 192, 210});
y += 18;
ui_font::draw_text(game.corruption.tier_name(), panel_x + 12, y, 21, tier_color);
y += 28;
row("SHIFT TIME", shift, y, Color{235, 232, 224, 245}); y += 19;
row("NEXT TIER", next_tier, y, Color{200, 198, 192, 230}); y += 19;
row("CASCADE IN", cascade, y, Color{200, 198, 192, 230}); y += 22;
draw_bar(static_cast<float>(panel_x + 12), static_cast<float>(y),
static_cast<float>(panel_w - 24), 10.0f,
game.corruption.tier_progress(), tier_color, nullptr);
// Floor 01 flow: current room + the key/door objective.
{
const int panel_w = 470;
const int px = GetScreenWidth() / 2 - panel_w / 2;
DrawRectangle(px, 16, panel_w, 62, Color{17, 17, 19, 205});
DrawRectangleLines(px, 16, panel_w, 62, Color{245, 197, 24, 120});
char room_line[96];
std::snprintf(room_line, sizeof(room_line), "FLOOR %02d // %s",
game.floor_number, game.floor.room_name(game.player.pos));
const int rw = ui_font::measure_text(room_line, 17);
ui_font::draw_text(room_line, px + panel_w / 2 - rw / 2, 22, 17,
Color{245, 197, 24, 245});
const char* objective = game.has_key
? "OBJECTIVE: REACH THE THRESHOLD STAIRWELL"
: "OBJECTIVE: RECOVER THE THRESHOLD KEY (SILOS)";
const int ow = ui_font::measure_text(objective, 16);
ui_font::draw_text(objective, px + panel_w / 2 - ow / 2, 46, 16,
Color{232, 228, 220, 235});
if (game.floor_banner > 0.0f) {
const char* banner = game.has_key
? "THRESHOLD KEY ACQUIRED" : "FLOOR CLEARED — THE HOUSE NOTICED";
const int bw = ui_font::measure_text(banner, 18);
ui_font::draw_text(banner, GetScreenWidth() / 2 - bw / 2, 108, 18,
Color{245, 197, 24, 250});
}
}
if (game.side_banner > 0.0f && game.side_banner_text != nullptr) {
const int bw = ui_font::measure_text(game.side_banner_text, 16);
ui_font::draw_text(game.side_banner_text, GetScreenWidth() / 2 - bw / 2, 132, 16,
Color{160, 220, 200, 250});
}
// Kills, XP level and the diegetic clearance stamp (which opens doors).
char misc[80];
std::snprintf(misc, sizeof(misc), "KILLS %d LV %d CLEARANCE %02d",
game.kills, game.level, game.clearance);
ui_font::draw_text(misc, 20, 52, 18, Color{200, 198, 192, 235});
// Ritual in progress — mini-game indicator (the player understands the
// ritual starts when it appears). MVP: the TV ritual; complex: the
// lightswitch — catch the bait around the switch (§2.3).
if (game.ritual_system.active()) {
char rit[160];
if (game.ritual_system.type() == RitualType::Lightswitch) {
std::snprintf(rit, sizeof(rit),
"RITUAL IN PROGRESS — LIGHT SWITCH — CATCHING (%d/%d)",
game.ritual_system.lightswitch_caught(),
game.ritual_system.lightswitch_required());
} else if (game.ritual_system.type() == RitualType::Projector) {
std::snprintf(rit, sizeof(rit),
"RITUAL IN PROGRESS — PROJECTOR / BRIEFING REEL (%.0f s)",
std::max(0.0f, game.ritual_system.remaining()));
} else {
std::snprintf(rit, sizeof(rit),
"RITUAL IN PROGRESS — The TV must flash (%.0f s)",
std::max(0.0f, game.ritual_system.remaining()));
}
ui_font::draw_text(rit, 20, 70.0f, 16, Color{245, 197, 24, 255});
}
if (game.ritual_system.complete()) {
const char* done =
game.ritual_system.type() == RitualType::Lightswitch
? "RITUAL COMPLETE — Fluorescents revived"
: game.ritual_system.type() == RitualType::Projector
? "RITUAL COMPLETE — PROJECTOR CLAIMED: briefing reel acquired"
: "RITUAL COMPLETE — The TV is live";
ui_font::draw_text(done, 20, 70.0f, 16, Color{245, 197, 24, 255});
}
// Owned OoP loadout (left column).
float oy = 84.0f;
ui_font::draw_text("OoP LOADOUT", 20, oy, 16, Color{245, 197, 24, 235});
oy += 22.0f;
for (const WeaponInstance& w : game.weapon_system.weapons) {
const WeaponDef& def = weapon_def(w.type);
char line[96];
if (w.evolved) {
std::snprintf(line, sizeof(line), "%s [ALTERED]", def.evolution_name);
ui_font::draw_text(line, 20, oy, 17, Color{245, 197, 24, 245});
} else {
std::snprintf(line, sizeof(line), "%s lv %d", def.name, w.level);
ui_font::draw_text(line, 20, oy, 17, Color{200, 198, 192, 235});
}
oy += 21.0f;
}
// Pending offers (real-time progression).
if (!game.pending_offers.empty()) {
float oy2 = oy + 6.0f;
char offer_line[128];
std::snprintf(offer_line, sizeof(offer_line), "OFFERS PENDING: %d", static_cast<int>(game.pending_offers.size()));
ui_font::draw_text(offer_line, 20, oy2, 16, Color{245, 197, 24, 245});
oy2 += 18.0f;
for (const Offer& o : game.pending_offers) {
std::snprintf(offer_line, sizeof(offer_line), "- %s", o.name);
ui_font::draw_text(offer_line, 20, oy2, 15, Color{200, 198, 192, 220});
oy2 += 16.0f;
}
}
// Bottom: XP bar.
const float xp_w = 640.0f;
draw_bar(GetScreenWidth() / 2.0f - xp_w / 2.0f, GetScreenHeight() - 44, xp_w, 18,
game.xp / game.xp_to_next, Color{245, 197, 24, 235}, "RESONANCE SHARDS");
// Dev readout (pre-alpha): frame rate + entity counts + director bank.
char debug[160];
std::snprintf(debug, sizeof(debug),
"FPS %d | ENEMIES %d | PROJECTILES %d | SHARDS %d | DIRECTOR %.0f "
"| WORLD OBJECTS %d | BUFFS %d",
GetFPS(), game.enemy_system.alive_count(),
static_cast<int>(game.weapon_system.projectiles.size()),
static_cast<int>(game.pickup_system.shards.size()),
static_cast<double>(game.director.credits()),
game.buff_system.world_object_count(),
game.buff_system.buff_count());
ui_font::draw_text(debug, 20, GetScreenHeight() - 22, 16, Color{150, 148, 144, 210});
}
void draw_gameover(const Game& game) {
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Color{40, 6, 8, 200});
const char* title = "CONTAINMENT FAILURE";
ui_font::draw_text(title, GetScreenWidth() / 2 - ui_font::measure_text(title, 42) / 2, GetScreenHeight() / 2 - 140,
42, Color{193, 18, 31, 255});
const int total = static_cast<int>(game.run_time);
char stats[256];
std::snprintf(stats, sizeof(stats),
"SHIFT TIME SURVIVED %02d:%02d\nKILLS CONFIRMED %d\nCLEARANCE LEVEL %d\nFINAL CORRUPTION %s",
total / 60, total % 60, game.kills, game.level, game.corruption.tier_name());
ui_font::draw_text(stats, GetScreenWidth() / 2 - 190, GetScreenHeight() / 2 - 60, 22, Color{232, 228, 216, 255});
const char* hint = "PRESS [R] TO RE-ENTER THE HOUSE";
ui_font::draw_text(hint, GetScreenWidth() / 2 - ui_font::measure_text(hint, 24) / 2, GetScreenHeight() / 2 + 110,
24, Color{245, 197, 24, 255});
}
} // namespace
void ui::draw_all(const Game& game) {
if (game.state == GameState::Title) return;
draw_hud(game);
if (game.state == GameState::GameOver) draw_gameover(game);
}
void ui::draw_camera_dev(CameraTuning& tuning) {
if (IsKeyPressed(KEY_F9)) tuning.panel = !tuning.panel;
if (!tuning.panel) {
ui_font::draw_text("F9 CAM DEV", GetScreenWidth() - 130, GetScreenHeight() - 26,
14, Color{160, 158, 152, 220});
return;
}
const float w = 300.0f, h = 150.0f;
const float x = static_cast<float>(GetScreenWidth()) - w - 20.0f;
const float y = static_cast<float>(GetScreenHeight()) - h - 20.0f;
DrawRectangle(x, y, w, h, Color{14, 16, 18, 225});
DrawRectangleLines(x, y, w, h, Color{120, 118, 112, 160});
ui_font::draw_text("CAMERA DEV [F9 HIDE]", x + 10, y + 8, 14,
Color{245, 197, 24, 245});
const Rectangle r_fov = {x + 90.0f, y + 34.0f, 150.0f, 18.0f};
const Rectangle r_height = {x + 90.0f, y + 62.0f, 150.0f, 18.0f};
const Rectangle r_forward = {x + 90.0f, y + 90.0f, 150.0f, 18.0f};
ui_font::draw_text("FOV", x + 10, y + 36, 14, Color{220, 218, 210, 230});
GuiSlider(r_fov, nullptr, TextFormat("%3.0f", tuning.fov), &tuning.fov, 30.0f, 80.0f);
ui_font::draw_text("HEIGHT", x + 10, y + 64, 14, Color{220, 218, 210, 230});
GuiSlider(r_height, nullptr, TextFormat("%4.1f", tuning.height), &tuning.height, 5.0f, 45.0f);
ui_font::draw_text("FORWARD", x + 10, y + 92, 14, Color{220, 218, 210, 230});
GuiSlider(r_forward, nullptr, TextFormat("%4.1f", tuning.forward), &tuning.forward, 2.0f, 40.0f);
ui_font::draw_text("[ ] FOV - = HEIGHT , . FORWARD", x + 10, y + 124, 12,
Color{160, 158, 152, 220});
if (IsKeyPressed(KEY_LEFT_BRACKET)) tuning.fov = std::max(30.0f, tuning.fov - 2.0f);
if (IsKeyPressed(KEY_RIGHT_BRACKET)) tuning.fov = std::min(80.0f, tuning.fov + 2.0f);
if (IsKeyPressed(KEY_MINUS)) tuning.height = std::max(5.0f, tuning.height - 1.0f);
if (IsKeyPressed(KEY_EQUAL)) tuning.height = std::min(45.0f, tuning.height + 1.0f);
if (IsKeyPressed(KEY_COMMA)) tuning.forward = std::max(2.0f, tuning.forward - 1.0f);
if (IsKeyPressed(KEY_PERIOD)) tuning.forward = std::min(40.0f, tuning.forward + 1.0f);
}
void ui::draw_menu(Game& game, int gamepad) {
bool start = false;
CLAY(CLAY_ID("MenuRoot"), {
.layout = { .sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0) },
.padding = CLAY_PADDING_ALL(64),
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_TOP_TO_BOTTOM },
.backgroundColor = clay_ui::theme::kDim }) {
CLAY(CLAY_ID("MenuPanel"), {
.layout = { .sizing = { CLAY_SIZING_FIXED(520), CLAY_SIZING_FIT(0) },
.padding = CLAY_PADDING_ALL(40),
.childGap = 16,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_TOP_TO_BOTTOM },
.backgroundColor = clay_ui::theme::kPanel,
.cornerRadius = CLAY_CORNER_RADIUS(16),
.border = { .color = clay_ui::theme::kBorder, .width = CLAY_BORDER_ALL(2) } }) {
clay_ui::Label(clay_ui::S("FBC: NIGHT SHIFT"), 48, clay_ui::theme::kTitle,
CLAY_TEXT_ALIGN_CENTER, clay_ui::kFontTitle);
clay_ui::Label(clay_ui::S("CONTAINMENT PROTOCOL - PRE-ALPHA"), 16,
clay_ui::theme::kTextDim, CLAY_TEXT_ALIGN_CENTER);
CLAY(CLAY_ID("StartBtn"), {
.layout = { .sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_FIXED(52) },
.padding = CLAY_PADDING_ALL(16),
.childGap = 0,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER } },
.backgroundColor = Clay_Hovered() ? clay_ui::theme::kBorderFocus
: clay_ui::theme::kPanelDeep,
.cornerRadius = CLAY_CORNER_RADIUS(8),
.border = { .color = clay_ui::theme::kBorder, .width = CLAY_BORDER_ALL(1) } }) {
if (Clay_Hovered() && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) start = true;
clay_ui::Label(clay_ui::S("ENTER THE HOUSE"), 20, clay_ui::theme::kText,
CLAY_TEXT_ALIGN_CENTER);
}
}
};
if (IsKeyPressed(KEY_ENTER)) start = true;
if (gamepad >= 0 && (IsGamepadButtonPressed(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
|| IsGamepadButtonPressed(gamepad, GAMEPAD_BUTTON_MIDDLE_RIGHT))) {
start = true;
}
if (start) game.reset();
}