renamed particle* to animation*, added asesprite animation loading

This commit is contained in:
2024-04-02 20:05:23 +02:00
parent 20fa2639c6
commit 001b67f97a
14 changed files with 673 additions and 162 deletions

View File

@@ -9,15 +9,15 @@ import (
"github.com/mlange-42/arche/generic"
)
type ParticleSystem struct {
type AnimationSystem struct {
World *ecs.World
Selector *generic.Filter3[Position, Particle, Timer]
Selector *generic.Filter3[Position, Animation, Timer]
Cellsize int
}
func NewParticleSystem(world *ecs.World, cellsize int) System {
system := &ParticleSystem{
Selector: generic.NewFilter3[Position, Particle, Timer](),
func NewAnimationSystem(world *ecs.World, cellsize int) System {
system := &AnimationSystem{
Selector: generic.NewFilter3[Position, Animation, Timer](),
World: world,
Cellsize: cellsize,
}
@@ -25,7 +25,7 @@ func NewParticleSystem(world *ecs.World, cellsize int) System {
return system
}
func (system *ParticleSystem) Update() error {
func (system *AnimationSystem) Update() error {
// display debris after collecting
EntitiesToRemove := []ecs.Entity{}
@@ -33,16 +33,16 @@ func (system *ParticleSystem) Update() error {
for query.Next() {
// we loop, but it's only one anyway
_, particle, timer := query.Get()
_, animation, timer := query.Get()
particle.Show = true
animation.Show = true
if timer.IsReady() {
switch {
// particle shows from earlier tick, animate
case particle.Index > -1 && particle.Index < len(particle.Tiles)-1:
particle.Index++
timer.Start(config.PARTICLE_LOOPWAIT)
// animation shows from earlier tick, animate
case animation.Index > -1 && animation.Index < len(animation.Tiles)-1:
animation.Index++
timer.Start(config.ANIMATION_LOOPWAIT)
default:
// last sprite reached, remove it
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
@@ -60,18 +60,18 @@ func (system *ParticleSystem) Update() error {
return nil
}
func (system *ParticleSystem) Draw(screen *ebiten.Image) {
// write particles (these are no tiles!)
func (system *AnimationSystem) Draw(screen *ebiten.Image) {
// write animations (these are no tiles!)
op := &ebiten.DrawImageOptions{}
query := system.Selector.Query(system.World)
for query.Next() {
pos, particle, _ := query.Get()
pos, animation, _ := query.Get()
if particle.Show {
if animation.Show {
op.GeoM.Reset()
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
screen.DrawImage(particle.Tiles[particle.Index], op)
screen.DrawImage(animation.Tiles[animation.Index], op)
}
}
}

View File

@@ -33,7 +33,7 @@ func (system *CollectibleSystem) Update() error {
posID := ecs.ComponentID[components.Position](system.World)
veloID := ecs.ComponentID[components.Velocity](system.World)
particlepositions := []*components.Position{}
animationpositions := []*components.Position{}
EntitiesToRemove := []ecs.Entity{}
query := system.Selector.Query(system.World)
@@ -58,14 +58,14 @@ func (system *CollectibleSystem) Update() error {
ok, _ := colposition.Intersects(playerposition, playervelocity)
if ok {
//slog.Debug("bumped into collectible", "collectible", collectible)
particlepositions = append(particlepositions, colposition)
animationpositions = append(animationpositions, colposition)
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
}
}
}
for _, pos := range particlepositions {
system.AddParticle(pos)
for _, pos := range animationpositions {
system.AddAnimation(pos)
}
for _, entity := range EntitiesToRemove {
@@ -99,23 +99,23 @@ func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
}
}
func (system *CollectibleSystem) AddParticle(position *components.Position) {
func (system *CollectibleSystem) AddAnimation(position *components.Position) {
observer := observers.GetGameObserver(system.World)
ptmapper := generic.NewMap3[
components.Position,
components.Particle,
components.Animation,
components.Timer,
](system.World)
particleID := ecs.ComponentID[components.Particle](system.World)
animationID := ecs.ComponentID[components.Animation](system.World)
entity := ptmapper.New()
pos, particle, timer := ptmapper.Get(entity)
observer.AddEntity(entity, particleID)
pos, animation, timer := ptmapper.Get(entity)
observer.AddEntity(entity, animationID)
particle.Index = assets.Tiles["Particle"].Particle
particle.Tiles = assets.Tiles["Particle"].Tiles
animation.Index = assets.Tiles["Animation"].Animation
animation.Tiles = assets.Tiles["Animation"].Tiles
pos.Update(
position.X-(16), // FIXME: use global tilesize!
@@ -123,5 +123,5 @@ func (system *CollectibleSystem) AddParticle(position *components.Position) {
64,
)
timer.Start(config.PARTICLE_STARTWAIT)
timer.Start(config.ANIMATION_STARTWAIT)
}