1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-07-01 19:35:22 +00:00
OpenDiablo2/d2common/d2math/d2vector/position_test.go
danhale-git 894c60f77a
Map entity rework - vectors in mapEntity (#579)
* Fixed NaN on normalize 0 vector.

* Tentative implementation in getStepLength.

* mapEntity.TileX/Y removed.

* Fixed Position and Target not being initialised in createMapEntity.

* mapEntity.Position is no longer embedded.

* mapEntity.LocationX/Y no longer used outside map_entity.go.

* mapEntity.subCellX/Y removed.

* mapEntity.LocationX/Y removed.

* mapEntity.OffsetX/Y and TargetX/Y removed.

* Direction method added to Vector, returns 0-64 direction.

* Moved Vector.Direction() to Position.DirectionTo and refactored.

* Renamed RenderOffset from SubCell and tested IsZero.

* d2math.WrapInt added for use with directions.

* Tidied up position and tests.

* Refactored d2common.AdjustWithRemainder into d2mapEntity.

* Tidying up d2mapEntity.

* Final cleanup.

* Lint warnings.

* Spelling correction.
2020-07-13 09:06:50 -04:00

80 lines
2.0 KiB
Go

package d2vector
import (
"math"
"math/rand"
"testing"
)
func TestNewPosition(t *testing.T) {
x, y := rand.Intn(1000), rand.Intn(1000)
locX, locY := float64(x), float64(y)
pos := NewPosition(locX, locY)
// 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) // .RenderOffset().X()
subcellY := 1 + math.Mod(locY, 5) // .RenderOffset().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.RenderOffset()
if !got.Equals(want) {
t.Errorf("render offset position should match old value: got %s: want %s", got, want)
}
want = NewVector(locationX, locationY)
got = &pos.Vector
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) {
if !got.EqualsApprox(want) {
t.Errorf("%s: want %s: got %s", description, want, got)
}
if !original.EqualsApprox(unchanged) {
t.Errorf("Position value %s was incorrectly changed to %s when calling this method", unchanged, original)
}
}
func TestPosition_World(t *testing.T) {
p := NewPosition(5, 10)
unchanged := p.Clone()
got := p.World()
want := NewVector(1, 2)
validate("world position", t, p.Vector, *got, want, unchanged)
}
func TestPosition_Tile(t *testing.T) {
p := NewPosition(23, 24)
unchanged := p.Clone()
got := p.Tile()
want := NewVector(4, 4)
validate("tile position", t, p.Vector, *got, want, unchanged)
}
func TestPosition_RenderOffset(t *testing.T) {
p := NewPosition(12.1, 14.2)
unchanged := p.Clone()
got := p.RenderOffset()
want := NewVector(3.1, 5.2)
validate("offset from sub tile", t, p.Vector, *got, want, unchanged)
}