2020-06-21 22:40:37 +00:00
|
|
|
package d2mapentity
|
2019-11-06 23:25:19 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-29 04:41:58 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2019-11-06 23:25:19 +00:00
|
|
|
)
|
|
|
|
|
2020-02-23 01:44:30 +00:00
|
|
|
// AnimatedEntity represents an animation that can be projected onto the map.
|
2019-11-06 23:25:19 +00:00
|
|
|
type AnimatedEntity struct {
|
2020-02-23 01:44:30 +00:00
|
|
|
mapEntity
|
|
|
|
direction int
|
|
|
|
action int
|
|
|
|
repetitions int
|
2019-12-24 06:48:45 +00:00
|
|
|
|
2020-07-05 17:01:44 +00:00
|
|
|
animation d2interface.Animation
|
2019-11-06 23:25:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 03:12:15 +00:00
|
|
|
// CreateAnimatedEntity creates an instance of AnimatedEntity
|
2020-07-05 17:01:44 +00:00
|
|
|
func CreateAnimatedEntity(x, y int, animation d2interface.Animation) *AnimatedEntity {
|
2020-02-23 01:44:30 +00:00
|
|
|
entity := &AnimatedEntity{
|
|
|
|
mapEntity: createMapEntity(x, y),
|
|
|
|
animation: animation,
|
2019-11-15 03:20:01 +00:00
|
|
|
}
|
2020-02-23 01:44:30 +00:00
|
|
|
entity.mapEntity.directioner = entity.rotate
|
2020-07-09 20:11:01 +00:00
|
|
|
|
2020-02-23 01:44:30 +00:00
|
|
|
return entity
|
2019-11-06 23:25:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 03:12:15 +00:00
|
|
|
// Render draws this animated entity onto the target
|
2020-06-29 04:41:58 +00:00
|
|
|
func (ae *AnimatedEntity) Render(target d2interface.Surface) {
|
2019-12-28 21:46:08 +00:00
|
|
|
target.PushTranslation(
|
2020-02-23 01:44:30 +00:00
|
|
|
ae.offsetX+int((ae.subcellX-ae.subcellY)*16),
|
|
|
|
ae.offsetY+int(((ae.subcellX+ae.subcellY)*8)-5),
|
2019-12-24 06:48:45 +00:00
|
|
|
)
|
2019-12-28 21:46:08 +00:00
|
|
|
defer target.Pop()
|
2020-02-23 01:44:30 +00:00
|
|
|
ae.animation.Render(target)
|
2019-11-06 23:25:19 +00:00
|
|
|
}
|
2019-11-17 21:06:02 +00:00
|
|
|
|
2020-07-09 20:11:01 +00:00
|
|
|
// GetDirection returns the current facing direction of this entity.
|
2020-06-24 14:13:11 +00:00
|
|
|
func (ae *AnimatedEntity) GetDirection() int {
|
2020-02-23 01:44:30 +00:00
|
|
|
return ae.direction
|
2019-11-17 21:06:02 +00:00
|
|
|
}
|
2019-12-02 21:55:48 +00:00
|
|
|
|
2020-02-23 01:44:30 +00:00
|
|
|
// rotate sets direction and changes animation
|
2020-06-24 17:49:13 +00:00
|
|
|
func (ae *AnimatedEntity) rotate(direction int) {
|
|
|
|
ae.direction = direction
|
2020-02-02 17:46:19 +00:00
|
|
|
|
2020-02-23 03:33:58 +00:00
|
|
|
ae.animation.SetDirection(ae.direction)
|
2019-12-13 05:33:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 20:11:01 +00:00
|
|
|
// Advance is called once per frame and processes a
|
|
|
|
// single game tick.
|
2020-02-23 01:44:30 +00:00
|
|
|
func (ae *AnimatedEntity) Advance(elapsed float64) {
|
|
|
|
ae.animation.Advance(elapsed)
|
2019-12-13 05:33:11 +00:00
|
|
|
}
|