mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-20 07:27:19 -05:00
Implemented Position in Path and PathTile (#605)
* Unnecessary constructor assignments removed. * Position implemented in d2common.Path * Position implemented in mapEntity.Step. * Position implemented in d2common.PathTile.
This commit is contained in:
parent
dbc07e2ce8
commit
727e8244c6
@ -4,6 +4,7 @@ import (
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
)
|
||||
|
||||
const maxActNumber = 5
|
||||
@ -223,8 +224,9 @@ func (ds1 *DS1) loadNpcPaths(br *d2common.StreamReader, objIdx, numPaths int) {
|
||||
|
||||
for pathIdx := 0; pathIdx < numPaths; pathIdx++ {
|
||||
newPath := d2common.Path{}
|
||||
newPath.X = int(br.GetInt32())
|
||||
newPath.Y = int(br.GetInt32())
|
||||
newPath.Position = d2vector.NewPosition(
|
||||
float64(br.GetInt32()),
|
||||
float64(br.GetInt32()))
|
||||
|
||||
if ds1.Version >= 15 { //nolint:gomnd // Version number
|
||||
newPath.Action = int(br.GetInt32())
|
||||
|
@ -28,6 +28,14 @@ func NewPosition(x, y float64) Position {
|
||||
return p
|
||||
}
|
||||
|
||||
// NewPosition returns a Position struct with the given tile coordinates where 1 = 1 tile, with a fractional offset.
|
||||
func NewPositionTile(x, y float64) Position {
|
||||
p := Position{NewVector(x*subTilesPerTile, y*subTilesPerTile)}
|
||||
p.checkValues()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// Set sets this position to the given sub tile coordinates where 1 = 1 sub tile, with a fractional offset.
|
||||
func (p *Position) Set(x, y float64) {
|
||||
p.x, p.y = x, y
|
||||
|
@ -1,8 +1,9 @@
|
||||
package d2common
|
||||
|
||||
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
|
||||
// Path represents a path
|
||||
type Path struct {
|
||||
X int
|
||||
Y int
|
||||
Action int
|
||||
Position d2vector.Position
|
||||
Action int
|
||||
}
|
||||
|
@ -1,12 +1,18 @@
|
||||
package d2common
|
||||
|
||||
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2astar"
|
||||
import (
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2astar"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
)
|
||||
|
||||
// PathTile represents a node in path finding
|
||||
type PathTile struct {
|
||||
Walkable bool
|
||||
Up, Down, Left, Right, UpLeft, UpRight, DownLeft, DownRight *PathTile
|
||||
X, Y float64
|
||||
Walkable bool
|
||||
Up, Down *PathTile
|
||||
Left, Right *PathTile
|
||||
UpLeft, UpRight *PathTile
|
||||
DownLeft, DownRight *PathTile
|
||||
Position d2vector.Position
|
||||
}
|
||||
|
||||
// PathNeighbors returns the direct neighboring nodes of this node which can be pathed to
|
||||
@ -54,20 +60,9 @@ func (t *PathTile) PathNeighborCost(to d2astar.Pather) float64 {
|
||||
|
||||
// PathEstimatedCost is a heuristic method for estimating movement costs between non-adjacent nodes
|
||||
func (t *PathTile) PathEstimatedCost(to d2astar.Pather) float64 {
|
||||
toT := to.(*PathTile)
|
||||
absX := toT.X - t.X
|
||||
delta := to.(*PathTile).Position.Clone()
|
||||
delta.Subtract(&t.Position.Vector)
|
||||
delta.Abs()
|
||||
|
||||
if absX < 0 {
|
||||
absX = -absX
|
||||
}
|
||||
|
||||
absY := toT.Y - t.Y
|
||||
|
||||
if absY < 0 {
|
||||
absY = -absY
|
||||
}
|
||||
|
||||
r := absX + absY
|
||||
|
||||
return r
|
||||
return delta.X() + delta.Y()
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package d2mapengine
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2astar"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||
@ -51,8 +53,9 @@ func (m *MapEngine) RegenerateWalkPaths() {
|
||||
index := subTileX + (subTileY * m.size.Width * 5)
|
||||
m.walkMesh[index] = d2common.PathTile{
|
||||
Walkable: !isBlocked,
|
||||
X: float64(subTileX) / 5.0,
|
||||
Y: float64(subTileY) / 5.0,
|
||||
Position: d2vector.NewPosition(
|
||||
float64(subTileX),
|
||||
float64(subTileY)),
|
||||
}
|
||||
|
||||
ySkew := m.size.Width * 5
|
||||
|
@ -23,14 +23,11 @@ type mapEntity struct {
|
||||
|
||||
// newMapEntity creates an instance of mapEntity
|
||||
func newMapEntity(x, y int) mapEntity {
|
||||
locX, locY := float64(x), float64(y)
|
||||
pos := d2vector.NewPosition(float64(x), float64(y))
|
||||
|
||||
return mapEntity{
|
||||
Position: d2vector.NewPosition(locX, locY),
|
||||
Target: d2vector.NewPosition(locX, locY),
|
||||
Speed: 6,
|
||||
drawLayer: 0,
|
||||
path: []d2astar.Pather{},
|
||||
Position: pos,
|
||||
Target: pos,
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,8 +140,7 @@ func (m *mapEntity) nextPath() {
|
||||
if m.hasPath() {
|
||||
// Set next path node
|
||||
m.setTarget(
|
||||
m.path[0].(*d2common.PathTile).X*5,
|
||||
m.path[0].(*d2common.PathTile).Y*5,
|
||||
m.path[0].(*d2common.PathTile).Position,
|
||||
m.done,
|
||||
)
|
||||
|
||||
@ -165,9 +161,9 @@ func (m *mapEntity) hasPath() bool {
|
||||
}
|
||||
|
||||
// setTarget sets target coordinates and changes animation based on proximity and direction.
|
||||
func (m *mapEntity) setTarget(tx, ty float64, done func()) {
|
||||
func (m *mapEntity) setTarget(p d2vector.Position, done func()) {
|
||||
// Set the target
|
||||
m.Target.Set(tx, ty)
|
||||
m.Target.Copy(&p.Vector)
|
||||
m.done = done
|
||||
|
||||
// Update the direction
|
||||
|
@ -51,7 +51,7 @@ func path(length int, origin d2vector.Position) []d2astar.Pather {
|
||||
}
|
||||
|
||||
func pathTile(x, y float64) *d2common.PathTile {
|
||||
return &d2common.PathTile{X: x, Y: y}
|
||||
return &d2common.PathTile{Position: d2vector.NewPositionTile(x, y)}
|
||||
}
|
||||
|
||||
func TestMapEntity_Step(t *testing.T) {
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
||||
@ -54,7 +56,7 @@ func (m *Missile) SetRadians(angle float64, done func()) {
|
||||
x := m.Position.X() + (r * math.Cos(angle))
|
||||
y := m.Position.Y() + (r * math.Sin(angle))
|
||||
|
||||
m.setTarget(x, y, done)
|
||||
m.setTarget(d2vector.NewPosition(x, y), done)
|
||||
}
|
||||
|
||||
// Advance is called once per frame and processes a
|
||||
|
@ -137,8 +137,7 @@ func (v *NPC) Advance(tickTime float64) {
|
||||
v.isDone = false
|
||||
path := v.NextPath()
|
||||
v.setTarget(
|
||||
float64(path.X),
|
||||
float64(path.Y),
|
||||
path.Position,
|
||||
v.next,
|
||||
)
|
||||
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapentity"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2object"
|
||||
|
||||
@ -171,8 +173,9 @@ func convertPaths(tileOffsetX, tileOffsetY int, paths []d2common.Path) []d2commo
|
||||
result := make([]d2common.Path, len(paths))
|
||||
for i := 0; i < len(paths); i++ {
|
||||
result[i].Action = paths[i].Action
|
||||
result[i].X = paths[i].X + (tileOffsetX * 5)
|
||||
result[i].Y = paths[i].Y + (tileOffsetY * 5)
|
||||
result[i].Position = d2vector.NewPosition(
|
||||
paths[i].Position.X()+float64(tileOffsetX*5),
|
||||
paths[i].Position.Y()+float64(tileOffsetY*5))
|
||||
}
|
||||
|
||||
return result
|
||||
|
Loading…
x
Reference in New Issue
Block a user