2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2019-11-06 18:25:19 -05:00
|
|
|
|
|
|
|
import (
|
2020-07-23 12:56:50 -04:00
|
|
|
"fmt"
|
|
|
|
|
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
|
2020-07-31 17:55:11 -04:00
|
|
|
animation d2interface.Animation
|
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
direction int
|
|
|
|
action int
|
|
|
|
repetitions int
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-07-31 17:55:11 -04: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-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-16 12:06:08 -04:00
|
|
|
int(((renderOffset.X()+renderOffset.Y())*8)-5),
|
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-07-23 12:56:50 -04:00
|
|
|
|
2020-07-31 17:55:11 -04:00
|
|
|
if ae.highlight {
|
|
|
|
target.PushBrightness(2)
|
|
|
|
defer target.Pop()
|
|
|
|
ae.highlight = false
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04: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 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-07-23 12:56:50 -04:00
|
|
|
if err := ae.animation.SetDirection(ae.direction); err != nil {
|
|
|
|
fmt.Printf("failed to update the animation direction, err: %v\n", err)
|
|
|
|
}
|
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) {
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := ae.animation.Advance(elapsed); err != nil {
|
|
|
|
fmt.Printf("failed to advance the animation, err: %v\n", err)
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
2020-07-31 17:55:11 -04:00
|
|
|
|
|
|
|
// SetHighlight sets the highlight state of the animated entity
|
|
|
|
func (ae *AnimatedEntity) SetHighlight(set bool) {
|
|
|
|
ae.highlight = set
|
|
|
|
}
|