57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package entity
|
|
|
|
import (
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
type EntityAnimation interface {
|
|
Draw(step int, windowPosition rl.Vector2, color rl.Color) error
|
|
GetSpeed() int
|
|
}
|
|
|
|
type AnimationTracker interface {
|
|
Draw(windowPosition rl.Vector2, color rl.Color) error
|
|
SetAnimation(id int)
|
|
}
|
|
|
|
const (
|
|
// These line up with the VEC_DIRECTIONS slice
|
|
DIR_LEFT int = iota
|
|
DIR_RIGHT
|
|
DIR_UP
|
|
DIR_DOWN
|
|
)
|
|
|
|
// The following are axis direction vectors based on world coordinates.
|
|
// UP/DOWN is intentionally UP = positive (which is different from window coordinates)
|
|
var (
|
|
VEC_LEFT = rl.Vector2{X: -1, Y: 0}
|
|
VEC_RIGHT = rl.Vector2{X: 1, Y: 0}
|
|
VEC_UP = rl.Vector2{X: 0, Y: 1}
|
|
VEC_DOWN = rl.Vector2{X: 0, Y: -1}
|
|
|
|
VEC_DIRECTIONS = []rl.Vector2{
|
|
// Prefer left/right animations by checking them first in the list
|
|
VEC_LEFT,
|
|
VEC_RIGHT,
|
|
VEC_UP,
|
|
VEC_DOWN,
|
|
}
|
|
)
|
|
|
|
func determineClosestDirection(velocity rl.Vector2) int {
|
|
closest := DIR_RIGHT
|
|
max := float32(0)
|
|
buffer := float32(0.5) // This buffer lets us stick to the left/right animations for diagonal movement
|
|
|
|
for i, dir := range VEC_DIRECTIONS {
|
|
dot := rl.Vector2DotProduct(velocity, dir)
|
|
if dot > max+buffer {
|
|
max = dot
|
|
closest = i
|
|
}
|
|
}
|
|
|
|
return closest
|
|
}
|