init
This commit is contained in:
13 files changed
+1700
No files matched your search
@@ -0,0 +1,170 @@
|
||||
package arcade
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
lg "charm.land/lipgloss/v2"
|
||||
|
||||
"thermal-snake/internal/game"
|
||||
)
|
||||
|
||||
const fieldWidth = 2 * game.Width
|
||||
|
||||
var (
|
||||
cold = lg.NewStyle().Foreground(lg.Color("#6E405D")).Background(lg.Color("#211322"))
|
||||
warm = lg.NewStyle().Foreground(lg.Color("#97536B")).Background(lg.Color("#30192B"))
|
||||
hotspot = lg.NewStyle().Foreground(lg.Color("#B06978")).Background(lg.Color("#3F2236"))
|
||||
foodStyles = [...]lg.Style{
|
||||
lg.NewStyle().Bold(true).Foreground(lg.Color("#FFE7A5")).Background(lg.Color("#5F3243")),
|
||||
lg.NewStyle().Bold(true).Foreground(lg.Color("#FFF5C4")).Background(lg.Color("#894B49")),
|
||||
}
|
||||
bodyStyles = [...]lg.Style{
|
||||
lg.NewStyle().Foreground(lg.Color("#FFAD7C")).Background(lg.Color("#49233A")),
|
||||
lg.NewStyle().Foreground(lg.Color("#FC7986")).Background(lg.Color("#49233A")),
|
||||
lg.NewStyle().Foreground(lg.Color("#D95685")).Background(lg.Color("#49233A")),
|
||||
lg.NewStyle().Foreground(lg.Color("#BB548C")).Background(lg.Color("#49233A")),
|
||||
}
|
||||
headStyle = lg.NewStyle().Bold(true).Foreground(lg.Color("#2C1727")).Background(lg.Color("#FFE7A5"))
|
||||
impactStyle = lg.NewStyle().Bold(true).Foreground(lg.Color("#291423")).Background(lg.Color("#FF836D"))
|
||||
overlayStyle = lg.NewStyle().Bold(true).Foreground(lg.Color("#FFE0A2")).Background(lg.Color("#522E46"))
|
||||
headGlyphs = [...]string{"▴", "▸", "▾", "◂"}
|
||||
)
|
||||
|
||||
// The lobby scan loops a single coil around the contours behind the live field.
|
||||
var displayCoil = game.Frame{
|
||||
Body: []game.Point{
|
||||
{X: 16, Y: 4}, {X: 15, Y: 4}, {X: 14, Y: 4}, {X: 13, Y: 4}, {X: 12, Y: 4},
|
||||
{X: 12, Y: 5}, {X: 12, Y: 6}, {X: 12, Y: 7}, {X: 11, Y: 7}, {X: 10, Y: 7},
|
||||
{X: 9, Y: 7}, {X: 8, Y: 7}, {X: 8, Y: 6}, {X: 8, Y: 5}, {X: 8, Y: 4},
|
||||
{X: 7, Y: 4}, {X: 6, Y: 4}, {X: 5, Y: 4}, {X: 4, Y: 4}, {X: 4, Y: 5},
|
||||
{X: 4, Y: 6}, {X: 4, Y: 7}, {X: 4, Y: 8}, {X: 4, Y: 9}, {X: 5, Y: 9},
|
||||
{X: 6, Y: 9}, {X: 7, Y: 9}, {X: 8, Y: 9}, {X: 9, Y: 9}, {X: 10, Y: 9},
|
||||
{X: 11, Y: 9}, {X: 12, Y: 9}, {X: 13, Y: 9}, {X: 14, Y: 9}, {X: 15, Y: 9},
|
||||
{X: 16, Y: 9}, {X: 17, Y: 9}, {X: 17, Y: 8}, {X: 17, Y: 7}, {X: 17, Y: 6},
|
||||
{X: 16, Y: 6}, {X: 15, Y: 6},
|
||||
},
|
||||
Food: game.Point{X: 19, Y: 4}, Direction: game.Right,
|
||||
}
|
||||
|
||||
func (m *Model) field() []string {
|
||||
frame := displayCoil
|
||||
label := fmt.Sprintf("FIELD / %d × %d", game.Width, game.Height)
|
||||
art := m.screen == lobby
|
||||
if art {
|
||||
label = "CHAMBER SCAN"
|
||||
} else {
|
||||
frame = m.run.Frame()
|
||||
}
|
||||
|
||||
cells := darkField
|
||||
if art {
|
||||
cells = scanField
|
||||
row := m.phase % game.Height
|
||||
cells[row] = scanLine[row]
|
||||
}
|
||||
if frame.Outcome != game.Clear {
|
||||
cells[frame.Food.Y][frame.Food.X] = foodStyles[m.phase%2].Render("◆ ")
|
||||
}
|
||||
for i, p := range frame.Body {
|
||||
shade := i * len(bodyStyles) / len(frame.Body)
|
||||
glyph := [...]string{"██", "██", "▓▓", "▒▒"}[shade]
|
||||
cells[p.Y][p.X] = bodyStyles[shade].Render(glyph)
|
||||
}
|
||||
head := frame.Body[0]
|
||||
cells[head.Y][head.X] = headStyle.Render(headGlyphs[frame.Direction] + " ")
|
||||
if frame.Outcome == game.Coil {
|
||||
cells[frame.Impact.Y][frame.Impact.X] = impactStyle.Render("××")
|
||||
}
|
||||
|
||||
switch m.screen {
|
||||
case warmup:
|
||||
box(&cells, 1, fmt.Sprintf("SEEK HEAT IN %02d", m.countdown))
|
||||
case paused:
|
||||
box(&cells, 5, "PAUSED")
|
||||
}
|
||||
|
||||
top, bottom := -1, -1
|
||||
if frame.Outcome == game.Wall {
|
||||
switch frame.Impact.Y {
|
||||
case -1:
|
||||
top = frame.Impact.X
|
||||
case game.Height:
|
||||
bottom = frame.Impact.X
|
||||
}
|
||||
}
|
||||
lines := []string{fieldEdge("╭", "╮", label, top)}
|
||||
for y, row := range cells {
|
||||
left, right := subtle.Render("│"), subtle.Render("│")
|
||||
if frame.Outcome == game.Wall && frame.Impact.Y == y {
|
||||
if frame.Impact.X < 0 {
|
||||
left = impactStyle.Render("×")
|
||||
} else {
|
||||
right = impactStyle.Render("×")
|
||||
}
|
||||
}
|
||||
lines = append(lines, left+strings.Join(row[:], "")+right)
|
||||
}
|
||||
return append(lines, fieldEdge("╰", "╯", "", bottom))
|
||||
}
|
||||
|
||||
func fieldEdge(left, right, label string, impact int) string {
|
||||
if impact >= 0 {
|
||||
cells := 2 * impact
|
||||
return subtle.Render(left+strings.Repeat("─", cells)) + impactStyle.Render("××") +
|
||||
subtle.Render(strings.Repeat("─", fieldWidth-cells-2)+right)
|
||||
}
|
||||
if label == "" {
|
||||
return subtle.Render(left + strings.Repeat("─", fieldWidth) + right)
|
||||
}
|
||||
prefix := subtle.Render(left + "─ ")
|
||||
title := " " + label + " "
|
||||
return prefix + accent.Render(title) + subtle.Render(" "+strings.Repeat("─", fieldWidth-3-lg.Width(title))+right)
|
||||
}
|
||||
|
||||
var (
|
||||
darkField = terrain(false, false)
|
||||
scanField = terrain(true, false)
|
||||
scanLine = terrain(true, true)
|
||||
)
|
||||
|
||||
func terrain(gallery, scanning bool) [game.Height][game.Width]string {
|
||||
var cells [game.Height][game.Width]string
|
||||
for y := range game.Height {
|
||||
for x := range game.Width {
|
||||
style, glyph := cold, " "
|
||||
if x%3 == 0 && y%2 == 0 {
|
||||
glyph = "· "
|
||||
}
|
||||
if gallery {
|
||||
dx, dy := x-10, y-6
|
||||
radius := dx*dx + 3*dy*dy
|
||||
switch {
|
||||
case radius < 14:
|
||||
style, glyph = hotspot, "░░"
|
||||
case radius < 43:
|
||||
style, glyph = warm, "░░"
|
||||
case radius < 85:
|
||||
style, glyph = warm, "··"
|
||||
}
|
||||
}
|
||||
if scanning {
|
||||
style = style.Background(lg.Color("#4A2B43"))
|
||||
}
|
||||
cells[y][x] = style.Render(glyph)
|
||||
}
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func box(cells *[game.Height][game.Width]string, y int, title string) {
|
||||
const width = 24
|
||||
left := (fieldWidth - width) / 4
|
||||
// One box row takes the place of twelve two-column field cells.
|
||||
for i := 0; i < 3; i++ {
|
||||
clear(cells[y+i][left : left+width/2])
|
||||
}
|
||||
cells[y][left] = overlayStyle.Render("╭" + strings.Repeat("─", width-2) + "╮")
|
||||
cells[y+1][left] = overlayStyle.Render("│" + center(title, width-2) + "│")
|
||||
cells[y+2][left] = overlayStyle.Render("╰" + strings.Repeat("─", width-2) + "╯")
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
// Package arcade is the per-session terminal interface for Thermal Snake.
|
||||
package arcade
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"log/slog"
|
||||
random "math/rand/v2"
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
|
||||
"thermal-snake/internal/game"
|
||||
"thermal-snake/internal/scores"
|
||||
)
|
||||
|
||||
const (
|
||||
minWidth = 50
|
||||
minHeight = 23
|
||||
wideWidth = 76
|
||||
pageSize = 10
|
||||
)
|
||||
|
||||
type screen uint8
|
||||
|
||||
const (
|
||||
lobby screen = iota
|
||||
warmup
|
||||
playing
|
||||
paused
|
||||
debrief
|
||||
records
|
||||
)
|
||||
|
||||
type pulse struct{ generation uint64 }
|
||||
|
||||
type Model struct {
|
||||
store *scores.Store
|
||||
fingerprint string
|
||||
pilot scores.Pilot
|
||||
|
||||
width, height int
|
||||
screen screen
|
||||
generation uint64
|
||||
countdown int
|
||||
phase int
|
||||
run *game.Run
|
||||
result scores.Run
|
||||
saveFailed bool
|
||||
personalBest bool
|
||||
|
||||
returnScreen screen
|
||||
historyTab bool
|
||||
page int
|
||||
standings []scores.Standing
|
||||
history []scores.Run
|
||||
historyTotal int
|
||||
readFailed bool
|
||||
}
|
||||
|
||||
func New(store *scores.Store, fingerprint string, pilot scores.Pilot, width, height int) *Model {
|
||||
return &Model{
|
||||
store: store, fingerprint: fingerprint, pilot: pilot,
|
||||
width: width, height: height,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) Init() tea.Cmd { return m.nextPulse(360 * time.Millisecond) }
|
||||
|
||||
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width, m.height = msg.Width, msg.Height
|
||||
if m.tooSmall() && (m.screen == playing || m.screen == warmup) {
|
||||
m.screen = paused
|
||||
m.generation++
|
||||
}
|
||||
case pulse:
|
||||
if msg.generation == m.generation {
|
||||
return m, m.onPulse()
|
||||
}
|
||||
case tea.KeyPressMsg:
|
||||
return m, m.onKey(msg.String())
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Model) tooSmall() bool { return m.width < minWidth || m.height < minHeight }
|
||||
|
||||
func (m *Model) nextPulse(delay time.Duration) tea.Cmd {
|
||||
generation := m.generation
|
||||
return tea.Tick(delay, func(time.Time) tea.Msg { return pulse{generation} })
|
||||
}
|
||||
|
||||
func (m *Model) start() tea.Cmd {
|
||||
if m.tooSmall() {
|
||||
return nil
|
||||
}
|
||||
m.run = game.New(random.Uint64())
|
||||
m.result = scores.Run{ID: rand.Text()}
|
||||
m.countdown, m.phase = 3, 0
|
||||
m.personalBest, m.saveFailed = false, false
|
||||
m.screen = warmup
|
||||
m.generation++
|
||||
return m.nextPulse(450 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (m *Model) onPulse() tea.Cmd {
|
||||
switch m.screen {
|
||||
case lobby:
|
||||
m.phase = (m.phase + 1) % (2 * game.Height)
|
||||
return m.nextPulse(360 * time.Millisecond)
|
||||
case warmup:
|
||||
m.countdown--
|
||||
if m.countdown > 0 {
|
||||
return m.nextPulse(450 * time.Millisecond)
|
||||
}
|
||||
m.screen = playing
|
||||
return m.nextPulse(m.run.Interval())
|
||||
case playing:
|
||||
m.phase = (m.phase + 1) % (2 * game.Height)
|
||||
m.run.Step()
|
||||
if m.run.Outcome() == "" {
|
||||
return m.nextPulse(m.run.Interval())
|
||||
}
|
||||
m.screen = debrief
|
||||
m.result.Score, m.result.Heat = m.run.Score(), m.run.Heat()
|
||||
m.result.Outcome, m.result.Finished = m.run.Outcome(), time.Now().UTC()
|
||||
// Commit before the debrief is rendered, even if the client disconnects now.
|
||||
m.save()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Model) save() {
|
||||
pilot, best, err := m.store.Record(m.fingerprint, m.result)
|
||||
m.saveFailed = err != nil
|
||||
if err != nil {
|
||||
slog.Error("record run", "pilot", m.pilot.Callsign, "run", m.result.ID, "error", err)
|
||||
return
|
||||
}
|
||||
m.pilot, m.personalBest = pilot, best
|
||||
}
|
||||
|
||||
func (m *Model) onKey(key string) tea.Cmd {
|
||||
switch key {
|
||||
case "q", "ctrl+c":
|
||||
return tea.Quit
|
||||
}
|
||||
if m.tooSmall() {
|
||||
return nil
|
||||
}
|
||||
switch m.screen {
|
||||
case lobby:
|
||||
switch key {
|
||||
case "enter":
|
||||
return m.start()
|
||||
case "l", "h":
|
||||
m.openRecords(key == "h")
|
||||
}
|
||||
case warmup, playing:
|
||||
switch key {
|
||||
case "p", "space", "esc":
|
||||
m.screen = paused
|
||||
m.generation++
|
||||
case "up", "w":
|
||||
m.run.Steer(game.Up)
|
||||
case "right", "d":
|
||||
m.run.Steer(game.Right)
|
||||
case "down", "s":
|
||||
m.run.Steer(game.Down)
|
||||
case "left", "a":
|
||||
m.run.Steer(game.Left)
|
||||
}
|
||||
case paused:
|
||||
switch key {
|
||||
case "p", "space", "esc", "enter":
|
||||
if m.countdown > 0 {
|
||||
m.screen = warmup
|
||||
return m.nextPulse(450 * time.Millisecond)
|
||||
}
|
||||
m.screen = playing
|
||||
return m.nextPulse(m.run.Interval())
|
||||
}
|
||||
case debrief:
|
||||
if m.saveFailed {
|
||||
if key == "r" || key == "enter" {
|
||||
m.save()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
switch key {
|
||||
case "enter":
|
||||
return m.start()
|
||||
case "esc":
|
||||
return m.backToLobby()
|
||||
case "l", "h":
|
||||
m.openRecords(key == "h")
|
||||
}
|
||||
case records:
|
||||
return m.recordsKey(key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Model) backToLobby() tea.Cmd {
|
||||
if pilot, err := m.store.Profile(m.fingerprint); err == nil {
|
||||
m.pilot = pilot
|
||||
} else {
|
||||
slog.Error("read pilot profile", "pilot", m.pilot.Callsign, "error", err)
|
||||
}
|
||||
m.screen = lobby
|
||||
m.run = nil
|
||||
return m.nextPulse(360 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (m *Model) openRecords(history bool) {
|
||||
m.returnScreen, m.screen = m.screen, records
|
||||
m.generation++
|
||||
m.historyTab, m.page = history, 0
|
||||
m.loadRecords()
|
||||
}
|
||||
|
||||
func (m *Model) recordsKey(key string) tea.Cmd {
|
||||
switch key {
|
||||
case "esc":
|
||||
if m.returnScreen == lobby {
|
||||
return m.backToLobby()
|
||||
}
|
||||
m.screen = m.returnScreen
|
||||
case "tab", "l", "h":
|
||||
m.historyTab = key == "h" || (key == "tab" && !m.historyTab)
|
||||
m.page = 0
|
||||
m.loadRecords()
|
||||
case "left", "pgup":
|
||||
if m.page > 0 {
|
||||
m.page--
|
||||
m.loadRecords()
|
||||
}
|
||||
case "right", "pgdown":
|
||||
if m.page+1 < m.pages() {
|
||||
m.page++
|
||||
m.loadRecords()
|
||||
}
|
||||
case "r":
|
||||
m.loadRecords()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Model) pages() int {
|
||||
total := len(m.standings)
|
||||
if m.historyTab {
|
||||
total = m.historyTotal
|
||||
}
|
||||
return max(1, (total+pageSize-1)/pageSize)
|
||||
}
|
||||
|
||||
func (m *Model) loadRecords() {
|
||||
var err error
|
||||
if m.historyTab {
|
||||
m.history, m.historyTotal, err = m.store.History(m.fingerprint, m.page*pageSize, pageSize)
|
||||
} else {
|
||||
m.standings, err = m.store.Leaderboard()
|
||||
m.page = min(m.page, m.pages()-1)
|
||||
}
|
||||
m.readFailed = err != nil
|
||||
if err != nil {
|
||||
slog.Error("load records", "pilot", m.pilot.Callsign, "error", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package arcade
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
func (m *Model) recordsView(width int) []string {
|
||||
border := subtle.Render("│")
|
||||
inside := width - 4
|
||||
row := func(s string) string {
|
||||
return border + " " + s + strings.Repeat(" ", max(0, inside-ansi.StringWidth(s))) + " " + border
|
||||
}
|
||||
|
||||
var title, tableHead string
|
||||
if m.historyTab {
|
||||
title = "FLIGHT LOG"
|
||||
tableHead = historyLine(inside, "FINISHED UTC", "OUTCOME", "SCORE")
|
||||
} else {
|
||||
title = "SHARED RANKINGS"
|
||||
tableHead = standingLine(inside, "RANK", "PILOT", "BEST", "RUNS", " ")
|
||||
}
|
||||
|
||||
top := subtle.Render("╭─ ") + accent.Render(title) +
|
||||
subtle.Render(" "+strings.Repeat("─", width-5-len(title))+"╮")
|
||||
lines := []string{top, row(muted.Render(tableHead)), row(subtle.Render(strings.Repeat("─", inside)))}
|
||||
entries, note := m.recordRows(inside)
|
||||
for i := range pageSize {
|
||||
var entry string
|
||||
if i < len(entries) {
|
||||
entry = entries[i]
|
||||
}
|
||||
lines = append(lines, row(entry))
|
||||
}
|
||||
page := fmt.Sprintf("PAGE %d / %d", m.page+1, m.pages())
|
||||
lines = append(lines, row(pair(muted.Render(note), subtle.Render(page), inside)),
|
||||
subtle.Render("╰"+strings.Repeat("─", width-2)+"╯"), "")
|
||||
lines = append(lines, controls(control("TAB", "Switch"), control("←/→", "Page"), control("R", "Refresh"), control("ESC", "Back")))
|
||||
if m.historyTab {
|
||||
lines = append(lines, muted.Render("Newest completed runs first."))
|
||||
} else {
|
||||
lines = append(lines, muted.Render("Earliest best takes a score tie."))
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func (m *Model) recordRows(width int) ([]string, string) {
|
||||
if m.readFailed {
|
||||
return []string{accent.Render(" Records unavailable. Press R to retry.")}, ""
|
||||
}
|
||||
if m.historyTab {
|
||||
var rows []string
|
||||
for _, run := range m.history {
|
||||
line := historyLine(width, run.Finished.UTC().Format("2006-01-02 15:04:05"),
|
||||
outcomeName(run.Outcome), prettyScore(run.Score))
|
||||
rows = append(rows, rowStyle.Render(line))
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
rows = append(rows, muted.Render(" Finish a run to start your flight log."))
|
||||
}
|
||||
return rows, completionCount(m.historyTotal)
|
||||
}
|
||||
|
||||
var rows []string
|
||||
note := "Complete a run to rank"
|
||||
for _, standing := range m.standings {
|
||||
if standing.Callsign == m.pilot.Callsign {
|
||||
note = fmt.Sprintf("YOUR RANK #%d", standing.Rank)
|
||||
break
|
||||
}
|
||||
}
|
||||
start := m.page * pageSize
|
||||
end := min(start+pageSize, len(m.standings))
|
||||
for _, standing := range m.standings[start:end] {
|
||||
style, marker := rowStyle, " "
|
||||
if standing.Callsign == m.pilot.Callsign {
|
||||
style, marker = selfStyle, "›"
|
||||
}
|
||||
line := standingLine(width, fmt.Sprintf("#%d", standing.Rank), standing.Callsign,
|
||||
prettyScore(standing.Best.Score), strconv.Itoa(standing.Total), marker)
|
||||
rows = append(rows, style.Render(line))
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
rows = append(rows, muted.Render(" No completed runs yet."))
|
||||
}
|
||||
return rows, note
|
||||
}
|
||||
|
||||
func standingLine(width int, rank, pilot, best, runs, marker string) string {
|
||||
return fmt.Sprintf("%s %-6s %-*s %7s %6s ", marker, rank, width-25, pilot, best, runs)
|
||||
}
|
||||
|
||||
func historyLine(width int, finished, outcome, score string) string {
|
||||
return fmt.Sprintf(" %-19s %-*s %7s ", finished, width-32, outcome, score)
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user