69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package animation
|
|
|
|
import (
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
type SpriteAnimation interface {
|
|
Draw(
|
|
frame int,
|
|
windowPosition rl.Vector2,
|
|
angle float32,
|
|
center rl.Vector2,
|
|
flip rl.Vector2,
|
|
color rl.Color,
|
|
) error
|
|
}
|
|
|
|
var (
|
|
FLIP_NONE = rl.Vector2{X: 0, Y: 0}
|
|
FLIP_HORIZONTAL = rl.Vector2{X: 1, Y: 0}
|
|
FLIP_VERTICAL = rl.Vector2{X: 0, Y: 1}
|
|
FLIP_BOTH = rl.Vector2{X: 1, Y: 1}
|
|
)
|
|
|
|
// An entityAnimation will take a SpriteAnimation and may manipulate it somehow (e.g. flipped for walking the other direction)
|
|
// This way you may cut down the actual number of pictures needed to define all the different animations.
|
|
type entityAnimation struct {
|
|
spriteAnimation SpriteAnimation
|
|
speed int
|
|
length int
|
|
angle float32
|
|
center rl.Vector2
|
|
flip rl.Vector2
|
|
}
|
|
|
|
func NewEntityAnimation(
|
|
spriteAnimation SpriteAnimation,
|
|
speed int,
|
|
length int,
|
|
angle float32,
|
|
center rl.Vector2,
|
|
flip rl.Vector2,
|
|
) entityAnimation {
|
|
|
|
return entityAnimation{
|
|
spriteAnimation: spriteAnimation,
|
|
speed: speed,
|
|
length: length,
|
|
angle: angle,
|
|
center: center,
|
|
flip: flip,
|
|
}
|
|
}
|
|
|
|
func (e entityAnimation) Draw(frame int, windowPosition rl.Vector2, color rl.Color) error {
|
|
return e.spriteAnimation.Draw(frame, windowPosition, e.angle, e.center, e.flip, color)
|
|
}
|
|
|
|
func (e entityAnimation) GetSpeed() int {
|
|
return e.speed
|
|
}
|
|
|
|
func getCenter(dimensions rl.Vector2) rl.Vector2 {
|
|
x := dimensions.X / 2.0
|
|
y := dimensions.Y / 2.0
|
|
|
|
return rl.Vector2{X: x, Y: y}
|
|
}
|