Compare commits

...

1 Commits

Author SHA1 Message Date
d23861a427 started with animation interface 2024-04-15 14:42:52 +02:00

View File

@ -15,7 +15,18 @@ const (
Moving
)
type Animation struct {
type AnimationTrigger int
type Animation interface {
GetType() AnimationTrigger
StartAnimation()
StopAnimation()
GetSprite() *ebiten.Image
GetDuration() time.Duration
}
// implements the AnimationInterface
type StandardAnimation struct {
Active bool // animation is running
Loop bool // remove the entity if false, loop endless otherwise
Index int // where we are currently
@ -24,6 +35,59 @@ type Animation struct {
Timer Timer
Trigger string
Data assets.TileAnimation
Render *Renderable
Which AnimationTrigger
}
func (animation *StandardAnimation) StartAnimation() {
animation.Active = true
animation.Timer.Start(0)
}
func (animation *StandardAnimation) StopAnimation() {
animation.Active = false
}
func (animation *StandardAnimation) GetSprite() *ebiten.Image {
return animation.Sprites[animation.Index].Sprite
}
func (animation *StandardAnimation) GetDuration() time.Duration {
return animation.Sprites[animation.Index].Duration * time.Millisecond
}
func (animation *StandardAnimation) GetType() AnimationTrigger {
return animation.Which
}
type CollisionAnimation struct {
StandardAnimation
}
type IdleAnimation struct {
StandardAnimation
}
type DestructionAnimation struct {
StandardAnimation
}
type MovingAnimation struct {
StandardAnimation
}
func (animation *DestructionAnimation) StartAnimation() {
animation.Render.StopAnimations()
animation.Render.Hidden = true
animation.Active = true
animation.Timer.Start(config.ANIMATION_STARTWAIT)
}
func (animation *IdleAnimation) StartAnimation() {
animation.Render.StopAnimations()
animation.Active = true
animation.Loop = true
animation.Timer.Start(0)
}
type Renderable struct {
@ -32,49 +96,28 @@ type Renderable struct {
DamageImage *ebiten.Image // FIXME: put into its own struct
Damaged int
Shader *ebiten.Shader
DestructionAnimate Animation
IdleAnimate Animation
CollisionAnimate Animation
Animations map[AnimationTrigger]Animation
Hidden bool
}
func (render *Renderable) StartAnimation(which int) {
func (render *Renderable) StopAnimations() {
for _, animation := range render.Animations {
animation.StopAnimation()
}
}
func (render *Renderable) AddAnimation(which AnimationTrigger) {
render.Animations = make(map[AnimationTrigger]Animation)
switch which {
case Collision:
fallthrough
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)
idle := &IdleAnimation{
StandardAnimation: StandardAnimation{
Which: Idle,
Render: render,
},
}
render.Animations[Idle] = idle
}
}
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,
}
}
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
}