Spawn a border around the world atlas for now

Also spawn the rover within a single chunk in the middle
This commit is contained in:
Marc Di Luzio 2020-06-07 18:57:44 +01:00
parent cb725c96d2
commit 3781a4d10d
5 changed files with 59 additions and 5 deletions

View file

@ -46,6 +46,26 @@ func NewAtlas(size int, chunkSize int) Atlas {
return a
}
// SpawnAtlasBorder surrounds the current atlas in a border wall
func (a *Atlas) SpawnAtlasBorder() error {
extent := a.ChunkSize * (a.Size / 2)
// Surround the atlas in walls
for i := -extent; i < extent; i++ {
if err := a.SetTile(Vector{i, extent - 1}, TileWall); err != nil {
return err
} else if a.SetTile(Vector{extent - 1, i}, TileWall); err != nil {
return err
} else if a.SetTile(Vector{-extent, i}, TileWall); err != nil {
return err
} else if a.SetTile(Vector{i, extent - 1}, TileWall); err != nil {
return err
}
}
return nil
}
// SetTile sets an individual tile's kind
func (a *Atlas) SetTile(v Vector, tile Tile) error {
chunk := a.ToChunk(v)

View file

@ -134,3 +134,27 @@ func TestAtlas_Grown(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, Tile(3), tile)
}
func TestAtlas_SpawnAtlasBorder(t *testing.T) {
// Start with a small example
a := NewAtlas(2, 2)
assert.NotNil(t, a)
assert.Equal(t, 4, len(a.Chunks))
assert.NoError(t, a.SpawnAtlasBorder())
tile, err := a.GetTile(Vector{0, 0})
assert.NoError(t, err)
assert.Equal(t, TileEmpty, tile)
tile, err = a.GetTile(Vector{1, 1})
assert.NoError(t, err)
assert.Equal(t, TileWall, tile)
tile, err = a.GetTile(Vector{-1, -1})
assert.NoError(t, err)
assert.Equal(t, TileEmpty, tile)
tile, err = a.GetTile(Vector{-2, -2})
assert.NoError(t, err)
assert.Equal(t, TileWall, tile)
}

View file

@ -4,7 +4,7 @@ package game
type Tile byte
const (
TileEmpty = '_'
TileEmpty = Tile(0)
TileRock = 'o'
TileWall = Tile(1)
)

View file

@ -32,10 +32,15 @@ func NewWorld() *World {
return &World{
Rovers: make(map[uuid.UUID]Rover),
CommandQueue: make(map[uuid.UUID]CommandStream),
Atlas: NewAtlas(2, 8), // TODO: Choose an appropriate world size
Atlas: NewAtlas(4, 8), // TODO: Choose an appropriate world size
}
}
// SpawnWorldBorder spawns a border at the edge of the world atlas
func (w *World) SpawnWorldBorder() error {
return w.Atlas.SpawnAtlasBorder()
}
// SpawnRover adds an rover to the game
func (w *World) SpawnRover() uuid.UUID {
w.worldMutex.Lock()
@ -56,8 +61,8 @@ func (w *World) SpawnRover() uuid.UUID {
// Spawn in a random place near the origin
rover.Attributes.Pos = Vector{
10 - (rand.Int() % 20),
10 - (rand.Int() % 20),
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
}
// TODO: Verify no blockages in this area