2024-04-03 19:39:08 +02:00
|
|
|
package components
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-07 19:10:27 +02:00
|
|
|
"openquell/assets"
|
2024-04-03 19:39:08 +02:00
|
|
|
"openquell/config"
|
2024-04-07 19:10:27 +02:00
|
|
|
"time"
|
2024-04-03 19:39:08 +02:00
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-08 18:51:25 +02:00
|
|
|
const (
|
|
|
|
|
Destruction = iota
|
|
|
|
|
Idle
|
|
|
|
|
Collision
|
2024-04-15 13:36:40 +02:00
|
|
|
Moving
|
2024-04-08 18:51:25 +02:00
|
|
|
)
|
|
|
|
|
|
2024-04-03 19:39:08 +02:00
|
|
|
type Animation struct {
|
2024-04-08 18:51:25 +02:00
|
|
|
Active bool // animation is running
|
|
|
|
|
Loop bool // remove the entity if false, loop endless otherwise
|
|
|
|
|
Index int // where we are currently
|
|
|
|
|
Sprites []assets.AnimationSprite // each element contains the sprite and duration
|
|
|
|
|
Width, Height int // single sprite measurements
|
2024-04-03 19:39:08 +02:00
|
|
|
Timer Timer
|
|
|
|
|
Trigger string
|
2024-04-08 18:51:25 +02:00
|
|
|
Data assets.TileAnimation
|
2024-04-03 19:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Renderable struct {
|
2024-04-08 18:51:25 +02:00
|
|
|
Pos *Position // just for debugging, will not used as positiion!
|
|
|
|
|
Image *ebiten.Image
|
|
|
|
|
DamageImage *ebiten.Image // FIXME: put into its own struct
|
|
|
|
|
Damaged int
|
|
|
|
|
Shader *ebiten.Shader
|
|
|
|
|
DestructionAnimate Animation
|
|
|
|
|
IdleAnimate Animation
|
|
|
|
|
CollisionAnimate Animation
|
2024-04-16 17:46:53 +02:00
|
|
|
MovingAnimate Animation
|
2024-04-08 18:51:25 +02:00
|
|
|
Hidden bool
|
2024-04-03 19:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-08 18:51:25 +02:00
|
|
|
func (render *Renderable) StartAnimation(which int) {
|
|
|
|
|
switch which {
|
|
|
|
|
case Collision:
|
2024-04-03 19:39:08 +02:00
|
|
|
fallthrough
|
2024-04-08 18:51:25 +02:00
|
|
|
case Destruction:
|
|
|
|
|
render.Hidden = true
|
|
|
|
|
render.DestructionAnimate.Active = true
|
|
|
|
|
render.DestructionAnimate.Timer.Start(config.ANIMATION_STARTWAIT)
|
|
|
|
|
render.IdleAnimate.Active = false
|
|
|
|
|
case Idle:
|
|
|
|
|
render.IdleAnimate.Active = true
|
|
|
|
|
render.IdleAnimate.Loop = true
|
|
|
|
|
render.IdleAnimate.Timer.Start(0)
|
2024-04-16 17:46:53 +02:00
|
|
|
case Moving:
|
|
|
|
|
render.MovingAnimate.Loop = true
|
2024-04-08 18:51:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (render *Renderable) StopAnimation(which int) {
|
|
|
|
|
switch which {
|
|
|
|
|
case Collision:
|
|
|
|
|
render.CollisionAnimate.Active = false
|
|
|
|
|
case Idle:
|
|
|
|
|
render.IdleAnimate.Active = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (render *Renderable) Animations() map[int]*Animation {
|
|
|
|
|
return map[int]*Animation{
|
|
|
|
|
Collision: &render.CollisionAnimate,
|
|
|
|
|
Destruction: &render.DestructionAnimate,
|
|
|
|
|
Idle: &render.IdleAnimate,
|
2024-04-03 19:39:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 19:10:27 +02:00
|
|
|
|
|
|
|
|
func (animation *Animation) GetSprite() *ebiten.Image {
|
|
|
|
|
return animation.Sprites[animation.Index].Sprite
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (animation *Animation) GetDuration() time.Duration {
|
|
|
|
|
return animation.Sprites[animation.Index].Duration * time.Millisecond
|
|
|
|
|
}
|