2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2019-11-06 18:25:19 -05:00
|
|
|
|
|
|
|
import (
|
2020-07-23 19:56:50 +03:00
|
|
|
"fmt"
|
|
|
|
|
2020-06-28 21:41:58 -07:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2019-11-06 18:25:19 -05:00
|
|
|
)
|
|
|
|
|
2020-02-22 17:44:30 -08: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 17:44:30 -08:00
|
|
|
mapEntity
|
2020-07-31 14:55:11 -07:00
|
|
|
animation d2interface.Animation
|
|
|
|
|
2020-02-22 17:44:30 -08:00
|
|
|
direction int
|
|
|
|
action int
|
|
|
|
repetitions int
|
2019-12-23 22:48:45 -08:00
|
|
|
|
2020-07-31 14:55:11 -07:00
|
|
|
highlight bool
|
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-28 21:41:58 -07:00
|
|
|
func (ae *AnimatedEntity) Render(target d2interface.Surface) {
|
2020-07-13 14:06:50 +01:00
|
|
|
renderOffset := ae.Position.RenderOffset()
|
2020-08-01 16:03:09 -07:00
|
|
|
ox, oy := renderOffset.X(), renderOffset.Y()
|
|
|
|
tx, ty := int((ox-oy)*16), int((ox+oy)*8)-5
|
|
|
|
|
|
|
|
target.PushTranslation(tx, ty)
|
2020-07-13 14:06:50 +01:00
|
|
|
|
2019-12-28 13:46:08 -08:00
|
|
|
defer target.Pop()
|
2020-07-23 19:56:50 +03:00
|
|
|
|
2020-07-31 14:55:11 -07:00
|
|
|
if ae.highlight {
|
|
|
|
target.PushBrightness(2)
|
|
|
|
defer target.Pop()
|
2020-08-01 16:03:09 -07:00
|
|
|
|
2020-07-31 14:55:11 -07:00
|
|
|
ae.highlight = false
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:56:50 +03:00
|
|
|
if err := ae.animation.Render(target); err != nil {
|
|
|
|
fmt.Printf("failed to render animated entity, err: %v\n", err)
|
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-17 16:06:02 -05:00
|
|
|
|
2020-07-09 21:11:01 +01: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 17:44:30 -08:00
|
|
|
return ae.direction
|
2019-11-17 16:06:02 -05:00
|
|
|
}
|
2019-12-03 04:55:48 +07:00
|
|
|
|
2020-02-22 17:44:30 -08:00
|
|
|
// rotate sets direction and changes animation
|
2020-06-24 19:49:13 +02:00
|
|
|
func (ae *AnimatedEntity) rotate(direction int) {
|
|
|
|
ae.direction = direction
|
2020-02-02 12:46:19 -05:00
|
|
|
|
2020-07-23 19:56:50 +03:00
|
|
|
if err := ae.animation.SetDirection(ae.direction); err != nil {
|
|
|
|
fmt.Printf("failed to update the animation direction, err: %v\n", err)
|
|
|
|
}
|
2019-12-12 21:33:11 -08:00
|
|
|
}
|
|
|
|
|
2020-07-09 21:11:01 +01:00
|
|
|
// Advance is called once per frame and processes a
|
|
|
|
// single game tick.
|
2020-02-22 17:44:30 -08:00
|
|
|
func (ae *AnimatedEntity) Advance(elapsed float64) {
|
2020-07-23 19:56:50 +03:00
|
|
|
if err := ae.animation.Advance(elapsed); err != nil {
|
|
|
|
fmt.Printf("failed to advance the animation, err: %v\n", err)
|
|
|
|
}
|
2019-12-12 21:33:11 -08:00
|
|
|
}
|
2020-07-31 14:55:11 -07:00
|
|
|
|
|
|
|
// SetHighlight sets the highlight state of the animated entity
|
|
|
|
func (ae *AnimatedEntity) SetHighlight(set bool) {
|
|
|
|
ae.highlight = set
|
|
|
|
}
|