project-ely/internal/entity/common.go

58 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
DirLeft int = iota
DirRight
DirUp
DirDown
)
// The following are axis direction vectors based on world coordinates.
// UP/DOWN is intentionally UP = positive (which is different from window coordinates)
var (
VecLeft = rl.Vector2{X: -1, Y: 0}
VecRight = rl.Vector2{X: 1, Y: 0}
VecUp = rl.Vector2{X: 0, Y: 1}
VecDown = rl.Vector2{X: 0, Y: -1}
VecDirections = []rl.Vector2{
// Prefer left/right animations by checking them first in the list
VecLeft,
VecRight,
VecUp,
VecDown,
}
)
func determineClosestDirection(velocity rl.Vector2) int {
closest := DirRight
max := float32(0)
buffer := float32(0.5) // This buffer lets us stick to the left/right animations for diagonal movement
for i, dir := range VecDirections {
dot := rl.Vector2DotProduct(velocity, dir)
if dot > max+buffer {
max = dot
closest = i
}
}
return closest
}