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:
parent
cb725c96d2
commit
3781a4d10d
5 changed files with 59 additions and 5 deletions
|
@ -46,6 +46,26 @@ func NewAtlas(size int, chunkSize int) Atlas {
|
||||||
return a
|
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
|
// SetTile sets an individual tile's kind
|
||||||
func (a *Atlas) SetTile(v Vector, tile Tile) error {
|
func (a *Atlas) SetTile(v Vector, tile Tile) error {
|
||||||
chunk := a.ToChunk(v)
|
chunk := a.ToChunk(v)
|
||||||
|
|
|
@ -134,3 +134,27 @@ func TestAtlas_Grown(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, Tile(3), tile)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ package game
|
||||||
type Tile byte
|
type Tile byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TileEmpty = '_'
|
TileEmpty = Tile(0)
|
||||||
|
|
||||||
TileRock = 'o'
|
TileWall = Tile(1)
|
||||||
)
|
)
|
||||||
|
|
|
@ -32,10 +32,15 @@ func NewWorld() *World {
|
||||||
return &World{
|
return &World{
|
||||||
Rovers: make(map[uuid.UUID]Rover),
|
Rovers: make(map[uuid.UUID]Rover),
|
||||||
CommandQueue: make(map[uuid.UUID]CommandStream),
|
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
|
// SpawnRover adds an rover to the game
|
||||||
func (w *World) SpawnRover() uuid.UUID {
|
func (w *World) SpawnRover() uuid.UUID {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
|
@ -56,8 +61,8 @@ func (w *World) SpawnRover() uuid.UUID {
|
||||||
|
|
||||||
// Spawn in a random place near the origin
|
// Spawn in a random place near the origin
|
||||||
rover.Attributes.Pos = Vector{
|
rover.Attributes.Pos = Vector{
|
||||||
10 - (rand.Int() % 20),
|
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
|
||||||
10 - (rand.Int() % 20),
|
w.Atlas.ChunkSize - (rand.Int() % (w.Atlas.ChunkSize * 2)),
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Verify no blockages in this area
|
// TODO: Verify no blockages in this area
|
||||||
|
|
|
@ -109,6 +109,11 @@ func (s *Server) Initialise() (err error) {
|
||||||
// Add to our sync
|
// Add to our sync
|
||||||
s.sync.Add(1)
|
s.sync.Add(1)
|
||||||
|
|
||||||
|
// Spawn a border on the default world
|
||||||
|
if err := s.world.SpawnWorldBorder(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Load the accounts if requested
|
// Load the accounts if requested
|
||||||
if err := s.LoadAll(); err != nil {
|
if err := s.LoadAll(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue