2020-07-04 19:25:53 -04:00
|
|
|
package d2vector
|
|
|
|
|
|
|
|
import (
|
2020-07-11 18:11:26 -04:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2020-07-04 19:25:53 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
func TestEntityPosition(t *testing.T) {
|
|
|
|
x, y := rand.Intn(1000), rand.Intn(1000)
|
|
|
|
pos := EntityPosition(x, y)
|
|
|
|
locX, locY := float64(x), float64(y)
|
|
|
|
|
|
|
|
// old coordinate values Position equivalent
|
|
|
|
locationX := locX // .SubWord().X()
|
|
|
|
locationY := locY // .SubWord().Y()
|
|
|
|
tileX := float64(x / 5) // .Tile().X()
|
|
|
|
tileY := float64(y / 5) // .Tile().Y()
|
|
|
|
subcellX := 1 + math.Mod(locX, 5) // .SubCell().X()
|
|
|
|
subcellY := 1 + math.Mod(locY, 5) // .SubCell().Y()
|
|
|
|
|
|
|
|
want := NewVector(tileX, tileY)
|
|
|
|
got := pos.Tile()
|
|
|
|
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Errorf("world position should match old value: got %s: want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
want = NewVector(subcellX, subcellY)
|
|
|
|
got = pos.SubCell()
|
|
|
|
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Errorf("sub cell position should match old value: got %s: want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
want = NewVector(locationX, locationY)
|
|
|
|
got = pos.SubWorld()
|
|
|
|
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Errorf("sub tile position should match old value: got %s: want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(description string, t *testing.T, original, got, want, unchanged Vector) {
|
2020-07-09 08:30:55 -04:00
|
|
|
if !got.EqualsApprox(want) {
|
2020-07-11 18:11:26 -04:00
|
|
|
t.Errorf("%s: want %s: got %s", description, want, got)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 08:30:55 -04:00
|
|
|
if !original.EqualsApprox(unchanged) {
|
2020-07-04 19:25:53 -04:00
|
|
|
t.Errorf("Position value %s was incorrectly changed to %s when calling this method", unchanged, original)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTile(t *testing.T) {
|
|
|
|
p := NewPosition(1.6, 1.6)
|
|
|
|
got := p.Tile()
|
2020-07-09 08:30:55 -04:00
|
|
|
want := NewVector(1, 1)
|
|
|
|
unchanged := NewVector(1.6, 1.6)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
validate("tile position", t, p.Vector, *got, want, unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTileOffset(t *testing.T) {
|
|
|
|
p := NewPosition(1.6, 1.6)
|
|
|
|
got := p.TileOffset()
|
2020-07-09 08:30:55 -04:00
|
|
|
want := NewVector(0.6, 0.6)
|
|
|
|
unchanged := NewVector(1.6, 1.6)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
validate("tile offset", t, p.Vector, *got, want, unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubWorld(t *testing.T) {
|
|
|
|
p := NewPosition(1, 1)
|
|
|
|
got := p.SubWorld()
|
2020-07-09 08:30:55 -04:00
|
|
|
want := NewVector(5, 5)
|
|
|
|
unchanged := NewVector(1, 1)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
validate("sub tile world position", t, p.Vector, *got, want, unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubTile(t *testing.T) {
|
|
|
|
p := NewPosition(1, 1)
|
|
|
|
got := p.SubTile()
|
2020-07-09 08:30:55 -04:00
|
|
|
want := NewVector(5, 5)
|
|
|
|
unchanged := NewVector(1, 1)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
validate("sub tile with offset", t, p.Vector, *got, want, unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubTileOffset(t *testing.T) {
|
|
|
|
p := NewPosition(1.1, 1.1)
|
|
|
|
got := p.SubTileOffset()
|
2020-07-09 08:30:55 -04:00
|
|
|
want := NewVector(0.5, 0.5)
|
|
|
|
unchanged := NewVector(1.1, 1.1)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-11 18:11:26 -04:00
|
|
|
validate("offset from sub tile", t, p.Vector, *got, want, unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|