Files
games/ui/snakeboard.go
Ryan Hamamura 2dc75107d1 Add visual smoothing for snake game and systemd deployment
CSS animations for smoother 60fps feel despite 7Hz game ticks:
- 130ms transitions on cell background/box-shadow
- Head pop-in animation on direction changes
- Food pulse animation
- Smooth death state fade with grayscale

Per-snake colored glow on head cells.

Make server port configurable via PORT env var (default 8080).

Add deploy/ with systemd service and scripts:
- setup.sh: create games user, /opt/c4, install unit
- deploy.sh: build and install binary, restart service
- package.sh: cross-compile, tarball, base64 split for transfer
- reassemble.sh: decode and extract on target server
2026-02-04 06:50:18 -10:00

97 lines
2.2 KiB
Go

package ui
import (
"fmt"
"github.com/ryanhamamura/c4/snake"
"github.com/ryanhamamura/via/h"
)
func SnakeBoard(sg *snake.SnakeGame) h.H {
state := sg.State
if state == nil || sg.Status != snake.StatusInProgress && sg.Status != snake.StatusFinished {
return nil
}
// Build a lookup grid for rendering
type cellInfo struct {
snakeIdx int // -1 = empty, -2 = food
isHead bool
}
grid := make([][]cellInfo, state.Height)
for y := 0; y < state.Height; y++ {
grid[y] = make([]cellInfo, state.Width)
for x := 0; x < state.Width; x++ {
grid[y][x] = cellInfo{snakeIdx: -1}
}
}
for fi := range state.Food {
f := state.Food[fi]
if f.X >= 0 && f.X < state.Width && f.Y >= 0 && f.Y < state.Height {
grid[f.Y][f.X] = cellInfo{snakeIdx: -2}
}
}
for si, s := range state.Snakes {
if s == nil {
continue
}
for bi, bp := range s.Body {
if bp.X >= 0 && bp.X < state.Width && bp.Y >= 0 && bp.Y < state.Height {
grid[bp.Y][bp.X] = cellInfo{snakeIdx: si, isHead: bi == 0}
}
}
}
// Cell size scales with grid dimensions
cellSize := 20
if state.Width <= 20 {
cellSize = 24
}
var rows []h.H
for y := 0; y < state.Height; y++ {
var cells []h.H
for x := 0; x < state.Width; x++ {
ci := grid[y][x]
class := "snake-cell"
style := fmt.Sprintf("width:%dpx;height:%dpx;", cellSize, cellSize)
switch {
case ci.snakeIdx == -2:
class += " snake-food"
case ci.snakeIdx >= 0:
s := state.Snakes[ci.snakeIdx]
colorIdx := ci.snakeIdx
bg := ""
if colorIdx < len(snake.SnakeColors) {
bg = snake.SnakeColors[colorIdx]
style += fmt.Sprintf("background:%s;", bg)
}
if !s.Alive {
class += " snake-dead"
}
if ci.isHead {
class += " snake-head"
if bg != "" {
style += fmt.Sprintf("box-shadow:0 0 8px %s;", bg)
}
}
}
cells = append(cells, h.Div(h.Class(class), h.Attr("style", style)))
}
rowAttrs := append([]h.H{h.Class("snake-row")}, cells...)
rows = append(rows, h.Div(rowAttrs...))
}
boardStyle := fmt.Sprintf("grid-template-columns:repeat(%d,1fr);", state.Width)
attrs := []h.H{
h.Class("snake-board"),
h.Attr("style", boardStyle),
}
attrs = append(attrs, rows...)
return h.Div(attrs...)
}