project-ely/internal/game/entity/types/common.go

53 lines
1.1 KiB
Go

package types
import (
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"github.com/veandco/go-sdl2/sdl"
)
type EntityAnimation interface {
Draw(step int, windowPosition sdl.Point) error
GetSpeed() 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 = vector.Vec2F{X: -1, Y: 0}
VEC_RIGHT = vector.Vec2F{X: 1, Y: 0}
VEC_UP = vector.Vec2F{X: 0, Y: 1}
VEC_DOWN = vector.Vec2F{X: 0, Y: -1}
VEC_DIRECTIONS = []vector.Vec2F{
// Prefer left/right animations by checking them first in the list
VEC_LEFT,
VEC_RIGHT,
VEC_UP,
VEC_DOWN,
}
)
func determineClosestDirection(velocity vector.Vec2F) int {
closest := DIR_RIGHT
max := 0.0
buffer := 0.5 // This buffer lets us stick to the left/right animations for diagonal movement
for i, dir := range VEC_DIRECTIONS {
dot := velocity.Dot(dir)
if dot > max+buffer {
max = dot
closest = i
}
}
return closest
}