Large refactor, move object and tile types out into the proto
This commit is contained in:
parent
24d4fe9273
commit
305f64ec38
9 changed files with 338 additions and 210 deletions
|
@ -4,35 +4,19 @@ import (
|
|||
"log"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
)
|
||||
|
||||
// Tile describes the type of terrain
|
||||
type Tile byte
|
||||
|
||||
const (
|
||||
// TileNone is a keyword for nothing
|
||||
TileNone = Tile(0)
|
||||
|
||||
// TileRock is solid rock ground
|
||||
TileRock = Tile(1)
|
||||
|
||||
// TileGravel is loose rocks
|
||||
TileGravel = Tile(2)
|
||||
|
||||
// TileSand is sand
|
||||
TileSand = Tile(3)
|
||||
)
|
||||
|
||||
// Glyph returns the glyph for this tile type
|
||||
func (t Tile) Glyph() Glyph {
|
||||
// TileGlyph returns the glyph for this tile type
|
||||
func TileGlyph(t roveapi.Tile) Glyph {
|
||||
switch t {
|
||||
case TileNone:
|
||||
case roveapi.Tile_TileNone:
|
||||
return GlyphNone
|
||||
case TileRock:
|
||||
case roveapi.Tile_Rock:
|
||||
return GlyphGroundRock
|
||||
case TileGravel:
|
||||
case roveapi.Tile_Gravel:
|
||||
return GlyphGroundGravel
|
||||
case TileSand:
|
||||
case roveapi.Tile_Sand:
|
||||
return GlyphGroundSand
|
||||
}
|
||||
|
||||
|
@ -43,11 +27,11 @@ func (t Tile) Glyph() Glyph {
|
|||
// Atlas represents a 2D world atlas of tiles and objects
|
||||
type Atlas interface {
|
||||
// SetTile sets a location on the Atlas to a type of tile
|
||||
SetTile(v maths.Vector, tile Tile)
|
||||
SetTile(v maths.Vector, tile roveapi.Tile)
|
||||
|
||||
// SetObject will set a location on the Atlas to contain an object
|
||||
SetObject(v maths.Vector, obj Object)
|
||||
|
||||
// QueryPosition queries a position on the atlas
|
||||
QueryPosition(v maths.Vector) (byte, Object)
|
||||
QueryPosition(v maths.Vector) (roveapi.Tile, Object)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -169,15 +170,15 @@ func TestAtlas_GetSetTile(t *testing.T) {
|
|||
a := NewChunkAtlas(10)
|
||||
assert.NotNil(t, a)
|
||||
|
||||
// Set the origin tile to 1 and test it
|
||||
a.SetTile(maths.Vector{X: 0, Y: 0}, 1)
|
||||
// Set the origin tile and test it
|
||||
a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel)
|
||||
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||
|
||||
// Set another tile to 1 and test it
|
||||
a.SetTile(maths.Vector{X: 5, Y: -2}, 2)
|
||||
// Set another tile and test it
|
||||
a.SetTile(maths.Vector{X: 5, Y: -2}, roveapi.Tile_Rock)
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: 5, Y: -2})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||
}
|
||||
|
||||
func TestAtlas_GetSetObject(t *testing.T) {
|
||||
|
@ -185,14 +186,14 @@ func TestAtlas_GetSetObject(t *testing.T) {
|
|||
assert.NotNil(t, a)
|
||||
|
||||
// Set the origin tile to 1 and test it
|
||||
a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: ObjectRockLarge})
|
||||
a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_RockLarge})
|
||||
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, Object{Type: ObjectRockLarge}, obj)
|
||||
assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj)
|
||||
|
||||
// Set another tile to 1 and test it
|
||||
a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectRockSmall})
|
||||
a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: roveapi.Object_RockSmall})
|
||||
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
|
||||
assert.Equal(t, Object{Type: ObjectRockSmall}, obj)
|
||||
assert.Equal(t, Object{Type: roveapi.Object_RockSmall}, obj)
|
||||
}
|
||||
|
||||
func TestAtlas_Grown(t *testing.T) {
|
||||
|
@ -202,28 +203,28 @@ func TestAtlas_Grown(t *testing.T) {
|
|||
assert.Equal(t, 1, len(a.Chunks))
|
||||
|
||||
// Set a few tiles to values
|
||||
a.SetTile(maths.Vector{X: 0, Y: 0}, 1)
|
||||
a.SetTile(maths.Vector{X: -1, Y: -1}, 2)
|
||||
a.SetTile(maths.Vector{X: 1, Y: -2}, 3)
|
||||
a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel)
|
||||
a.SetTile(maths.Vector{X: -1, Y: -1}, roveapi.Tile_Rock)
|
||||
a.SetTile(maths.Vector{X: 1, Y: -2}, roveapi.Tile_Sand)
|
||||
|
||||
// Check tile values
|
||||
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
|
||||
assert.Equal(t, byte(3), tile)
|
||||
assert.Equal(t, roveapi.Tile_Sand, tile)
|
||||
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, byte(1), tile)
|
||||
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||
assert.Equal(t, byte(2), tile)
|
||||
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||
|
||||
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
|
||||
assert.Equal(t, byte(3), tile)
|
||||
assert.Equal(t, roveapi.Tile_Sand, tile)
|
||||
}
|
||||
|
||||
func TestAtlas_GetSetCorrect(t *testing.T) {
|
||||
|
@ -237,12 +238,12 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
|
|||
assert.Equal(t, 1, len(a.Chunks))
|
||||
|
||||
pos := maths.Vector{X: x, Y: y}
|
||||
a.SetTile(pos, TileRock)
|
||||
a.SetObject(pos, Object{Type: ObjectRockLarge})
|
||||
a.SetTile(pos, roveapi.Tile_Rock)
|
||||
a.SetObject(pos, Object{Type: roveapi.Object_RockLarge})
|
||||
tile, obj := a.QueryPosition(pos)
|
||||
|
||||
assert.Equal(t, TileRock, Tile(tile))
|
||||
assert.Equal(t, Object{Type: ObjectRockLarge}, obj)
|
||||
assert.Equal(t, roveapi.Tile_Rock, roveapi.Tile(tile))
|
||||
assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -259,10 +260,10 @@ func TestAtlas_WorldGen(t *testing.T) {
|
|||
for j := num - 1; j >= 0; j-- {
|
||||
for i := 0; i < num; i++ {
|
||||
t, o := a.QueryPosition(maths.Vector{X: i, Y: j})
|
||||
if o.Type != ObjectNone {
|
||||
fmt.Printf("%c", o.Type)
|
||||
} else if t != byte(TileNone) {
|
||||
fmt.Printf("%c", t)
|
||||
if o.Type != roveapi.Object_ObjectNone {
|
||||
fmt.Printf("%c", ObjectGlyph(o.Type))
|
||||
} else if t != roveapi.Tile_TileNone {
|
||||
fmt.Printf("%c", TileGlyph(t))
|
||||
} else {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"math/rand"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
"github.com/ojrac/opensimplex-go"
|
||||
)
|
||||
|
||||
|
@ -63,7 +64,7 @@ func NewChunkAtlas(chunkSize int) Atlas {
|
|||
}
|
||||
|
||||
// SetTile sets an individual tile's kind
|
||||
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) {
|
||||
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile roveapi.Tile) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
a.setTile(c, local, byte(tile))
|
||||
|
@ -77,13 +78,13 @@ func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj Object) {
|
|||
}
|
||||
|
||||
// QueryPosition will return information for a specific position
|
||||
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, Object) {
|
||||
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (roveapi.Tile, Object) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
a.populate(c)
|
||||
chunk := a.Chunks[c]
|
||||
i := a.chunkTileIndex(local)
|
||||
return chunk.Tiles[i], chunk.Objects[i]
|
||||
return roveapi.Tile(chunk.Tiles[i]), chunk.Objects[i]
|
||||
}
|
||||
|
||||
// chunkTileID returns the tile index within a chunk
|
||||
|
@ -107,28 +108,28 @@ func (a *chunkBasedAtlas) populate(chunk int) {
|
|||
|
||||
// Get the terrain noise value for this location
|
||||
t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale)
|
||||
var tile Tile
|
||||
var tile roveapi.Tile
|
||||
switch {
|
||||
case t > 0.5:
|
||||
tile = TileGravel
|
||||
tile = roveapi.Tile_Gravel
|
||||
case t > 0.05:
|
||||
tile = TileSand
|
||||
tile = roveapi.Tile_Sand
|
||||
default:
|
||||
tile = TileRock
|
||||
tile = roveapi.Tile_Rock
|
||||
}
|
||||
c.Tiles[j*a.ChunkSize+i] = byte(tile)
|
||||
|
||||
// Get the object noise value for this location
|
||||
o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale)
|
||||
var obj = ObjectNone
|
||||
var obj = roveapi.Object_ObjectNone
|
||||
switch {
|
||||
case o > 0.6:
|
||||
obj = ObjectRockLarge
|
||||
obj = roveapi.Object_RockLarge
|
||||
case o > 0.5:
|
||||
obj = ObjectRockSmall
|
||||
obj = roveapi.Object_RockSmall
|
||||
}
|
||||
if obj != ObjectNone {
|
||||
c.Objects[j*a.ChunkSize+i] = Object{Type: ObjectType(obj)}
|
||||
if obj != roveapi.Object_ObjectNone {
|
||||
c.Objects[j*a.ChunkSize+i] = Object{Type: roveapi.Object(obj)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -136,9 +137,9 @@ func (a *chunkBasedAtlas) populate(chunk int) {
|
|||
// Set up any objects
|
||||
for i := 0; i < len(c.Tiles); i++ {
|
||||
if rand.Intn(16) == 0 {
|
||||
c.Objects[i] = Object{Type: ObjectRockLarge}
|
||||
c.Objects[i] = Object{Type: roveapi.Object_RockLarge}
|
||||
} else if rand.Intn(32) == 0 {
|
||||
c.Objects[i] = Object{Type: ObjectRockSmall}
|
||||
c.Objects[i] = Object{Type: roveapi.Object_RockSmall}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,7 +160,7 @@ func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object
|
|||
|
||||
c := a.Chunks[chunk]
|
||||
i := a.chunkTileIndex(local)
|
||||
if object.Type != ObjectNone {
|
||||
if object.Type != roveapi.Object_ObjectNone {
|
||||
c.Objects[i] = object
|
||||
} else {
|
||||
delete(c.Objects, i)
|
||||
|
|
|
@ -1,35 +1,21 @@
|
|||
package atlas
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"log"
|
||||
|
||||
// ObjectType represents an object type
|
||||
type ObjectType byte
|
||||
|
||||
// Types of objects
|
||||
const (
|
||||
// ObjectNone represents no object at all
|
||||
ObjectNone = ObjectType(0)
|
||||
|
||||
// ObjectRover represents a live rover
|
||||
ObjectRoverLive = ObjectType(1)
|
||||
|
||||
// ObjectSmallRock is a small stashable rock
|
||||
ObjectRockSmall = ObjectType(2)
|
||||
|
||||
// ObjectLargeRock is a large blocking rock
|
||||
ObjectRockLarge = ObjectType(3)
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
)
|
||||
|
||||
// Glyph returns the glyph for this object type
|
||||
func (o ObjectType) Glyph() Glyph {
|
||||
// ObjectGlyph returns the glyph for this object type
|
||||
func ObjectGlyph(o roveapi.Object) Glyph {
|
||||
switch o {
|
||||
case ObjectNone:
|
||||
case roveapi.Object_ObjectNone:
|
||||
return GlyphNone
|
||||
case ObjectRoverLive:
|
||||
case roveapi.Object_RoverLive:
|
||||
return GlyphRoverLive
|
||||
case ObjectRockSmall:
|
||||
case roveapi.Object_RockSmall:
|
||||
return GlyphRockSmall
|
||||
case ObjectRockLarge:
|
||||
case roveapi.Object_RockLarge:
|
||||
return GlyphRockLarge
|
||||
}
|
||||
|
||||
|
@ -40,14 +26,14 @@ func (o ObjectType) Glyph() Glyph {
|
|||
// Object represents an object in the world
|
||||
type Object struct {
|
||||
// The type of the object
|
||||
Type ObjectType `json:"type"`
|
||||
Type roveapi.Object `json:"type"`
|
||||
}
|
||||
|
||||
// IsBlocking checks if an object is a blocking object
|
||||
func (o *Object) IsBlocking() bool {
|
||||
var blocking = [...]ObjectType{
|
||||
ObjectRoverLive,
|
||||
ObjectRockLarge,
|
||||
var blocking = [...]roveapi.Object{
|
||||
roveapi.Object_RoverLive,
|
||||
roveapi.Object_RockLarge,
|
||||
}
|
||||
|
||||
for _, t := range blocking {
|
||||
|
@ -60,8 +46,8 @@ func (o *Object) IsBlocking() bool {
|
|||
|
||||
// IsStashable checks if an object is stashable
|
||||
func (o *Object) IsStashable() bool {
|
||||
var stashable = [...]ObjectType{
|
||||
ObjectRockSmall,
|
||||
var stashable = [...]roveapi.Object{
|
||||
roveapi.Object_RockSmall,
|
||||
}
|
||||
|
||||
for _, t := range stashable {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue