diff --git a/pkg/game/atlas.go b/pkg/game/atlas.go index e981005..90144ee 100644 --- a/pkg/game/atlas.go +++ b/pkg/game/atlas.go @@ -1,6 +1,9 @@ package game -import "fmt" +import ( + "fmt" + "log" +) // Kind represents the type of a tile on the map type Kind byte @@ -25,8 +28,11 @@ type Atlas struct { } // NewAtlas creates a new empty atlas -func NewAtlas(radius int, chunkSize int) Atlas { - size := radius * 2 +func NewAtlas(size int, chunkSize int) Atlas { + if size%2 != 0 { + log.Fatal("atlas size must always be even") + } + a := Atlas{ Size: size, Chunks: make([]Chunk, size*size), @@ -108,17 +114,20 @@ func (a *Atlas) ChunkOrigin(chunk int) Vector { return v.Multiplied(a.ChunkSize) } -// Grown will return a grown copy of the current atlas -func (a *Atlas) Grown(newRadius int) (Atlas, error) { - delta := (newRadius * 2) - a.Size +// Grow will return a grown copy of the current atlas +func (a *Atlas) Grow(size int) error { + if size%2 != 0 { + return fmt.Errorf("atlas size must always be even") + } + delta := size - a.Size if delta < 0 { - return Atlas{}, fmt.Errorf("Cannot shrink an atlas") + return fmt.Errorf("Cannot shrink an atlas") } else if delta == 0 { - return Atlas{}, nil + return nil } // Create a new atlas - newAtlas := NewAtlas(newRadius, a.ChunkSize) + newAtlas := NewAtlas(size, a.ChunkSize) // Copy old chunks into new chunks for index, chunk := range a.Chunks { @@ -126,6 +135,9 @@ func (a *Atlas) Grown(newRadius int) (Atlas, error) { newAtlas.Chunks[newAtlas.ToChunk(a.ChunkOrigin(index))] = chunk } + // Copy the new atlas data into this one + *a = newAtlas + // Return the new atlas - return newAtlas, nil + return nil } diff --git a/pkg/game/atlas_test.go b/pkg/game/atlas_test.go index c5591e8..f0f32f7 100644 --- a/pkg/game/atlas_test.go +++ b/pkg/game/atlas_test.go @@ -7,14 +7,14 @@ import ( ) func TestAtlas_NewAtlas(t *testing.T) { - a := NewAtlas(1, 1) // "radius" of 1, each chunk just one tile + a := NewAtlas(2, 1) assert.NotNil(t, a) // Tiles should look like: 2 | 3 // ----- // 0 | 1 assert.Equal(t, 4, len(a.Chunks)) - a = NewAtlas(2, 1) // "radius" of 2 + a = NewAtlas(4, 1) assert.NotNil(t, a) // Tiles should look like: 2 | 3 // ----- @@ -23,7 +23,7 @@ func TestAtlas_NewAtlas(t *testing.T) { } func TestAtlas_ToChunk(t *testing.T) { - a := NewAtlas(1, 1) + a := NewAtlas(2, 1) assert.NotNil(t, a) // Tiles should look like: 2 | 3 // ----- @@ -37,7 +37,7 @@ func TestAtlas_ToChunk(t *testing.T) { tile = a.ToChunk(Vector{-1, 0}) assert.Equal(t, 2, tile) - a = NewAtlas(1, 2) + a = NewAtlas(2, 2) assert.NotNil(t, a) // Tiles should look like: // 2 | 3 @@ -52,7 +52,7 @@ func TestAtlas_ToChunk(t *testing.T) { tile = a.ToChunk(Vector{-2, 1}) assert.Equal(t, 2, tile) - a = NewAtlas(2, 2) + a = NewAtlas(4, 2) assert.NotNil(t, a) // Tiles should look like: // 12| 13|| 14| 15 @@ -73,7 +73,7 @@ func TestAtlas_ToChunk(t *testing.T) { } func TestAtlas_GetSetTile(t *testing.T) { - a := NewAtlas(2, 10) + a := NewAtlas(4, 10) assert.NotNil(t, a) // Set the origin tile to 1 and test it @@ -91,7 +91,7 @@ func TestAtlas_GetSetTile(t *testing.T) { func TestAtlas_Grown(t *testing.T) { // Start with a small example - a := NewAtlas(1, 2) + a := NewAtlas(2, 2) assert.NotNil(t, a) assert.Equal(t, 4, len(a.Chunks)) @@ -101,7 +101,7 @@ func TestAtlas_Grown(t *testing.T) { assert.NoError(t, a.SetTile(Vector{1, -2}, 3)) // Grow once to just double it - a, err := a.Grown(2) + err := a.Grow(4) assert.NoError(t, err) assert.Equal(t, 16, len(a.Chunks)) @@ -118,7 +118,7 @@ func TestAtlas_Grown(t *testing.T) { assert.Equal(t, Kind(3), tile) // Grow it again even bigger - a, err = a.Grown(5) + err = a.Grow(10) assert.NoError(t, err) assert.Equal(t, 100, len(a.Chunks))