package components
import (
"fmt"
"github.com/ryanhamamura/games/snake"
)
func cellSizeForGrid(width, height int) int {
maxDim := width
if height > maxDim {
maxDim = height
}
switch {
case maxDim <= 15:
return 28
case maxDim <= 20:
return 24
case maxDim <= 30:
return 20
case maxDim <= 40:
return 16
default:
return 14
}
}
type cellInfo struct {
snakeIdx int // -1 = empty, -2 = food
isHead bool
}
templ Board(sg *snake.SnakeGame) {
if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) {
@boardCells(sg)
}
}
templ boardCells(sg *snake.SnakeGame) {
{{ state := sg.State }}
{{ grid := buildGrid(state) }}
{{ cellSize := cellSizeForGrid(state.Width, state.Height) }}
for y := 0; y < state.Height; y++ {
for x := 0; x < state.Width; x++ {
{{ ci := grid[y][x] }}
if ci.snakeIdx == -2 {
} else if ci.snakeIdx >= 0 {
{{ s := state.Snakes[ci.snakeIdx] }}
{{ bg := snakeColor(ci.snakeIdx) }}
if ci.isHead {
if s.Alive {
} else {
}
} else {
if s.Alive {
} else {
}
}
} else {
}
}
}
}
func buildGrid(state *snake.GameState) [][]cellInfo {
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}
}
}
}
return grid
}
func snakeColor(idx int) string {
if idx >= 0 && idx < len(snake.SnakeColors) {
return snake.SnakeColors[idx]
}
return "#666"
}