2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2020-02-22 20:44:30 -05:00
|
|
|
|
|
|
|
import (
|
2020-02-22 22:33:58 -05:00
|
|
|
"math"
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-06-23 02:04:17 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2astar"
|
2020-02-22 20:44:30 -05:00
|
|
|
)
|
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
type MapEntity interface {
|
2020-06-29 00:41:58 -04:00
|
|
|
Render(target d2interface.Surface)
|
2020-06-21 18:40:37 -04:00
|
|
|
Advance(tickTime float64)
|
|
|
|
GetPosition() (float64, float64)
|
2020-06-29 00:41:58 -04:00
|
|
|
GetLayer() int
|
2020-06-25 00:39:09 -04:00
|
|
|
GetPositionF() (float64, float64)
|
|
|
|
Name() string
|
2020-06-27 18:58:41 -04:00
|
|
|
Selectable() bool
|
|
|
|
Highlight()
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
// mapEntity represents an entity on the map that can be animated
|
|
|
|
type mapEntity struct {
|
|
|
|
LocationX float64
|
|
|
|
LocationY float64
|
|
|
|
TileX, TileY int // Coordinates of the tile the unit is within
|
|
|
|
subcellX, subcellY float64 // Subcell coordinates within the current tile
|
|
|
|
weaponClass string
|
|
|
|
offsetX, offsetY int
|
|
|
|
TargetX float64
|
|
|
|
TargetY float64
|
|
|
|
Speed float64
|
2020-06-23 02:04:17 -04:00
|
|
|
path []d2astar.Pather
|
2020-06-27 14:30:23 -04:00
|
|
|
drawLayer int
|
2020-02-22 20:44:30 -05:00
|
|
|
|
|
|
|
done func()
|
2020-06-24 13:49:13 -04:00
|
|
|
directioner func(direction int)
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// createMapEntity creates an instance of mapEntity
|
|
|
|
func createMapEntity(x, y int) mapEntity {
|
|
|
|
locX, locY := float64(x), float64(y)
|
|
|
|
return mapEntity{
|
|
|
|
LocationX: locX,
|
|
|
|
LocationY: locY,
|
|
|
|
TargetX: locX,
|
|
|
|
TargetY: locY,
|
|
|
|
TileX: x / 5,
|
|
|
|
TileY: y / 5,
|
|
|
|
subcellX: 1 + math.Mod(locX, 5),
|
|
|
|
subcellY: 1 + math.Mod(locY, 5),
|
|
|
|
Speed: 6,
|
2020-06-27 14:30:23 -04:00
|
|
|
drawLayer: 0,
|
2020-06-23 02:04:17 -04:00
|
|
|
path: []d2astar.Pather{},
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:30:23 -04:00
|
|
|
func (m *mapEntity) GetLayer() int {
|
|
|
|
return m.drawLayer
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:04:17 -04:00
|
|
|
func (m *mapEntity) SetPath(path []d2astar.Pather, done func()) {
|
2020-02-22 20:44:30 -05:00
|
|
|
m.path = path
|
|
|
|
m.done = done
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:03:00 -04:00
|
|
|
func (m *mapEntity) ClearPath() {
|
|
|
|
m.path = nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:32 -04:00
|
|
|
func (m *mapEntity) SetSpeed(speed float64) {
|
|
|
|
m.Speed = speed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) GetSpeed() float64 {
|
|
|
|
return m.Speed
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
func (m *mapEntity) getStepLength(tickTime float64) (float64, float64) {
|
|
|
|
length := tickTime * m.Speed
|
|
|
|
|
|
|
|
angle := 359 - d2common.GetAngleBetween(
|
|
|
|
m.LocationX,
|
|
|
|
m.LocationY,
|
|
|
|
m.TargetX,
|
|
|
|
m.TargetY,
|
|
|
|
)
|
|
|
|
radians := (math.Pi / 180.0) * float64(angle)
|
|
|
|
oneStepX := length * math.Cos(radians)
|
|
|
|
oneStepY := length * math.Sin(radians)
|
|
|
|
return oneStepX, oneStepY
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) IsAtTarget() bool {
|
2020-06-20 00:40:49 -04:00
|
|
|
return math.Abs(m.LocationX-m.TargetX) < 0.0001 && math.Abs(m.LocationY-m.TargetY) < 0.0001 && !m.HasPathFinding()
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) Step(tickTime float64) {
|
|
|
|
if m.IsAtTarget() {
|
|
|
|
if m.done != nil {
|
|
|
|
m.done()
|
|
|
|
m.done = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stepX, stepY := m.getStepLength(tickTime)
|
2020-06-20 00:40:49 -04:00
|
|
|
for {
|
|
|
|
if d2common.AlmostEqual(m.LocationX-m.TargetX, 0, 0.0001) {
|
|
|
|
stepX = 0
|
|
|
|
}
|
|
|
|
if d2common.AlmostEqual(m.LocationY-m.TargetY, 0, 0.0001) {
|
|
|
|
stepY = 0
|
|
|
|
}
|
|
|
|
m.LocationX, stepX = d2common.AdjustWithRemainder(m.LocationX, stepX, m.TargetX)
|
|
|
|
m.LocationY, stepY = d2common.AdjustWithRemainder(m.LocationY, stepY, m.TargetY)
|
|
|
|
|
|
|
|
m.subcellX = 1 + math.Mod(m.LocationX, 5)
|
|
|
|
m.subcellY = 1 + math.Mod(m.LocationY, 5)
|
|
|
|
m.TileX = int(m.LocationX / 5)
|
|
|
|
m.TileY = int(m.LocationY / 5)
|
|
|
|
|
|
|
|
if d2common.AlmostEqual(m.LocationX, m.TargetX, 0.01) && d2common.AlmostEqual(m.LocationY, m.TargetY, 0.01) {
|
|
|
|
if len(m.path) > 0 {
|
2020-06-21 18:40:37 -04:00
|
|
|
m.SetTarget(m.path[0].(*d2common.PathTile).X*5, m.path[0].(*d2common.PathTile).Y*5, m.done)
|
2020-06-20 00:40:49 -04:00
|
|
|
|
|
|
|
if len(m.path) > 1 {
|
|
|
|
m.path = m.path[1:]
|
|
|
|
} else {
|
2020-06-23 02:04:17 -04:00
|
|
|
m.path = []d2astar.Pather{}
|
2020-06-20 00:40:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m.LocationX = m.TargetX
|
|
|
|
m.LocationY = m.TargetY
|
|
|
|
m.subcellX = 1 + math.Mod(m.LocationX, 5)
|
|
|
|
m.subcellY = 1 + math.Mod(m.LocationY, 5)
|
|
|
|
m.TileX = int(m.LocationX / 5)
|
|
|
|
m.TileY = int(m.LocationY / 5)
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 20:44:30 -05:00
|
|
|
|
2020-06-20 00:40:49 -04:00
|
|
|
if stepX == 0 && stepY == 0 {
|
|
|
|
break
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
2020-06-20 00:40:49 -04:00
|
|
|
}
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) HasPathFinding() bool {
|
|
|
|
return len(m.path) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTarget sets target coordinates and changes animation based on proximity and direction
|
|
|
|
func (m *mapEntity) SetTarget(tx, ty float64, done func()) {
|
|
|
|
m.TargetX, m.TargetY = tx, ty
|
|
|
|
m.done = done
|
|
|
|
|
|
|
|
if m.directioner != nil {
|
|
|
|
angle := 359 - d2common.GetAngleBetween(
|
|
|
|
m.LocationX,
|
|
|
|
m.LocationY,
|
|
|
|
tx,
|
|
|
|
ty,
|
|
|
|
)
|
2020-06-24 13:49:13 -04:00
|
|
|
m.directioner(angleToDirection(float64(angle)))
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 22:33:58 -05:00
|
|
|
func angleToDirection(angle float64) int {
|
|
|
|
degreesPerDirection := 360.0 / 64.0
|
2020-02-22 20:44:30 -05:00
|
|
|
offset := 45.0 - (degreesPerDirection / 2)
|
2020-02-22 22:33:58 -05:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
newDirection := int((angle - offset) / degreesPerDirection)
|
2020-02-22 22:33:58 -05:00
|
|
|
|
|
|
|
if newDirection >= 64 {
|
|
|
|
newDirection = newDirection - 64
|
2020-02-22 20:44:30 -05:00
|
|
|
} else if newDirection < 0 {
|
2020-02-22 22:33:58 -05:00
|
|
|
newDirection = 64 + newDirection
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return newDirection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) GetPosition() (float64, float64) {
|
|
|
|
return float64(m.TileX), float64(m.TileY)
|
|
|
|
}
|
2020-06-25 00:39:09 -04:00
|
|
|
|
|
|
|
func (m *mapEntity) GetPositionF() (float64, float64) {
|
2020-06-26 20:03:00 -04:00
|
|
|
return float64(m.TileX) + (float64(m.subcellX) / 5.0), float64(m.TileY) + (float64(m.subcellY) / 5.0)
|
2020-06-25 00:39:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
2020-06-27 18:58:41 -04:00
|
|
|
|
|
|
|
func (m *mapEntity) Highlight() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapEntity) Selectable() bool {
|
|
|
|
return false
|
|
|
|
}
|