1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-27 17:45:24 +00:00
OpenDiablo2/d2core/d2map/d2mapentity/animated_entity.go

83 lines
1.9 KiB
Go
Raw Normal View History

2020-06-21 22:40:37 +00:00
package d2mapentity
import (
2020-07-23 16:56:50 +00:00
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
// AnimatedEntity represents an animation that can be projected onto the map.
type AnimatedEntity struct {
mapEntity
animation d2interface.Animation
direction int
action int
repetitions int
highlight bool
}
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
func (ae *AnimatedEntity) Render(target d2interface.Surface) {
renderOffset := ae.Position.RenderOffset()
ox, oy := renderOffset.X(), renderOffset.Y()
tx, ty := translateSubtile(ox-oy, ox+oy)
target.PushTranslation(tx, ty)
defer target.Pop()
2020-07-23 16:56:50 +00:00
if ae.highlight {
target.PushBrightness(highlightBrightness)
defer target.Pop()
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-17 21:06:02 +00:00
// GetDirection returns the current facing direction of this entity.
2020-06-24 14:13:11 +00:00
func (ae *AnimatedEntity) GetDirection() int {
return ae.direction
2019-11-17 21:06:02 +00:00
}
// rotate sets direction and changes animation
func (ae *AnimatedEntity) rotate(direction int) {
ae.direction = direction
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)
}
}
// Advance is called once per frame and processes a
// single game tick.
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)
}
}
// SetHighlight sets the highlight state of the animated entity
func (ae *AnimatedEntity) SetHighlight(set bool) {
ae.highlight = set
}