2024-02-10 19:45:06 +01:00
|
|
|
package systems
|
2024-02-09 20:20:13 +01:00
|
|
|
|
|
|
|
|
import (
|
2024-04-08 18:51:25 +02:00
|
|
|
"log/slog"
|
|
|
|
|
"openquell/components"
|
2024-02-09 20:20:13 +01:00
|
|
|
. "openquell/components"
|
|
|
|
|
|
2024-02-10 19:45:06 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2024-02-09 20:20:13 +01:00
|
|
|
"github.com/mlange-42/arche/ecs"
|
|
|
|
|
"github.com/mlange-42/arche/generic"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-02 20:05:23 +02:00
|
|
|
type AnimationSystem struct {
|
2024-02-09 20:20:13 +01:00
|
|
|
World *ecs.World
|
2024-04-03 19:39:08 +02:00
|
|
|
Selector *generic.Filter2[Position, Renderable]
|
2024-02-09 20:20:13 +01:00
|
|
|
Cellsize int
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 20:05:23 +02:00
|
|
|
func NewAnimationSystem(world *ecs.World, cellsize int) System {
|
|
|
|
|
system := &AnimationSystem{
|
2024-04-03 19:39:08 +02:00
|
|
|
Selector: generic.NewFilter2[Position, Renderable](),
|
2024-02-10 19:45:06 +01:00
|
|
|
World: world,
|
|
|
|
|
Cellsize: cellsize,
|
2024-02-09 20:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return system
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 20:05:23 +02:00
|
|
|
func (system *AnimationSystem) Update() error {
|
2024-02-11 13:00:56 +01:00
|
|
|
EntitiesToRemove := []ecs.Entity{}
|
|
|
|
|
|
2024-02-09 20:20:13 +01:00
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
2024-04-03 19:39:08 +02:00
|
|
|
_, render := query.Get()
|
2024-04-08 18:51:25 +02:00
|
|
|
|
|
|
|
|
for animationtype, animate := range render.Animations() {
|
|
|
|
|
if animate.Active {
|
|
|
|
|
if animate.Timer.IsReady() {
|
|
|
|
|
switch {
|
|
|
|
|
// animation shows from earlier tick, animate
|
|
|
|
|
case animate.Index > -1 && animate.Index < len(animate.Sprites)-1:
|
|
|
|
|
animate.Index += 1
|
|
|
|
|
animate.Timer.Start(animate.GetDuration())
|
|
|
|
|
default:
|
|
|
|
|
// last sprite reached
|
|
|
|
|
if animate.Loop {
|
|
|
|
|
animate.Index = 0
|
|
|
|
|
animate.Timer.Start(animate.GetDuration())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if animationtype == components.Destruction {
|
|
|
|
|
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
|
|
|
|
}
|
2024-04-03 19:39:08 +02:00
|
|
|
}
|
2024-04-08 18:51:25 +02:00
|
|
|
} else {
|
|
|
|
|
animate.Timer.Update()
|
2024-04-03 19:39:08 +02:00
|
|
|
}
|
2024-02-12 17:27:52 +01:00
|
|
|
}
|
2024-02-09 20:20:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-11 13:00:56 +01:00
|
|
|
|
|
|
|
|
for _, entity := range EntitiesToRemove {
|
2024-04-08 18:51:25 +02:00
|
|
|
slog.Debug("remove collectible")
|
2024-02-11 13:00:56 +01:00
|
|
|
system.World.RemoveEntity(entity)
|
|
|
|
|
}
|
2024-02-23 18:47:15 +01:00
|
|
|
|
|
|
|
|
return nil
|
2024-02-09 20:20:13 +01:00
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
2024-04-02 20:05:23 +02:00
|
|
|
func (system *AnimationSystem) Draw(screen *ebiten.Image) {
|
|
|
|
|
// write animations (these are no tiles!)
|
2024-02-10 19:45:06 +01:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
2024-04-03 19:39:08 +02:00
|
|
|
pos, render := query.Get()
|
2024-02-10 19:45:06 +01:00
|
|
|
|
2024-04-08 18:51:25 +02:00
|
|
|
for _, animate := range render.Animations() {
|
|
|
|
|
if animate.Active {
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
|
|
|
|
sprite := animate.GetSprite()
|
|
|
|
|
screen.DrawImage(sprite, op)
|
|
|
|
|
}
|
2024-02-12 17:27:52 +01:00
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
}
|