Move object into atlas
This commit is contained in:
parent
f40f7123d4
commit
f0ab2abf6e
8 changed files with 66 additions and 72 deletions
|
@ -2,7 +2,6 @@ package atlas
|
|||
|
||||
import (
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
)
|
||||
|
||||
// Tile describes the type of terrain
|
||||
|
@ -28,8 +27,8 @@ type Atlas interface {
|
|||
SetTile(v maths.Vector, tile Tile)
|
||||
|
||||
// SetObject will set a location on the Atlas to contain an object
|
||||
SetObject(v maths.Vector, obj objects.Object)
|
||||
SetObject(v maths.Vector, obj Object)
|
||||
|
||||
// QueryPosition queries a position on the atlas
|
||||
QueryPosition(v maths.Vector) (byte, objects.Object)
|
||||
QueryPosition(v maths.Vector) (byte, Object)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -186,14 +185,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}, objects.Object{Type: objects.LargeRock})
|
||||
a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: ObjectLargeRock})
|
||||
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
|
||||
assert.Equal(t, Object{Type: ObjectLargeRock}, obj)
|
||||
|
||||
// Set another tile to 1 and test it
|
||||
a.SetObject(maths.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
|
||||
a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: ObjectSmallRock})
|
||||
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
|
||||
assert.Equal(t, objects.Object{Type: objects.SmallRock}, obj)
|
||||
assert.Equal(t, Object{Type: ObjectSmallRock}, obj)
|
||||
}
|
||||
|
||||
func TestAtlas_Grown(t *testing.T) {
|
||||
|
@ -239,11 +238,11 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
|
|||
|
||||
pos := maths.Vector{X: x, Y: y}
|
||||
a.SetTile(pos, TileRock)
|
||||
a.SetObject(pos, objects.Object{Type: objects.LargeRock})
|
||||
a.SetObject(pos, Object{Type: ObjectLargeRock})
|
||||
tile, obj := a.QueryPosition(pos)
|
||||
|
||||
assert.Equal(t, TileRock, Tile(tile))
|
||||
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
|
||||
assert.Equal(t, Object{Type: ObjectLargeRock}, obj)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -260,7 +259,7 @@ 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 != objects.None {
|
||||
if o.Type != ObjectNone {
|
||||
fmt.Printf("%c", o.Type)
|
||||
} else if t != byte(TileNone) {
|
||||
fmt.Printf("%c", t)
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"math/rand"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/ojrac/opensimplex-go"
|
||||
)
|
||||
|
||||
|
@ -16,7 +15,7 @@ type chunk struct {
|
|||
|
||||
// Objects represents the objects within the chunk
|
||||
// only one possible object per tile for now
|
||||
Objects map[int]objects.Object `json:"objects"`
|
||||
Objects map[int]Object `json:"objects"`
|
||||
}
|
||||
|
||||
// chunkBasedAtlas represents a grid of Chunks
|
||||
|
@ -71,14 +70,14 @@ func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) {
|
|||
}
|
||||
|
||||
// SetObject sets the object on a tile
|
||||
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj objects.Object) {
|
||||
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj Object) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
a.setObject(c, local, obj)
|
||||
}
|
||||
|
||||
// QueryPosition will return information for a specific position
|
||||
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, objects.Object) {
|
||||
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, Object) {
|
||||
c := a.worldSpaceToChunkWithGrow(v)
|
||||
local := a.worldSpaceToChunkLocal(v)
|
||||
a.populate(c)
|
||||
|
@ -100,7 +99,7 @@ func (a *chunkBasedAtlas) populate(chunk int) {
|
|||
}
|
||||
|
||||
c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize)
|
||||
c.Objects = make(map[int]objects.Object)
|
||||
c.Objects = make(map[int]Object)
|
||||
|
||||
origin := a.chunkOriginInWorldSpace(chunk)
|
||||
for i := 0; i < a.ChunkSize; i++ {
|
||||
|
@ -121,15 +120,15 @@ func (a *chunkBasedAtlas) populate(chunk int) {
|
|||
|
||||
// Get the object noise value for this location
|
||||
o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale)
|
||||
var obj = objects.None
|
||||
var obj = ObjectNone
|
||||
switch {
|
||||
case o > 0.6:
|
||||
obj = objects.LargeRock
|
||||
obj = ObjectLargeRock
|
||||
case o > 0.5:
|
||||
obj = objects.SmallRock
|
||||
obj = ObjectSmallRock
|
||||
}
|
||||
if obj != objects.None {
|
||||
c.Objects[j*a.ChunkSize+i] = objects.Object{Type: obj}
|
||||
if obj != ObjectNone {
|
||||
c.Objects[j*a.ChunkSize+i] = Object{Type: obj}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -137,9 +136,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] = objects.Object{Type: objects.LargeRock}
|
||||
c.Objects[i] = Object{Type: ObjectLargeRock}
|
||||
} else if rand.Intn(32) == 0 {
|
||||
c.Objects[i] = objects.Object{Type: objects.SmallRock}
|
||||
c.Objects[i] = Object{Type: ObjectSmallRock}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -155,12 +154,12 @@ func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
|
|||
}
|
||||
|
||||
// setObject sets an object in a specific chunk
|
||||
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object objects.Object) {
|
||||
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object) {
|
||||
a.populate(chunk)
|
||||
|
||||
c := a.Chunks[chunk]
|
||||
i := a.chunkTileIndex(local)
|
||||
if object.Type != objects.None {
|
||||
if object.Type != ObjectNone {
|
||||
c.Objects[i] = object
|
||||
} else {
|
||||
delete(c.Objects, i)
|
||||
|
|
53
pkg/atlas/objects.go
Normal file
53
pkg/atlas/objects.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package atlas
|
||||
|
||||
// Type represents an object type
|
||||
type Type byte
|
||||
|
||||
// Types of objects
|
||||
const (
|
||||
// ObjectNone represents no object at all
|
||||
ObjectNone = Type(0)
|
||||
|
||||
// ObjectRover represents a live rover
|
||||
ObjectRover = Type('R')
|
||||
|
||||
// ObjectSmallRock is a small stashable rock
|
||||
ObjectSmallRock = Type('o')
|
||||
|
||||
// ObjectLargeRock is a large blocking rock
|
||||
ObjectLargeRock = Type('O')
|
||||
)
|
||||
|
||||
// Object represents an object in the world
|
||||
type Object struct {
|
||||
Type Type `json:"type"`
|
||||
}
|
||||
|
||||
// IsBlocking checks if an object is a blocking object
|
||||
func (o *Object) IsBlocking() bool {
|
||||
var blocking = [...]Type{
|
||||
ObjectRover,
|
||||
ObjectLargeRock,
|
||||
}
|
||||
|
||||
for _, t := range blocking {
|
||||
if o.Type == t {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsStashable checks if an object is stashable
|
||||
func (o *Object) IsStashable() bool {
|
||||
var stashable = [...]Type{
|
||||
ObjectSmallRock,
|
||||
}
|
||||
|
||||
for _, t := range stashable {
|
||||
if o.Type == t {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue