2022-10-23 00:57:23 -07:00
|
|
|
package animation
|
2022-10-13 15:37:22 -07:00
|
|
|
|
2022-10-13 21:38:52 -07:00
|
|
|
import (
|
2022-11-22 00:08:00 -08:00
|
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
2022-10-13 21:38:52 -07:00
|
|
|
)
|
2022-10-13 15:37:22 -07:00
|
|
|
|
|
|
|
type SpriteAnimation interface {
|
2022-11-22 00:08:00 -08:00
|
|
|
Draw(
|
|
|
|
frame int,
|
|
|
|
windowPosition rl.Vector2,
|
|
|
|
angle float32,
|
|
|
|
center rl.Vector2,
|
|
|
|
flip rl.Vector2,
|
|
|
|
color rl.Color,
|
|
|
|
) error
|
2022-10-13 15:37:22 -07:00
|
|
|
}
|
|
|
|
|
2022-11-22 00:08:00 -08:00
|
|
|
var (
|
2023-04-30 00:33:18 -07:00
|
|
|
FlipNone = rl.Vector2{X: 0, Y: 0}
|
|
|
|
FlipHorizontal = rl.Vector2{X: 1, Y: 0}
|
|
|
|
FlipVertical = rl.Vector2{X: 0, Y: 1}
|
|
|
|
FlipBoth = rl.Vector2{X: 1, Y: 1}
|
2022-11-22 00:08:00 -08:00
|
|
|
)
|
|
|
|
|
2022-10-13 15:37:22 -07:00
|
|
|
// 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
|
2022-10-27 01:37:35 -07:00
|
|
|
speed int
|
|
|
|
length int
|
2022-11-22 00:08:00 -08:00
|
|
|
angle float32
|
|
|
|
center rl.Vector2
|
|
|
|
flip rl.Vector2
|
2022-10-13 15:37:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEntityAnimation(
|
|
|
|
spriteAnimation SpriteAnimation,
|
2022-10-27 01:37:35 -07:00
|
|
|
speed int,
|
|
|
|
length int,
|
2022-11-22 00:08:00 -08:00
|
|
|
angle float32,
|
|
|
|
center rl.Vector2,
|
|
|
|
flip rl.Vector2,
|
2022-11-20 15:20:20 -08:00
|
|
|
) entityAnimation {
|
2022-10-13 15:37:22 -07:00
|
|
|
|
2022-11-20 15:20:20 -08:00
|
|
|
return entityAnimation{
|
2022-10-13 15:37:22 -07:00
|
|
|
spriteAnimation: spriteAnimation,
|
|
|
|
speed: speed,
|
|
|
|
length: length,
|
|
|
|
angle: angle,
|
|
|
|
center: center,
|
|
|
|
flip: flip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 00:08:00 -08:00
|
|
|
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)
|
2022-10-13 15:37:22 -07:00
|
|
|
}
|
2022-10-22 02:00:29 -07:00
|
|
|
|
2022-11-20 15:20:20 -08:00
|
|
|
func (e entityAnimation) GetSpeed() int {
|
2022-10-23 00:57:23 -07:00
|
|
|
return e.speed
|
|
|
|
}
|
|
|
|
|
2022-11-22 00:08:00 -08:00
|
|
|
func getCenter(dimensions rl.Vector2) rl.Vector2 {
|
|
|
|
x := dimensions.X / 2.0
|
|
|
|
y := dimensions.Y / 2.0
|
2022-11-20 15:20:20 -08:00
|
|
|
|
2022-11-22 00:08:00 -08:00
|
|
|
return rl.Vector2{X: x, Y: y}
|
2022-11-20 15:20:20 -08:00
|
|
|
}
|