2020-02-01 18:55:56 -05:00
|
|
|
package d2map
|
2019-11-10 03:36:53 -05:00
|
|
|
|
|
|
|
import (
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2020-02-22 20:44:30 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-22 20:44:30 -05:00
|
|
|
"math/rand"
|
2019-11-10 03:36:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NPC struct {
|
2020-02-22 20:44:30 -05:00
|
|
|
*AnimatedComposite
|
|
|
|
action int
|
|
|
|
HasPaths bool
|
|
|
|
Paths []d2common.Path
|
|
|
|
path int
|
|
|
|
isDone bool
|
|
|
|
repetitions int
|
2019-11-10 03:36:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
func CreateNPC(x, y int, object *d2datadict.ObjectLookupRecord, direction int) *NPC {
|
|
|
|
entity, err := CreateAnimatedComposite(x, y, object, d2resource.PaletteUnits)
|
2019-12-24 01:48:45 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-11-10 03:36:53 -05:00
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
result := &NPC{AnimatedComposite: entity, HasPaths: false}
|
|
|
|
result.SetMode(object.Mode, object.Class, direction)
|
2019-11-10 03:36:53 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:44:52 -05:00
|
|
|
func (v *NPC) Path() d2common.Path {
|
|
|
|
return v.Paths[v.path]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *NPC) NextPath() d2common.Path {
|
|
|
|
v.path++
|
|
|
|
if v.path == len(v.Paths) {
|
|
|
|
v.path = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.Paths[v.path]
|
|
|
|
}
|
|
|
|
|
2019-11-11 23:48:55 -05:00
|
|
|
func (v *NPC) SetPaths(paths []d2common.Path) {
|
|
|
|
v.Paths = paths
|
2019-12-06 09:44:52 -05:00
|
|
|
v.HasPaths = len(paths) > 0
|
2020-02-22 20:44:30 -05:00
|
|
|
v.isDone = true
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *NPC) Advance(tickTime float64) {
|
2020-02-22 20:44:30 -05:00
|
|
|
v.Step(tickTime)
|
|
|
|
v.AnimatedComposite.Advance(tickTime)
|
|
|
|
|
|
|
|
if v.HasPaths && v.wait() {
|
2019-12-13 00:33:11 -05:00
|
|
|
// If at the target, set target to the next path.
|
2020-02-22 20:44:30 -05:00
|
|
|
v.isDone = false
|
2019-12-13 00:33:11 -05:00
|
|
|
path := v.NextPath()
|
2020-02-22 20:44:30 -05:00
|
|
|
v.SetTarget(
|
2019-12-13 00:33:11 -05:00
|
|
|
float64(path.X),
|
|
|
|
float64(path.Y),
|
2020-02-22 20:44:30 -05:00
|
|
|
v.next,
|
2019-12-13 00:33:11 -05:00
|
|
|
)
|
2020-02-22 20:44:30 -05:00
|
|
|
v.action = path.Action
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
2020-02-22 20:44:30 -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 *NPC) wait() bool {
|
|
|
|
return v.isDone && v.composite.GetPlayedCount() > v.repetitions
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
func (v *NPC) next() {
|
|
|
|
v.isDone = true
|
|
|
|
v.repetitions = 3 + rand.Intn(5)
|
|
|
|
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
|
|
|
|
default:
|
|
|
|
v.repetitions = 0
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
if v.animationMode != newAnimationMode.String() {
|
|
|
|
v.SetMode(newAnimationMode.String(), v.weaponClass, v.direction)
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|