From d23861a427f47a22273f9675cc9c470469cdfb4c Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 15 Apr 2024 14:42:52 +0200 Subject: [PATCH] started with animation interface --- components/renderable.go | 131 ++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 44 deletions(-) diff --git a/components/renderable.go b/components/renderable.go index f7d1a34..985dc6a 100644 --- a/components/renderable.go +++ b/components/renderable.go @@ -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,57 +35,89 @@ type Animation struct { Timer Timer Trigger string Data assets.TileAnimation + Render *Renderable + Which AnimationTrigger } -type Renderable struct { - 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 - Hidden bool +func (animation *StandardAnimation) StartAnimation() { + animation.Active = true + animation.Timer.Start(0) } -func (render *Renderable) StartAnimation(which int) { - 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) - } +func (animation *StandardAnimation) StopAnimation() { + animation.Active = false } -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 { +func (animation *StandardAnimation) GetSprite() *ebiten.Image { return animation.Sprites[animation.Index].Sprite } -func (animation *Animation) GetDuration() time.Duration { +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 { + 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 + Animations map[AnimationTrigger]Animation + Hidden bool +} + +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 Idle: + idle := &IdleAnimation{ + StandardAnimation: StandardAnimation{ + Which: Idle, + Render: render, + }, + } + render.Animations[Idle] = idle + + } +}