chore: gitignore generated _templ.go files, track .templ sources
Generated _templ.go files are deterministic output from .templ sources, same as output.css from input.css. Remove them from version control to reduce diff noise and merge conflicts. Add build:templ and live:templ tasks to the Taskfile so generation happens as part of the build.
This commit is contained in:
113
features/snakegame/components/board.templ
Normal file
113
features/snakegame/components/board.templ
Normal file
@@ -0,0 +1,113 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ryanhamamura/c4/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) {
|
||||
<div
|
||||
id="snake-board"
|
||||
class="snake-board"
|
||||
if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) {
|
||||
style={ fmt.Sprintf("grid-template-columns:repeat(%d,1fr);", sg.State.Width) }
|
||||
}
|
||||
>
|
||||
if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) {
|
||||
@boardCells(sg)
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ boardCells(sg *snake.SnakeGame) {
|
||||
{{ state := sg.State }}
|
||||
{{ grid := buildGrid(state) }}
|
||||
{{ cellSize := cellSizeForGrid(state.Width, state.Height) }}
|
||||
for y := 0; y < state.Height; y++ {
|
||||
<div class="snake-row">
|
||||
for x := 0; x < state.Width; x++ {
|
||||
{{ ci := grid[y][x] }}
|
||||
if ci.snakeIdx == -2 {
|
||||
<div class="snake-cell snake-food" style={ fmt.Sprintf("width:%dpx;height:%dpx;", cellSize, cellSize) }></div>
|
||||
} else if ci.snakeIdx >= 0 {
|
||||
{{ s := state.Snakes[ci.snakeIdx] }}
|
||||
{{ bg := snakeColor(ci.snakeIdx) }}
|
||||
if ci.isHead {
|
||||
if s.Alive {
|
||||
<div class="snake-cell snake-head" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;box-shadow:0 0 8px %s;", cellSize, cellSize, bg, bg) }></div>
|
||||
} else {
|
||||
<div class="snake-cell snake-head snake-dead" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;box-shadow:0 0 8px %s;", cellSize, cellSize, bg, bg) }></div>
|
||||
}
|
||||
} else {
|
||||
if s.Alive {
|
||||
<div class="snake-cell snake-body" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;", cellSize, cellSize, bg) }></div>
|
||||
} else {
|
||||
<div class="snake-cell snake-body snake-dead" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;", cellSize, cellSize, bg) }></div>
|
||||
}
|
||||
}
|
||||
} else {
|
||||
<div class="snake-cell" style={ fmt.Sprintf("width:%dpx;height:%dpx;", cellSize, cellSize) }></div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user