Move vector into maths package

This commit is contained in:
Marc Di Luzio 2020-07-10 18:22:59 +01:00
parent 97d3be000b
commit 5b1fe61097
11 changed files with 131 additions and 134 deletions

View file

@ -1,8 +1,8 @@
package atlas
import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
)
// Tile describes the type of terrain
@ -25,11 +25,11 @@ const (
// 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 vector.Vector, tile Tile)
SetTile(v maths.Vector, tile Tile)
// SetObject will set a location on the Atlas to contain an object
SetObject(v vector.Vector, obj objects.Object)
SetObject(v maths.Vector, obj objects.Object)
// QueryPosition queries a position on the atlas
QueryPosition(v vector.Vector) (byte, objects.Object)
QueryPosition(v maths.Vector) (byte, objects.Object)
}

View file

@ -4,8 +4,8 @@ import (
"fmt"
"testing"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
"github.com/stretchr/testify/assert"
)
@ -21,49 +21,49 @@ func TestAtlas_toChunk(t *testing.T) {
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -1, Y: -1})
a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.QueryPosition(maths.Vector{X: -1, Y: -1})
a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
chunkID := a.worldSpaceToChunkIndex(vector.Vector{X: 0, Y: 0})
chunkID := a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: 0})
assert.Equal(t, 3, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 0, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: -1})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: 0})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: 0})
assert.Equal(t, 2, chunkID)
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -2, Y: -2})
a.QueryPosition(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: 1, Y: 1})
a.QueryPosition(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 3, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: -2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -2})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: -2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 1})
assert.Equal(t, 2, chunkID)
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: -3, Y: -3})
a.QueryPosition(maths.Vector{X: -3, Y: -3})
assert.Equal(t, 4*4, len(a.Chunks))
// Chunks should look like:
@ -74,19 +74,19 @@ func TestAtlas_toChunk(t *testing.T) {
// 4 | 5 || 6 | 7
// ----------------
// 0 | 1 || 2 | 3
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 3})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 3})
assert.Equal(t, 14, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: -3})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -3})
assert.Equal(t, 2, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -1, Y: -1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 5, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: -2, Y: 2})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 2})
assert.Equal(t, 13, chunkID)
a = NewChunkAtlas(3).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
@ -94,13 +94,13 @@ func TestAtlas_toChunk(t *testing.T) {
// -------
// || 0| 1
// =======
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 0, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 3, Y: 1})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 3, Y: 1})
assert.Equal(t, 1, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 1, Y: 4})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 4})
assert.Equal(t, 2, chunkID)
chunkID = a.worldSpaceToChunkIndex(vector.Vector{X: 5, Y: 5})
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 5, Y: 5})
assert.Equal(t, 3, chunkID)
}
@ -109,36 +109,36 @@ func TestAtlas_toWorld(t *testing.T) {
assert.NotNil(t, a)
// Get a tile to spawn some chunks
a.QueryPosition(vector.Vector{X: -1, Y: -1})
a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
assert.Equal(t, vector.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1))
assert.Equal(t, maths.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1))
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn the chunks
a.QueryPosition(vector.Vector{X: -2, Y: -2})
a.QueryPosition(maths.Vector{X: -2, Y: -2})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: 1, Y: 1})
a.QueryPosition(maths.Vector{X: 1, Y: 1})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
// 2 | 3
// -----
// 0 | 1
assert.Equal(t, vector.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2))
assert.Equal(t, maths.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2))
a = NewChunkAtlas(2).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
a.QueryPosition(vector.Vector{X: -3, Y: -3})
a.QueryPosition(maths.Vector{X: -3, Y: -3})
assert.Equal(t, 4*4, len(a.Chunks))
// Chunks should look like:
@ -149,13 +149,13 @@ func TestAtlas_toWorld(t *testing.T) {
// 4 | 5 || 6 | 7
// ----------------
// 0 | 1 || 2 | 3
assert.Equal(t, vector.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, vector.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7))
assert.Equal(t, maths.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7))
a = NewChunkAtlas(3).(*chunkBasedAtlas)
assert.NotNil(t, a)
// Get a tile to spawn a 4x4 grid of chunks
a.QueryPosition(vector.Vector{X: 3, Y: 3})
a.QueryPosition(maths.Vector{X: 3, Y: 3})
assert.Equal(t, 2*2, len(a.Chunks))
// Chunks should look like:
@ -163,7 +163,7 @@ func TestAtlas_toWorld(t *testing.T) {
// -------
// || 0| 1
// =======
assert.Equal(t, vector.Vector{X: 0, Y: 0}, a.chunkOriginInWorldSpace(0))
assert.Equal(t, maths.Vector{X: 0, Y: 0}, a.chunkOriginInWorldSpace(0))
}
func TestAtlas_GetSetTile(t *testing.T) {
@ -171,13 +171,13 @@ func TestAtlas_GetSetTile(t *testing.T) {
assert.NotNil(t, a)
// Set the origin tile to 1 and test it
a.SetTile(vector.Vector{X: 0, Y: 0}, 1)
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.SetTile(maths.Vector{X: 0, Y: 0}, 1)
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
// Set another tile to 1 and test it
a.SetTile(vector.Vector{X: 5, Y: -2}, 2)
tile, _ = a.QueryPosition(vector.Vector{X: 5, Y: -2})
a.SetTile(maths.Vector{X: 5, Y: -2}, 2)
tile, _ = a.QueryPosition(maths.Vector{X: 5, Y: -2})
assert.Equal(t, byte(2), tile)
}
@ -186,13 +186,13 @@ func TestAtlas_GetSetObject(t *testing.T) {
assert.NotNil(t, a)
// Set the origin tile to 1 and test it
a.SetObject(vector.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
_, obj := a.QueryPosition(vector.Vector{X: 0, Y: 0})
a.SetObject(maths.Vector{X: 0, Y: 0}, objects.Object{Type: objects.LargeRock})
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, objects.Object{Type: objects.LargeRock}, obj)
// Set another tile to 1 and test it
a.SetObject(vector.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
_, obj = a.QueryPosition(vector.Vector{X: 5, Y: -2})
a.SetObject(maths.Vector{X: 5, Y: -2}, objects.Object{Type: objects.SmallRock})
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
assert.Equal(t, objects.Object{Type: objects.SmallRock}, obj)
}
@ -203,27 +203,27 @@ func TestAtlas_Grown(t *testing.T) {
assert.Equal(t, 1, len(a.Chunks))
// Set a few tiles to values
a.SetTile(vector.Vector{X: 0, Y: 0}, 1)
a.SetTile(vector.Vector{X: -1, Y: -1}, 2)
a.SetTile(vector.Vector{X: 1, Y: -2}, 3)
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)
// Check tile values
tile, _ := a.QueryPosition(vector.Vector{X: 0, Y: 0})
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, byte(2), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
assert.Equal(t, byte(3), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 0, Y: 0})
tile, _ = a.QueryPosition(maths.Vector{X: 0, Y: 0})
assert.Equal(t, byte(1), tile)
tile, _ = a.QueryPosition(vector.Vector{X: -1, Y: -1})
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
assert.Equal(t, byte(2), tile)
tile, _ = a.QueryPosition(vector.Vector{X: 1, Y: -2})
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
assert.Equal(t, byte(3), tile)
}
@ -237,7 +237,7 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
assert.NotNil(t, a)
assert.Equal(t, 1, len(a.Chunks))
pos := vector.Vector{X: x, Y: y}
pos := maths.Vector{X: x, Y: y}
a.SetTile(pos, TileRock)
a.SetObject(pos, objects.Object{Type: objects.LargeRock})
tile, obj := a.QueryPosition(pos)
@ -253,13 +253,13 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
func TestAtlas_WorldGen(t *testing.T) {
a := NewChunkAtlas(8)
// Spawn a large world
_, _ = a.QueryPosition(vector.Vector{X: 20, Y: 20})
_, _ = a.QueryPosition(maths.Vector{X: 20, Y: 20})
// Print out the world for manual evaluation
num := 20
for j := num - 1; j >= 0; j-- {
for i := 0; i < num; i++ {
t, o := a.QueryPosition(vector.Vector{X: i, Y: j})
t, o := a.QueryPosition(maths.Vector{X: i, Y: j})
if o.Type != objects.None {
fmt.Printf("%c", o.Type)
} else if t != byte(TileNone) {

View file

@ -6,7 +6,6 @@ import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
"github.com/ojrac/opensimplex-go"
)
@ -27,10 +26,10 @@ type chunkBasedAtlas struct {
Chunks []chunk `json:"chunks"`
// LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value)
LowerBound vector.Vector `json:"lowerBound"`
LowerBound maths.Vector `json:"lowerBound"`
// UpperBound is the top left corner of the current chunks (curent chunks cover < this value)
UpperBound vector.Vector `json:"upperBound"`
UpperBound maths.Vector `json:"upperBound"`
// ChunkSize is the x/y dimensions of each square chunk
ChunkSize int `json:"chunksize"`
@ -54,8 +53,8 @@ func NewChunkAtlas(chunkSize int) Atlas {
a := chunkBasedAtlas{
ChunkSize: chunkSize,
Chunks: make([]chunk, 1),
LowerBound: vector.Vector{X: 0, Y: 0},
UpperBound: vector.Vector{X: chunkSize, Y: chunkSize},
LowerBound: maths.Vector{X: 0, Y: 0},
UpperBound: maths.Vector{X: chunkSize, Y: chunkSize},
terrainNoise: opensimplex.New(noiseSeed),
objectNoise: opensimplex.New(noiseSeed),
}
@ -65,21 +64,21 @@ func NewChunkAtlas(chunkSize int) Atlas {
}
// SetTile sets an individual tile's kind
func (a *chunkBasedAtlas) SetTile(v vector.Vector, tile Tile) {
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile Tile) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.setTile(c, local, byte(tile))
}
// SetObject sets the object on a tile
func (a *chunkBasedAtlas) SetObject(v vector.Vector, obj objects.Object) {
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj objects.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 vector.Vector) (byte, objects.Object) {
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (byte, objects.Object) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.populate(c)
@ -89,7 +88,7 @@ func (a *chunkBasedAtlas) QueryPosition(v vector.Vector) (byte, objects.Object)
}
// chunkTileID returns the tile index within a chunk
func (a *chunkBasedAtlas) chunkTileIndex(local vector.Vector) int {
func (a *chunkBasedAtlas) chunkTileIndex(local maths.Vector) int {
return local.X + local.Y*a.ChunkSize
}
@ -148,7 +147,7 @@ func (a *chunkBasedAtlas) populate(chunk int) {
}
// setTile sets a tile in a specific chunk
func (a *chunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) {
func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
a.populate(chunk)
c := a.Chunks[chunk]
c.Tiles[a.chunkTileIndex(local)] = tile
@ -156,7 +155,7 @@ func (a *chunkBasedAtlas) setTile(chunk int, local vector.Vector, tile byte) {
}
// setObject sets an object in a specific chunk
func (a *chunkBasedAtlas) setObject(chunk int, local vector.Vector, object objects.Object) {
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object objects.Object) {
a.populate(chunk)
c := a.Chunks[chunk]
@ -170,12 +169,12 @@ func (a *chunkBasedAtlas) setObject(chunk int, local vector.Vector, object objec
}
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v vector.Vector) vector.Vector {
return vector.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v maths.Vector) maths.Vector {
return maths.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
}
// worldSpaceToChunkID gets the current chunk ID for a position in the world
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int {
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v maths.Vector) int {
// Shift the vector by our current min
v = v.Added(a.LowerBound.Negated())
@ -191,13 +190,13 @@ func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v vector.Vector) int {
}
// chunkOriginInWorldSpace returns the origin of the chunk in world space
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector {
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) maths.Vector {
// Calculate the width
width := a.UpperBound.X - a.LowerBound.X
widthInChunks := width / a.ChunkSize
// Reverse the along the corridor and up the stairs
v := vector.Vector{
v := maths.Vector{
X: chunk % widthInChunks,
Y: chunk / widthInChunks,
}
@ -208,15 +207,15 @@ func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) vector.Vector {
}
// getNewBounds gets new lower and upper bounds for the world space given a vector
func (a *chunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, upper vector.Vector) {
lower = vector.Min(v, a.LowerBound)
upper = vector.Max(v.Added(vector.Vector{X: 1, Y: 1}), a.UpperBound)
func (a *chunkBasedAtlas) getNewBounds(v maths.Vector) (lower maths.Vector, upper maths.Vector) {
lower = maths.Min2(v, a.LowerBound)
upper = maths.Max2(v.Added(maths.Vector{X: 1, Y: 1}), a.UpperBound)
lower = vector.Vector{
lower = maths.Vector{
X: maths.RoundDown(lower.X, a.ChunkSize),
Y: maths.RoundDown(lower.Y, a.ChunkSize),
}
upper = vector.Vector{
upper = maths.Vector{
X: maths.RoundUp(upper.X, a.ChunkSize),
Y: maths.RoundUp(upper.Y, a.ChunkSize),
}
@ -224,7 +223,7 @@ func (a *chunkBasedAtlas) getNewBounds(v vector.Vector) (lower vector.Vector, up
}
// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v vector.Vector) int {
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v maths.Vector) int {
// If we're within bounds, just return the current chunk
if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y {
return a.worldSpaceToChunkIndex(v)