Pepper the world with rocks
This commit is contained in:
parent
43648926ca
commit
e82fb3dbfe
4 changed files with 21 additions and 8 deletions
|
@ -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++ {
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue