refactored grid management

This commit is contained in:
2024-05-30 10:23:31 +02:00
parent 1ec4b9e257
commit 5fae7256d7
2 changed files with 19 additions and 14 deletions

13
grid.go
View File

@@ -36,7 +36,7 @@ func NewGrid(width, height, density int, empty bool) *Grid {
return grid return grid
} }
// Create a 1:1 copy // Create a new 1:1 instance
func (grid *Grid) Clone() *Grid { func (grid *Grid) Clone() *Grid {
newgrid := &Grid{} newgrid := &Grid{}
@@ -47,6 +47,15 @@ func (grid *Grid) Clone() *Grid {
return newgrid return newgrid
} }
// copy data
func (grid *Grid) Copy(other *Grid) {
for y := range grid.Data {
for x := range grid.Data[y] {
other.Data[y][x] = grid.Data[y][x]
}
}
}
// delete all contents // delete all contents
func (grid *Grid) Clear() { func (grid *Grid) Clear() {
for y := range grid.Data { for y := range grid.Data {
@@ -57,7 +66,7 @@ func (grid *Grid) Clear() {
} }
// initialize with random life cells using the given density // initialize with random life cells using the given density
func (grid *Grid) FillRandom(game *ScenePlay) { func (grid *Grid) FillRandom() {
if !grid.Empty { if !grid.Empty {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {

View File

@@ -5,7 +5,6 @@ import (
"image" "image"
"image/color" "image/color"
"log" "log"
"math/rand"
"os" "os"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@@ -493,6 +492,7 @@ func (scene *ScenePlay) InitPattern() {
scene.History.LoadRLE(scene.Config.RLE) scene.History.LoadRLE(scene.Config.RLE)
} }
// pre-render offscreen cache image
func (scene *ScenePlay) InitCache() { func (scene *ScenePlay) InitCache() {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
@@ -505,13 +505,17 @@ func (scene *ScenePlay) InitCache() {
for y := 0; y < scene.Config.Height; y++ { for y := 0; y < scene.Config.Height; y++ {
for x := 0; x < scene.Config.Width; x++ { for x := 0; x < scene.Config.Width; x++ {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(float64(x*scene.Config.Cellsize), float64(y*scene.Config.Cellsize)) op.GeoM.Translate(
float64(x*scene.Config.Cellsize),
float64(y*scene.Config.Cellsize),
)
scene.Cache.DrawImage(scene.Tiles.White, op) scene.Cache.DrawImage(scene.Tiles.White, op)
} }
} }
} }
// initialize grid[s], either using pre-computed from state or rle file, or random
func (scene *ScenePlay) InitGrid(grid *Grid) { func (scene *ScenePlay) InitGrid(grid *Grid) {
if grid != nil { if grid != nil {
// use pre-loaded grid // use pre-loaded grid
@@ -529,16 +533,8 @@ func (scene *ScenePlay) InitGrid(grid *Grid) {
gridb := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty) gridb := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
history := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty) history := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
for y := 0; y < scene.Config.Height; y++ { grida.FillRandom()
if !scene.Config.Empty { grida.Copy(history)
for x := 0; x < scene.Config.Width; x++ {
if rand.Intn(scene.Config.Density) == 1 {
history.Data[y][x] = 1
grida.Data[y][x] = 1
}
}
}
}
scene.Grids = []*Grid{ scene.Grids = []*Grid{
grida, grida,