2020-02-01 18:55:56 -05:00
|
|
|
package d2map
|
2019-11-06 18:25:19 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-13 00:33:11 -05:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-11-06 18:25:19 -05:00
|
|
|
)
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// AnimatedEntity represents an entity on the map that can be animated
|
2019-11-06 18:25:19 -05:00
|
|
|
type AnimatedEntity struct {
|
2019-12-06 09:44:52 -05:00
|
|
|
LocationX float64
|
2019-11-13 20:31:47 -05:00
|
|
|
LocationY float64
|
2019-12-06 09:44:52 -05:00
|
|
|
TileX, TileY int // Coordinates of the tile the unit is within
|
|
|
|
subcellX, subcellY float64 // Subcell coordinates within the current tile
|
2019-11-13 20:31:47 -05:00
|
|
|
animationMode string
|
|
|
|
weaponClass string
|
|
|
|
direction int
|
2019-11-17 16:06:02 -05:00
|
|
|
offsetX, offsetY int32
|
2019-12-17 18:23:21 -05:00
|
|
|
TargetX float64
|
|
|
|
TargetY float64
|
|
|
|
action int32
|
2019-12-24 01:48:45 -05:00
|
|
|
repetitions int
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
composite *d2asset.Composite
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// CreateAnimatedEntity creates an instance of AnimatedEntity
|
2019-12-24 01:48:45 -05:00
|
|
|
func CreateAnimatedEntity(x, y int32, object *d2datadict.ObjectLookupRecord, palettePath string) (*AnimatedEntity, error) {
|
2020-02-01 18:55:56 -05:00
|
|
|
composite, err := d2asset.LoadComposite(object, palettePath)
|
2019-12-24 01:48:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-13 20:31:47 -05:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
entity := &AnimatedEntity{composite: composite}
|
|
|
|
entity.LocationX = float64(x)
|
|
|
|
entity.LocationY = float64(y)
|
|
|
|
entity.TargetX = entity.LocationX
|
|
|
|
entity.TargetY = entity.LocationY
|
|
|
|
|
|
|
|
entity.TileX = int(entity.LocationX / 5)
|
|
|
|
entity.TileY = int(entity.LocationY / 5)
|
|
|
|
entity.subcellX = 1 + math.Mod(entity.LocationX, 5)
|
|
|
|
entity.subcellY = 1 + math.Mod(entity.LocationY, 5)
|
2019-11-13 20:31:47 -05:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
return entity, nil
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// SetMode changes the graphical mode of this animated entity
|
2019-12-24 01:48:45 -05:00
|
|
|
func (v *AnimatedEntity) SetMode(animationMode, weaponClass string, direction int) error {
|
2019-11-06 18:25:19 -05:00
|
|
|
v.animationMode = animationMode
|
|
|
|
v.direction = direction
|
2019-11-09 23:37:02 -05:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
err := v.composite.SetMode(animationMode, weaponClass, direction)
|
2019-12-21 20:53:18 -05:00
|
|
|
if err != nil {
|
2019-12-24 01:48:45 -05:00
|
|
|
err = v.composite.SetMode(animationMode, "HTH", direction)
|
2019-11-14 22:20:01 -05:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
return err
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 23:58:36 -05:00
|
|
|
// If an npc has a path to pause at each location.
|
|
|
|
// Waits for animation to end and all repetitions to be exhausted.
|
|
|
|
func (v AnimatedEntity) Wait() bool {
|
2019-12-24 01:48:45 -05:00
|
|
|
return v.composite.GetPlayedCount() > v.repetitions
|
2019-12-06 23:58:36 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// Render draws this animated entity onto the target
|
2020-02-01 20:39:28 -05:00
|
|
|
func (v *AnimatedEntity) Render(target d2render.Surface) {
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(
|
|
|
|
int(v.offsetX)+int((v.subcellX-v.subcellY)*16),
|
|
|
|
int(v.offsetY)+int(((v.subcellX+v.subcellY)*8)-5),
|
2019-12-24 01:48:45 -05:00
|
|
|
)
|
2019-12-28 16:46:08 -05:00
|
|
|
defer target.Pop()
|
|
|
|
v.composite.Render(target)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-17 16:06:02 -05:00
|
|
|
|
|
|
|
func (v AnimatedEntity) GetDirection() int {
|
|
|
|
return v.direction
|
|
|
|
}
|
2019-12-02 16:55:48 -05:00
|
|
|
|
|
|
|
func (v *AnimatedEntity) getStepLength(tickTime float64) (float64, float64) {
|
2019-12-06 09:44:52 -05:00
|
|
|
speed := 6.0
|
2019-12-02 16:55:48 -05:00
|
|
|
length := tickTime * speed
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
angle := 359 - d2common.GetAngleBetween(
|
2019-12-02 16:55:48 -05:00
|
|
|
v.LocationX,
|
|
|
|
v.LocationY,
|
|
|
|
v.TargetX,
|
|
|
|
v.TargetY,
|
|
|
|
)
|
2019-12-06 17:52:35 -05:00
|
|
|
radians := (math.Pi / 180.0) * float64(angle)
|
2019-12-02 16:55:48 -05:00
|
|
|
oneStepX := length * math.Cos(radians)
|
|
|
|
oneStepY := length * math.Sin(radians)
|
|
|
|
return oneStepX, oneStepY
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AnimatedEntity) Step(tickTime float64) {
|
|
|
|
stepX, stepY := v.getStepLength(tickTime)
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
if d2common.AlmostEqual(v.LocationX, v.TargetX, stepX) {
|
2019-12-02 16:55:48 -05:00
|
|
|
v.LocationX = v.TargetX
|
|
|
|
}
|
2020-02-01 21:06:22 -05:00
|
|
|
if d2common.AlmostEqual(v.LocationY, v.TargetY, stepY) {
|
2019-12-02 16:55:48 -05:00
|
|
|
v.LocationY = v.TargetY
|
|
|
|
}
|
|
|
|
if v.LocationX != v.TargetX {
|
|
|
|
v.LocationX += stepX
|
|
|
|
}
|
|
|
|
if v.LocationY != v.TargetY {
|
|
|
|
v.LocationY += stepY
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
|
|
|
v.subcellX = 1 + math.Mod(v.LocationX, 5)
|
|
|
|
v.subcellY = 1 + math.Mod(v.LocationY, 5)
|
|
|
|
v.TileX = int(v.LocationX / 5)
|
|
|
|
v.TileY = int(v.LocationY / 5)
|
|
|
|
|
2019-12-02 16:55:48 -05:00
|
|
|
if v.LocationX == v.TargetX && v.LocationY == v.TargetY {
|
2019-12-24 01:48:45 -05:00
|
|
|
v.repetitions = 3 + rand.Intn(5)
|
2019-12-06 23:58:36 -05:00
|
|
|
newAnimationMode := d2enum.AnimationModeObjectNeutral
|
|
|
|
// TODO: Figure out what 1-3 are for, 4 is correct.
|
|
|
|
switch v.action {
|
|
|
|
case 1:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 2:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 3:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 4:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterSkill1
|
|
|
|
v.repetitions = 0
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
v.composite.ResetPlayedCount()
|
2019-12-06 23:58:36 -05:00
|
|
|
if v.animationMode != newAnimationMode.String() {
|
|
|
|
v.SetMode(newAnimationMode.String(), v.weaponClass, v.direction)
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTarget sets target coordinates and changes animation based on proximity and direction
|
2019-12-06 23:58:36 -05:00
|
|
|
func (v *AnimatedEntity) SetTarget(tx, ty float64, action int32) {
|
2020-02-01 21:06:22 -05:00
|
|
|
angle := 359 - d2common.GetAngleBetween(
|
2019-12-02 16:55:48 -05:00
|
|
|
v.LocationX,
|
|
|
|
v.LocationY,
|
|
|
|
tx,
|
|
|
|
ty,
|
|
|
|
)
|
2019-12-06 23:58:36 -05:00
|
|
|
|
|
|
|
v.action = action
|
2019-12-06 09:44:52 -05:00
|
|
|
// TODO: Check if is in town and if is player.
|
|
|
|
newAnimationMode := d2enum.AnimationModeMonsterWalk.String()
|
2019-12-02 16:55:48 -05:00
|
|
|
if tx != v.LocationX || ty != v.LocationY {
|
|
|
|
v.TargetX, v.TargetY = tx, ty
|
2019-12-06 09:44:52 -05:00
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterWalk.String()
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
2019-12-28 23:33:30 -05:00
|
|
|
if newAnimationMode != v.animationMode {
|
|
|
|
v.SetMode(newAnimationMode, v.weaponClass, v.direction)
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
newDirection := angleToDirection(float64(angle), v.composite.GetDirectionCount())
|
2019-12-28 23:33:30 -05:00
|
|
|
|
|
|
|
if newDirection != v.GetDirection() {
|
|
|
|
v.SetMode(v.animationMode, v.weaponClass, newDirection)
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
|
|
|
func angleToDirection(angle float64, numberOfDirections int) int {
|
2019-12-06 23:58:36 -05:00
|
|
|
if numberOfDirections == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:44:52 -05:00
|
|
|
degreesPerDirection := 360.0 / float64(numberOfDirections)
|
|
|
|
offset := 45.0 - (degreesPerDirection / 2)
|
|
|
|
newDirection := int((angle - offset) / degreesPerDirection)
|
|
|
|
if newDirection >= numberOfDirections {
|
|
|
|
newDirection = newDirection - numberOfDirections
|
|
|
|
} else if newDirection < 0 {
|
|
|
|
newDirection = numberOfDirections + newDirection
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDirection
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
func (v *AnimatedEntity) Advance(elapsed float64) {
|
|
|
|
v.composite.Advance(elapsed)
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AnimatedEntity) GetPosition() (float64, float64) {
|
|
|
|
return float64(v.TileX), float64(v.TileY)
|
|
|
|
}
|