// Code generated by templ - DO NOT EDIT. // templ: version: v0.3.1001 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" 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 } func Board(sg *snake.SnakeGame) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { return templ_7745c5c3_CtxErr } templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { defer func() { templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) if templ_7745c5c3_Err == nil { templ_7745c5c3_Err = templ_7745c5c3_BufErr } }() } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) { templ_7745c5c3_Err = boardCells(sg).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) } func boardCells(sg *snake.SnakeGame) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { return templ_7745c5c3_CtxErr } templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { defer func() { templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) if templ_7745c5c3_Err == nil { templ_7745c5c3_Err = templ_7745c5c3_BufErr } }() } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) state := sg.State grid := buildGrid(state) cellSize := cellSizeForGrid(state.Width, state.Height) for y := 0; y < state.Height; y++ { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for x := 0; x < state.Width; x++ { ci := grid[y][x] if ci.snakeIdx == -2 { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } else if ci.snakeIdx >= 0 { s := state.Snakes[ci.snakeIdx] bg := snakeColor(ci.snakeIdx) if ci.isHead { if s.Alive { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } else { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } } else { if s.Alive { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } else { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } } } else { templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } } templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } return nil }) } 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" } var _ = templruntime.GeneratedTemplate