Skip to content

Commit

Permalink
feat(game): Keep starting pattern on reset
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 15, 2024
1 parent 6f0f406 commit aca0290
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions internal/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Game struct {
conf *config.Config
viewW, viewH int
x, y int
startPattern pattern.Pattern
pattern pattern.Pattern
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -254,11 +255,7 @@ func (g *Game) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
case key.Matches(msg, g.keymap.reset):
for _, row := range g.pattern.Grid {
for i := range row {
row[i] = 0
}
}
g.Reset()
case key.Matches(msg, g.keymap.quit):
return g, tea.Quit
}
Expand Down Expand Up @@ -367,3 +364,16 @@ func (g *Game) CenterView() {
g.x = g.BoardW()/2 - g.viewW/2
g.y = g.BoardH()/2 - g.viewH/2
}

func (g *Game) Reset() {
cloned := g.startPattern
cloned.Grid = slices.Clone(g.startPattern.Grid)
for i, row := range cloned.Grid {
cloned.Grid[i] = slices.Clip(row)
}
w, h := g.BoardW(), g.BoardH()
g.pattern = cloned
if w != 0 && h != 0 {
g.Resize(w, h, image.Pt(0, 0))
}
}
3 changes: 2 additions & 1 deletion internal/game/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Option func(game *Game)

func WithPattern(pat pattern.Pattern) Option {
return func(game *Game) {
game.pattern = pat
game.startPattern = pat
game.Reset()
}
}

Expand Down

0 comments on commit aca0290

Please sign in to comment.