From e82fb3dbfe9cad5905aea2b448501c942dd5951b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 7 Jun 2020 22:36:11 +0100 Subject: [PATCH] Pepper the world with rocks --- pkg/game/atlas.go | 17 +++++++++++++++-- pkg/game/atlas_test.go | 4 ++-- pkg/game/tile.go | 6 +++--- pkg/game/world.go | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pkg/game/atlas.go b/pkg/game/atlas.go index 4f9f684..d5d9a5a 100644 --- a/pkg/game/atlas.go +++ b/pkg/game/atlas.go @@ -3,6 +3,7 @@ package game import ( "fmt" "log" + "math/rand" ) // Chunk represents a fixed square grid of tiles @@ -46,9 +47,21 @@ func NewAtlas(size int, chunkSize int) Atlas { return a } -// SpawnAtlasBorder surrounds the current atlas in a border wall -func (a *Atlas) SpawnAtlasBorder() error { +// SpawnWorld spawns the current world +func (a *Atlas) SpawnWorld() error { 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 for i := -extent; i < extent; i++ { diff --git a/pkg/game/atlas_test.go b/pkg/game/atlas_test.go index 9571f8a..bb7fee5 100644 --- a/pkg/game/atlas_test.go +++ b/pkg/game/atlas_test.go @@ -135,13 +135,13 @@ func TestAtlas_Grown(t *testing.T) { assert.Equal(t, Tile(3), tile) } -func TestAtlas_SpawnAtlasBorder(t *testing.T) { +func TestAtlas_SpawnWorld(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()) + assert.NoError(t, a.SpawnWorld()) tile, err := a.GetTile(Vector{0, 0}) assert.NoError(t, err) assert.Equal(t, TileEmpty, tile) diff --git a/pkg/game/tile.go b/pkg/game/tile.go index b0d1fb8..954f571 100644 --- a/pkg/game/tile.go +++ b/pkg/game/tile.go @@ -5,8 +5,8 @@ type Tile byte const ( TileEmpty = Tile(0) + TileRover = Tile(1) - TileWall = Tile(1) - - TileRover = Tile(2) + TileWall = Tile(2) + TileRock = Tile(3) ) diff --git a/pkg/game/world.go b/pkg/game/world.go index f6a3965..68ece50 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -38,7 +38,7 @@ func NewWorld() *World { // SpawnWorldBorder spawns a border at the edge of the world atlas func (w *World) SpawnWorldBorder() error { - return w.Atlas.SpawnAtlasBorder() + return w.Atlas.SpawnWorld() } // SpawnRover adds an rover to the game