Pepper the world with rocks

This commit is contained in:
Marc Di Luzio 2020-06-07 22:36:11 +01:00
parent 43648926ca
commit e82fb3dbfe
4 changed files with 21 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package game
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
) )
// Chunk represents a fixed square grid of tiles // Chunk represents a fixed square grid of tiles
@ -46,9 +47,21 @@ func NewAtlas(size int, chunkSize int) Atlas {
return a return a
} }
// SpawnAtlasBorder surrounds the current atlas in a border wall // SpawnWorld spawns the current world
func (a *Atlas) SpawnAtlasBorder() error { func (a *Atlas) SpawnWorld() error {
extent := a.ChunkSize * (a.Size / 2) extent := a.ChunkSize * (a.Size / 2)
// Pepper the current world with rocks
for i := -extent; i < extent; i++ {
for j := -extent; j < extent; j++ {
if rand.Int()%16 == 0 {
if err := a.SetTile(Vector{i, j}, TileRock); err != nil {
return err
}
}
}
}
// Surround the atlas in walls // Surround the atlas in walls
for i := -extent; i < extent; i++ { for i := -extent; i < extent; i++ {

View file

@ -135,13 +135,13 @@ func TestAtlas_Grown(t *testing.T) {
assert.Equal(t, Tile(3), tile) assert.Equal(t, Tile(3), tile)
} }
func TestAtlas_SpawnAtlasBorder(t *testing.T) { func TestAtlas_SpawnWorld(t *testing.T) {
// Start with a small example // Start with a small example
a := NewAtlas(2, 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))
assert.NoError(t, a.SpawnAtlasBorder()) assert.NoError(t, a.SpawnWorld())
tile, err := a.GetTile(Vector{0, 0}) tile, err := a.GetTile(Vector{0, 0})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, TileEmpty, tile) assert.Equal(t, TileEmpty, tile)

View file

@ -5,8 +5,8 @@ type Tile byte
const ( const (
TileEmpty = Tile(0) TileEmpty = Tile(0)
TileRover = Tile(1)
TileWall = Tile(1) TileWall = Tile(2)
TileRock = Tile(3)
TileRover = Tile(2)
) )

View file

@ -38,7 +38,7 @@ func NewWorld() *World {
// SpawnWorldBorder spawns a border at the edge of the world atlas // SpawnWorldBorder spawns a border at the edge of the world atlas
func (w *World) SpawnWorldBorder() error { func (w *World) SpawnWorldBorder() error {
return w.Atlas.SpawnAtlasBorder() return w.Atlas.SpawnWorld()
} }
// SpawnRover adds an rover to the game // SpawnRover adds an rover to the game