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