Refactor atlas creation and growth
Pass in the real size and grow itself rather than return a new one
This commit is contained in:
parent
82e6c4e33f
commit
289bab2a7a
2 changed files with 31 additions and 19 deletions
|
@ -1,6 +1,9 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
// Kind represents the type of a tile on the map
|
// Kind represents the type of a tile on the map
|
||||||
type Kind byte
|
type Kind byte
|
||||||
|
@ -25,8 +28,11 @@ type Atlas struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAtlas creates a new empty atlas
|
// NewAtlas creates a new empty atlas
|
||||||
func NewAtlas(radius int, chunkSize int) Atlas {
|
func NewAtlas(size int, chunkSize int) Atlas {
|
||||||
size := radius * 2
|
if size%2 != 0 {
|
||||||
|
log.Fatal("atlas size must always be even")
|
||||||
|
}
|
||||||
|
|
||||||
a := Atlas{
|
a := Atlas{
|
||||||
Size: size,
|
Size: size,
|
||||||
Chunks: make([]Chunk, size*size),
|
Chunks: make([]Chunk, size*size),
|
||||||
|
@ -108,17 +114,20 @@ func (a *Atlas) ChunkOrigin(chunk int) Vector {
|
||||||
return v.Multiplied(a.ChunkSize)
|
return v.Multiplied(a.ChunkSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grown will return a grown copy of the current atlas
|
// Grow will return a grown copy of the current atlas
|
||||||
func (a *Atlas) Grown(newRadius int) (Atlas, error) {
|
func (a *Atlas) Grow(size int) error {
|
||||||
delta := (newRadius * 2) - a.Size
|
if size%2 != 0 {
|
||||||
|
return fmt.Errorf("atlas size must always be even")
|
||||||
|
}
|
||||||
|
delta := size - a.Size
|
||||||
if delta < 0 {
|
if delta < 0 {
|
||||||
return Atlas{}, fmt.Errorf("Cannot shrink an atlas")
|
return fmt.Errorf("Cannot shrink an atlas")
|
||||||
} else if delta == 0 {
|
} else if delta == 0 {
|
||||||
return Atlas{}, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new atlas
|
// Create a new atlas
|
||||||
newAtlas := NewAtlas(newRadius, a.ChunkSize)
|
newAtlas := NewAtlas(size, a.ChunkSize)
|
||||||
|
|
||||||
// Copy old chunks into new chunks
|
// Copy old chunks into new chunks
|
||||||
for index, chunk := range a.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
|
newAtlas.Chunks[newAtlas.ToChunk(a.ChunkOrigin(index))] = chunk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy the new atlas data into this one
|
||||||
|
*a = newAtlas
|
||||||
|
|
||||||
// Return the new atlas
|
// Return the new atlas
|
||||||
return newAtlas, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAtlas_NewAtlas(t *testing.T) {
|
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)
|
assert.NotNil(t, a)
|
||||||
// Tiles should look like: 2 | 3
|
// Tiles should look like: 2 | 3
|
||||||
// -----
|
// -----
|
||||||
// 0 | 1
|
// 0 | 1
|
||||||
assert.Equal(t, 4, len(a.Chunks))
|
assert.Equal(t, 4, len(a.Chunks))
|
||||||
|
|
||||||
a = NewAtlas(2, 1) // "radius" of 2
|
a = NewAtlas(4, 1)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
// Tiles should look like: 2 | 3
|
// Tiles should look like: 2 | 3
|
||||||
// -----
|
// -----
|
||||||
|
@ -23,7 +23,7 @@ func TestAtlas_NewAtlas(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAtlas_ToChunk(t *testing.T) {
|
func TestAtlas_ToChunk(t *testing.T) {
|
||||||
a := NewAtlas(1, 1)
|
a := NewAtlas(2, 1)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
// Tiles should look like: 2 | 3
|
// Tiles should look like: 2 | 3
|
||||||
// -----
|
// -----
|
||||||
|
@ -37,7 +37,7 @@ func TestAtlas_ToChunk(t *testing.T) {
|
||||||
tile = a.ToChunk(Vector{-1, 0})
|
tile = a.ToChunk(Vector{-1, 0})
|
||||||
assert.Equal(t, 2, tile)
|
assert.Equal(t, 2, tile)
|
||||||
|
|
||||||
a = NewAtlas(1, 2)
|
a = NewAtlas(2, 2)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
// Tiles should look like:
|
// Tiles should look like:
|
||||||
// 2 | 3
|
// 2 | 3
|
||||||
|
@ -52,7 +52,7 @@ func TestAtlas_ToChunk(t *testing.T) {
|
||||||
tile = a.ToChunk(Vector{-2, 1})
|
tile = a.ToChunk(Vector{-2, 1})
|
||||||
assert.Equal(t, 2, tile)
|
assert.Equal(t, 2, tile)
|
||||||
|
|
||||||
a = NewAtlas(2, 2)
|
a = NewAtlas(4, 2)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
// Tiles should look like:
|
// Tiles should look like:
|
||||||
// 12| 13|| 14| 15
|
// 12| 13|| 14| 15
|
||||||
|
@ -73,7 +73,7 @@ func TestAtlas_ToChunk(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAtlas_GetSetTile(t *testing.T) {
|
func TestAtlas_GetSetTile(t *testing.T) {
|
||||||
a := NewAtlas(2, 10)
|
a := NewAtlas(4, 10)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
|
|
||||||
// Set the origin tile to 1 and test it
|
// 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) {
|
func TestAtlas_Grown(t *testing.T) {
|
||||||
// Start with a small example
|
// Start with a small example
|
||||||
a := NewAtlas(1, 2)
|
a := NewAtlas(2, 2)
|
||||||
assert.NotNil(t, a)
|
assert.NotNil(t, a)
|
||||||
assert.Equal(t, 4, len(a.Chunks))
|
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))
|
assert.NoError(t, a.SetTile(Vector{1, -2}, 3))
|
||||||
|
|
||||||
// Grow once to just double it
|
// Grow once to just double it
|
||||||
a, err := a.Grown(2)
|
err := a.Grow(4)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 16, len(a.Chunks))
|
assert.Equal(t, 16, len(a.Chunks))
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ func TestAtlas_Grown(t *testing.T) {
|
||||||
assert.Equal(t, Kind(3), tile)
|
assert.Equal(t, Kind(3), tile)
|
||||||
|
|
||||||
// Grow it again even bigger
|
// Grow it again even bigger
|
||||||
a, err = a.Grown(5)
|
err = a.Grow(10)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 100, len(a.Chunks))
|
assert.Equal(t, 100, len(a.Chunks))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue