2020-06-21 22:40:37 +00:00
|
|
|
package d2mapentity
|
2019-11-06 23:25:19 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-23 16:56:50 +00:00
|
|
|
"fmt"
|
|
|
|
|
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
|
2020-07-31 21:55:11 +00:00
|
|
|
animation d2interface.Animation
|
|
|
|
|
2020-02-23 01:44:30 +00:00
|
|
|
direction int
|
|
|
|
action int
|
|
|
|
repetitions int
|
2019-12-24 06:48:45 +00:00
|
|
|
|
2020-07-31 21:55:11 +00:00
|
|
|
highlight bool
|
2019-11-06 23:25:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 14:02:32 +00:00
|
|
|
const (
|
|
|
|
subtileWidth = 16 // pixels
|
|
|
|
subtileHeight = 8
|
|
|
|
subtileOffsetY = -5
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
highlightBrightness = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
func translateSubtile(fx, fy float64) (x, y int) {
|
|
|
|
return int(fx * subtileWidth), int(fy*subtileHeight) - subtileOffsetY
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-07-13 13:06:50 +00:00
|
|
|
renderOffset := ae.Position.RenderOffset()
|
2020-08-01 23:03:09 +00:00
|
|
|
ox, oy := renderOffset.X(), renderOffset.Y()
|
2020-10-22 14:02:32 +00:00
|
|
|
tx, ty := translateSubtile(ox-oy, ox+oy)
|
2020-08-01 23:03:09 +00:00
|
|
|
|
|
|
|
target.PushTranslation(tx, ty)
|
2020-07-13 13:06:50 +00:00
|
|
|
|
2019-12-28 21:46:08 +00:00
|
|
|
defer target.Pop()
|
2020-07-23 16:56:50 +00:00
|
|
|
|
2020-07-31 21:55:11 +00:00
|
|
|
if ae.highlight {
|
2020-10-22 14:02:32 +00:00
|
|
|
target.PushBrightness(highlightBrightness)
|
2020-07-31 21:55:11 +00:00
|
|
|
defer target.Pop()
|
2020-08-01 23:03:09 +00:00
|
|
|
|
2020-07-31 21:55:11 +00:00
|
|
|
ae.highlight = false
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:56:50 +00:00
|
|
|
if err := ae.animation.Render(target); err != nil {
|
|
|
|
fmt.Printf("failed to render animated entity, err: %v\n", err)
|
|
|
|
}
|
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-07-23 16:56:50 +00: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 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) {
|
2020-07-23 16:56:50 +00:00
|
|
|
if err := ae.animation.Advance(elapsed); err != nil {
|
|
|
|
fmt.Printf("failed to advance the animation, err: %v\n", err)
|
|
|
|
}
|
2019-12-13 05:33:11 +00:00
|
|
|
}
|
2020-07-31 21:55:11 +00:00
|
|
|
|
|
|
|
// SetHighlight sets the highlight state of the animated entity
|
|
|
|
func (ae *AnimatedEntity) SetHighlight(set bool) {
|
|
|
|
ae.highlight = set
|
|
|
|
}
|