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-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-11-10 03:36:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NPC struct {
|
2020-02-01 18:55:56 -05:00
|
|
|
AnimatedEntity *AnimatedEntity
|
2019-12-06 09:44:52 -05:00
|
|
|
HasPaths bool
|
2019-11-10 03:36:53 -05:00
|
|
|
Paths []d2common.Path
|
2019-12-06 09:44:52 -05:00
|
|
|
path int
|
2019-11-10 03:36:53 -05:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateNPC(x, y int32, object *d2datadict.ObjectLookupRecord, direction int) *NPC {
|
2020-02-01 18:55:56 -05:00
|
|
|
entity, err := CreateAnimatedEntity(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
|
|
|
|
|
|
|
result := &NPC{AnimatedEntity: entity, HasPaths: false}
|
2019-11-14 22:20:01 -05:00
|
|
|
result.AnimatedEntity.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
|
2019-11-11 23:48:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (v *NPC) Render(target d2render.Surface) {
|
2019-12-28 16:46:08 -05:00
|
|
|
v.AnimatedEntity.Render(target)
|
2019-11-10 03:36:53 -05:00
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
|
|
|
|
func (v *NPC) GetPosition() (float64, float64) {
|
|
|
|
return v.AnimatedEntity.GetPosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *NPC) Advance(tickTime float64) {
|
|
|
|
if v.HasPaths &&
|
|
|
|
v.AnimatedEntity.LocationX == v.AnimatedEntity.TargetX &&
|
|
|
|
v.AnimatedEntity.LocationY == v.AnimatedEntity.TargetY &&
|
|
|
|
v.AnimatedEntity.Wait() {
|
|
|
|
// If at the target, set target to the next path.
|
|
|
|
path := v.NextPath()
|
|
|
|
v.AnimatedEntity.SetTarget(
|
|
|
|
float64(path.X),
|
|
|
|
float64(path.Y),
|
|
|
|
path.Action,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.AnimatedEntity.LocationX != v.AnimatedEntity.TargetX ||
|
|
|
|
v.AnimatedEntity.LocationY != v.AnimatedEntity.TargetY {
|
|
|
|
v.AnimatedEntity.Step(tickTime)
|
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
|
|
|
v.AnimatedEntity.Advance(tickTime)
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|