Files
thermal-snake/internal/arcade/view.go
T
2026-11-27 19:04:23 +03:30

278 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package arcade
import (
"fmt"
"strings"
tea "charm.land/bubbletea/v2"
lg "charm.land/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"thermal-snake/internal/game"
"thermal-snake/internal/scores"
)
var (
background = lg.Color("#160F18")
panel = lg.Color("#211322")
line = lg.Color("#805570")
text = lg.NewStyle().Foreground(lg.Color("#F8E8E1"))
muted = lg.NewStyle().Foreground(lg.Color("#C0A3B1"))
subtle = lg.NewStyle().Foreground(line)
accent = lg.NewStyle().Bold(true).Foreground(lg.Color("#FF9C72"))
bright = lg.NewStyle().Bold(true).Foreground(lg.Color("#FFE6A1"))
hot = lg.NewStyle().Bold(true).Foreground(lg.Color("#241322")).Background(lg.Color("#FFC177"))
keyStyle = lg.NewStyle().Bold(true).Foreground(lg.Color("#FFE1CC")).Background(lg.Color("#512D46"))
metric = lg.NewStyle().Bold(true).Foreground(lg.Color("#FFF3E7"))
rowStyle = text.Background(panel)
selfStyle = lg.NewStyle().Bold(true).Foreground(lg.Color("#FFE6A1")).Background(lg.Color("#4D2C44"))
)
func (m *Model) View() tea.View {
view := tea.NewView(m.render())
view.AltScreen = true
view.WindowTitle = "Thermal Snake"
view.BackgroundColor = background
view.ForegroundColor = lg.Color("#F8E8E1")
return view
}
func (m *Model) render() string {
if m.tooSmall() {
lines := []string{
accent.Render("◈ THERMAL SNAKE"),
"",
text.Render(fmt.Sprintf("Resize to %d × %d to play.", minWidth, minHeight)),
control("Q", "Exit"),
}
return position(lines, min(30, m.width), m.width, m.height)
}
width := minWidth
wide := m.width >= wideWidth
if wide {
width = wideWidth
}
wordmark := accent.Render("◈") + metric.Render(" T H E R M A L S N A K E")
lines := []string{
pair(wordmark, muted.Render(m.pilot.Callsign), width),
m.subtitle(),
subtle.Render(strings.Repeat("─", width)),
"",
}
if m.screen == records {
lines = append(lines, m.recordsView(width)...)
} else {
field := m.field()
stats := m.stats()
if wide {
rail := m.rail()
for i, row := range field {
if i < len(rail) {
row += " " + rail[i]
}
lines = append(lines, row)
}
lines = append(lines, "")
} else {
for _, row := range field {
lines = append(lines, " "+row)
}
lines = append(lines, stats)
}
lines = append(lines, m.controls(wide)...)
}
return position(lines, width, m.width, m.height)
}
func (m *Model) subtitle() string {
switch m.screen {
case lobby:
return muted.Render("INFRARED ARCADE / CLASSIC")
case records:
ranks, runs := muted, accent
if !m.historyTab {
ranks, runs = accent, muted
}
return ranks.Render("L RANKINGS") + subtle.Render(" / ") + runs.Render("H MY RUNS")
case debrief:
return accent.Render("RUN DEBRIEF")
default:
return muted.Render("CHAMBER") + subtle.Render(" / ") + accent.Render(m.phaseLabel())
}
}
func (m *Model) phaseLabel() string {
switch m.screen {
case warmup:
return "CALIBRATING"
case paused:
return "PAUSED"
default:
return "LIVE"
}
}
func (m *Model) rail() []string {
if m.screen == lobby {
return []string{
accent.Render("THE CHAMBER"),
"",
text.Render("Collect heat to grow."),
text.Render("Avoid the walls and coil."),
"",
bright.Render("◆") + muted.Render(" HEAT +100"),
text.Render("▸") + muted.Render(" SNAKE HEAD"),
"",
subtle.Render(strings.Repeat("─", 27)),
"",
muted.Render("PERSONAL BEST"),
metric.Render(prettyBest(m.pilot.Best)),
muted.Render(completionCount(m.pilot.Total)),
}
}
lines := []string{
muted.Render("SCORE"),
bright.Render(prettyScore(m.run.Score())),
"",
muted.Render("PERSONAL BEST"),
metric.Render(prettyBest(m.pilot.Best)),
"",
subtle.Render(strings.Repeat("─", 27)),
}
if m.screen == debrief {
status, style := "RUN LOGGED", muted
switch {
case m.saveFailed:
status, style = "SAVE FAILED", accent
case m.personalBest:
status, style = "NEW PERSONAL BEST", bright
}
return append(lines, "", accent.Render(outcomeName(m.run.Outcome())), style.Render(status),
muted.Render(fmt.Sprintf("HEAT COLLECTED %02d", m.run.Heat())))
}
return append(lines, "",
accent.Render(fmt.Sprintf("HEAT LEVEL %02d", m.run.Level())),
heatMeter(m.run.Heat(), m.run.Level()), "",
bright.Render("◆")+muted.Render(" HEAT +100"),
text.Render("▸")+muted.Render(" SNAKE HEAD"))
}
func (m *Model) stats() string {
switch m.screen {
case lobby:
return muted.Render("Collect ◆ for +100") + subtle.Render(" / ") +
muted.Render("BEST ") + metric.Render(prettyBest(m.pilot.Best))
case debrief:
status := outcomeName(m.run.Outcome())
switch {
case m.saveFailed:
status = "SAVE FAILED"
case m.personalBest:
status = "NEW BEST"
}
return accent.Render(status) + subtle.Render(" / ") + m.scoreAndBest()
default:
return m.scoreAndBest() + subtle.Render(" / ") + accent.Render(fmt.Sprintf("LVL %02d", m.run.Level()))
}
}
func (m *Model) scoreAndBest() string {
return muted.Render("SCORE ") + bright.Render(prettyScore(m.run.Score())) +
subtle.Render(" / ") + muted.Render("BEST ") + metric.Render(prettyBest(m.pilot.Best))
}
func (m *Model) controls(wide bool) []string {
switch m.screen {
case lobby:
return []string{"", controls(control("ENTER", "Launch"), control("L", "Ranks"), control("H", "Runs"), control("Q", "Exit"))}
case warmup, playing:
return []string{"", controls(control("↑↓←→ / WASD", "Steer"), control("P", "Pause"), control("Q", "Exit"))}
case paused:
return []string{"", controls(control("P / ENTER", "Resume"), control("Q", "Exit"))}
case debrief:
if m.saveFailed {
return []string{"", control("R", "Retry save")}
}
main := controls(control("ENTER", "Replay"), control("L", "Ranks"), control("H", "Runs"), control("ESC", "Lobby"))
if wide {
main = controls(main, control("Q", "Exit"))
}
return []string{"", main}
}
return nil
}
func controls(items ...string) string { return strings.Join(items, " ") }
func control(key, label string) string {
return keyStyle.Render(" "+key+" ") + " " + text.Render(label)
}
func outcomeName(outcome game.Outcome) string {
switch outcome {
case game.Wall:
return "WALL STRIKE"
case game.Coil:
return "COIL STRIKE"
case game.Clear:
return "CHAMBER CLEAR"
default:
return "—"
}
}
func completionCount(count int) string {
if count == 1 {
return "1 completed run"
}
return fmt.Sprintf("%d completed runs", count)
}
func prettyScore(n int) string { return fmt.Sprintf("%06d", n) }
func prettyBest(best *scores.Run) string {
if best == nil {
return "—"
}
return prettyScore(best.Score)
}
func heatMeter(heat, level int) string {
if level == game.MaxLevel {
return bright.Render("▰ ▰ ▰") + muted.Render(" MAX PACE")
}
lit := heat % game.HeatPerLevel
var marks []string
for i := range game.HeatPerLevel {
mark := subtle.Render("▱")
if i < lit {
mark = bright.Render("▰")
}
marks = append(marks, mark)
}
return strings.Join(marks, " ") + muted.Render(fmt.Sprintf(" %d TO NEXT", game.HeatPerLevel-lit))
}
func position(lines []string, width, cols, rows int) string {
width = max(0, width)
rows = max(0, rows)
lines = lines[:min(rows, len(lines))]
x, y := max(0, (cols-width)/2), (rows-len(lines))/2
for i, row := range lines {
lines[i] = strings.Repeat(" ", x) + ansi.Truncate(row, width, "")
}
return strings.Repeat("\n", y) + strings.Join(lines, "\n")
}
func pair(left, right string, width int) string {
left = ansi.Truncate(left, max(0, width-2-ansi.StringWidth(right)), "")
return left + strings.Repeat(" ", max(0, width-ansi.StringWidth(left)-ansi.StringWidth(right))) + right
}
func center(s string, width int) string {
s = ansi.Truncate(s, width, "")
pad := width - ansi.StringWidth(s)
return strings.Repeat(" ", pad/2) + s + strings.Repeat(" ", pad-pad/2)
}