Split Atlas chunks into tiles and objects
This commit is contained in:
parent
74dcae6542
commit
062f9cfec8
11 changed files with 284 additions and 169 deletions
|
@ -1,7 +1,6 @@
|
|||
package atlas
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
|
@ -9,27 +8,28 @@ import (
|
|||
"github.com/mdiluz/rove/pkg/vector"
|
||||
)
|
||||
|
||||
// Tile describes the type of terrain
|
||||
type Tile byte
|
||||
|
||||
const (
|
||||
// TileNone is a keyword for nothing
|
||||
TileNone = Tile(0)
|
||||
|
||||
// TileRock is solid rock ground
|
||||
TileRock = Tile('.')
|
||||
|
||||
// TileSand is sand
|
||||
TileSand = Tile(',')
|
||||
)
|
||||
|
||||
// Chunk represents a fixed square grid of tiles
|
||||
type Chunk struct {
|
||||
// Tiles represents the tiles within the chunk
|
||||
Tiles []byte `json:"tiles"`
|
||||
}
|
||||
|
||||
// SpawnContent will create a chunk and fill it with spawned tiles
|
||||
func (c *Chunk) SpawnContent(size int) {
|
||||
c.Tiles = make([]byte, size*size)
|
||||
for i := 0; i < len(c.Tiles); i++ {
|
||||
c.Tiles[i] = objects.Empty
|
||||
}
|
||||
|
||||
// For now, fill it randomly with objects
|
||||
for i := range c.Tiles {
|
||||
if rand.Intn(16) == 0 {
|
||||
c.Tiles[i] = objects.LargeRock
|
||||
} else if rand.Intn(32) == 0 {
|
||||
c.Tiles[i] = objects.SmallRock
|
||||
}
|
||||
}
|
||||
// Objects represents the objects within the chunk
|
||||
// only one possible object per tile for now
|
||||
Objects map[int]objects.Object `json:"objects"`
|
||||
}
|
||||
|
||||
// Atlas represents a grid of Chunks
|
||||
|
@ -58,50 +58,101 @@ func NewAtlas(chunkSize int) Atlas {
|
|||
WorldOrigin: vector.Vector{X: 0, Y: 0},
|
||||
}
|
||||
// Initialise the first chunk
|
||||
a.Chunks[0].SpawnContent(chunkSize)
|
||||
a.Chunks[0].populate(chunkSize)
|
||||
return a
|
||||
}
|
||||
|
||||
// SetTile sets an individual tile's kind
|
||||
func (a *Atlas) SetTile(v vector.Vector, tile byte) {
|
||||
// Get the chunk
|
||||
func (a *Atlas) SetTile(v vector.Vector, tile Tile) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
chunk := a.Chunks[c]
|
||||
if chunk.Tiles == nil {
|
||||
chunk.SpawnContent(a.ChunkSize)
|
||||
}
|
||||
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
tileID := local.X + local.Y*a.ChunkSize
|
||||
|
||||
// Sanity check
|
||||
if tileID >= len(chunk.Tiles) || tileID < 0 {
|
||||
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
||||
}
|
||||
|
||||
// Set the chunk back
|
||||
chunk.Tiles[tileID] = tile
|
||||
a.Chunks[c] = chunk
|
||||
a.setTile(c, local, byte(tile))
|
||||
}
|
||||
|
||||
// GetTile will return an individual tile
|
||||
func (a *Atlas) GetTile(v vector.Vector) byte {
|
||||
// Get the chunk
|
||||
// SetObject sets the object on a tile
|
||||
func (a *Atlas) SetObject(v vector.Vector, obj objects.Object) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
a.setObject(c, local, obj)
|
||||
}
|
||||
|
||||
// QueryPosition will return information for a specific position
|
||||
func (a *Atlas) QueryPosition(v vector.Vector) (byte, objects.Object) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
chunk := a.Chunks[c]
|
||||
if chunk.Tiles == nil {
|
||||
chunk.SpawnContent(a.ChunkSize)
|
||||
chunk.populate(a.ChunkSize)
|
||||
}
|
||||
i := a.chunkTileIndex(local)
|
||||
return chunk.Tiles[i], chunk.Objects[i]
|
||||
}
|
||||
|
||||
// chunkTileID returns the tile index within a chunk
|
||||
func (a *Atlas) chunkTileIndex(local vector.Vector) int {
|
||||
return local.X + local.Y*a.ChunkSize
|
||||
}
|
||||
|
||||
// populate will fill a chunk with data
|
||||
func (c *Chunk) populate(size int) {
|
||||
c.Tiles = make([]byte, size*size)
|
||||
c.Objects = make(map[int]objects.Object)
|
||||
|
||||
// Set up the tiles
|
||||
for i := 0; i < len(c.Tiles); i++ {
|
||||
if rand.Intn(3) == 0 {
|
||||
c.Tiles[i] = byte(TileRock)
|
||||
} else {
|
||||
c.Tiles[i] = byte(TileSand)
|
||||
}
|
||||
}
|
||||
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
tileID := local.X + local.Y*a.ChunkSize
|
||||
// Set up any objects
|
||||
for i := 0; i < len(c.Tiles); i++ {
|
||||
if rand.Intn(16) == 0 {
|
||||
c.Objects[i] = objects.Object{Type: objects.LargeRock}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if tileID >= len(chunk.Tiles) || tileID < 0 {
|
||||
log.Fatalf("Local tileID is not in valid chunk, somehow, this means something is very wrong")
|
||||
// setTile sets a tile in a specific chunk
|
||||
func (a *Atlas) setTile(chunk int, local vector.Vector, tile byte) {
|
||||
c := a.Chunks[chunk]
|
||||
if c.Tiles == nil {
|
||||
c.populate(a.ChunkSize)
|
||||
}
|
||||
|
||||
return chunk.Tiles[tileID]
|
||||
c.Tiles[a.chunkTileIndex(local)] = tile
|
||||
a.Chunks[chunk] = c
|
||||
}
|
||||
|
||||
// setObject sets an object in a specific chunk
|
||||
func (a *Atlas) setObject(chunk int, local vector.Vector, object objects.Object) {
|
||||
c := a.Chunks[chunk]
|
||||
if c.Tiles == nil {
|
||||
c.populate(a.ChunkSize)
|
||||
}
|
||||
|
||||
i := a.chunkTileIndex(local)
|
||||
if object.Type != objects.None {
|
||||
c.Objects[i] = object
|
||||
} else {
|
||||
delete(c.Objects, i)
|
||||
}
|
||||
a.Chunks[chunk] = c
|
||||
}
|
||||
|
||||
// setTileAndObject sets both tile and object information for location in chunk
|
||||
func (a *Atlas) setTileAndObject(chunk int, local vector.Vector, tile byte, object objects.Object) {
|
||||
c := a.Chunks[chunk]
|
||||
if c.Tiles == nil {
|
||||
c.populate(a.ChunkSize)
|
||||
}
|
||||
|
||||
i := a.chunkTileIndex(local)
|
||||
c.Tiles[i] = tile
|
||||
c.Objects[i] = object
|
||||
a.Chunks[chunk] = c
|
||||
}
|
||||
|
||||
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
|
||||
|
|
|
@ -3,6 +3,7 @@ package atlas
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -19,8 +20,8 @@ func TestAtlas_toChunk(t *testing.T) {
|
|||
assert.NotNil(t, a)
|
||||
|
||||
// Get a tile to spawn the chunks
|
||||
a.GetTile(vector.Vector{X: -1, Y: -1})
|
||||
a.GetTile(vector.Vector{X: 0, Y: 0})
|
||||
a.QueryPosition(vector.Vector{X: -1, Y: -1})
|
||||
a.QueryPosition(vector.Vector{X: 0, Y: 0})
|
||||
|
||||
// Chunks should look like:
|
||||
// 2 | 3
|
||||
|
@ -38,8 +39,8 @@ func TestAtlas_toChunk(t *testing.T) {
|
|||
a = NewAtlas(2)
|
||||
assert.NotNil(t, a)
|
||||
// Get a tile to spawn the chunks
|
||||
a.GetTile(vector.Vector{X: -2, Y: -2})
|
||||
a.GetTile(vector.Vector{X: 1, Y: 1})
|
||||
a.QueryPosition(vector.Vector{X: -2, Y: -2})
|
||||
a.QueryPosition(vector.Vector{X: 1, Y: 1})
|
||||
// Chunks should look like:
|
||||
// 2 | 3
|
||||
// -----
|
||||
|
@ -56,8 +57,8 @@ func TestAtlas_toChunk(t *testing.T) {
|
|||
a = NewAtlas(2)
|
||||
assert.NotNil(t, a)
|
||||
// Get a tile to spawn the chunks
|
||||
a.GetTile(vector.Vector{X: 5, Y: 5})
|
||||
a.GetTile(vector.Vector{X: -5, Y: -5})
|
||||
a.QueryPosition(vector.Vector{X: 5, Y: 5})
|
||||
a.QueryPosition(vector.Vector{X: -5, Y: -5})
|
||||
// Chunks should look like:
|
||||
// 12| 13|| 14| 15
|
||||
// ----------------
|
||||
|
@ -82,15 +83,30 @@ func TestAtlas_GetSetTile(t *testing.T) {
|
|||
|
||||
// Set the origin tile to 1 and test it
|
||||
a.SetTile(vector.Vector{X: 0, Y: 0}, 1)
|
||||
tile := a.GetTile(vector.Vector{X: 0, Y: 0})
|
||||
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
|
||||
// Set another tile to 1 and test it
|
||||
a.SetTile(vector.Vector{X: 5, Y: -2}, 2)
|
||||
tile = a.GetTile(vector.Vector{X: 5, Y: -2})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: 5, Y: -2})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
}
|
||||
|
||||
func TestAtlas_GetSetObject(t *testing.T) {
|
||||
a := NewAtlas(10)
|
||||
assert.NotNil(t, a)
|
||||
|
||||
// Set the origin tile to 1 and test it
|
||||
a.SetObject(vector.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
|
||||
_, obj := a.QueryPosition(vector.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
|
||||
|
||||
// Set another tile to 1 and test it
|
||||
a.SetObject(vector.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
|
||||
_, obj = a.QueryPosition(vector.Vector{X: 5, Y: -2})
|
||||
assert.Equal(t, objects.Object{Type: objects.SmallRock}, obj)
|
||||
}
|
||||
|
||||
func TestAtlas_Grown(t *testing.T) {
|
||||
// Start with a small example
|
||||
a := NewAtlas(2)
|
||||
|
@ -103,21 +119,21 @@ func TestAtlas_Grown(t *testing.T) {
|
|||
a.SetTile(vector.Vector{X: 1, Y: -2}, 3)
|
||||
|
||||
// Check tile values
|
||||
tile := a.GetTile(vector.Vector{X: 0, Y: 0})
|
||||
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
|
||||
tile = a.GetTile(vector.Vector{X: -1, Y: -1})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
|
||||
tile = a.GetTile(vector.Vector{X: 1, Y: -2})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
|
||||
assert.Equal(t, byte(3), tile)
|
||||
|
||||
tile = a.GetTile(vector.Vector{X: 0, Y: 0})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
|
||||
tile = a.GetTile(vector.Vector{X: -1, Y: -1})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
|
||||
tile = a.GetTile(vector.Vector{X: 1, Y: -2})
|
||||
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
|
||||
assert.Equal(t, byte(3), tile)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue