completed animation setup and code based solely on LDTK settings
This commit is contained in:
@@ -11,13 +11,13 @@ import (
|
||||
|
||||
type AnimationSystem struct {
|
||||
World *ecs.World
|
||||
Selector *generic.Filter3[Position, Animation, Timer]
|
||||
Selector *generic.Filter2[Position, Renderable]
|
||||
Cellsize int
|
||||
}
|
||||
|
||||
func NewAnimationSystem(world *ecs.World, cellsize int) System {
|
||||
system := &AnimationSystem{
|
||||
Selector: generic.NewFilter3[Position, Animation, Timer](),
|
||||
Selector: generic.NewFilter2[Position, Renderable](),
|
||||
World: world,
|
||||
Cellsize: cellsize,
|
||||
}
|
||||
@@ -26,31 +26,31 @@ func NewAnimationSystem(world *ecs.World, cellsize int) System {
|
||||
}
|
||||
|
||||
func (system *AnimationSystem) Update() error {
|
||||
// display debris after collecting
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
for query.Next() {
|
||||
// we loop, but it's only one anyway
|
||||
_, animation, timer := query.Get()
|
||||
|
||||
animation.Show = true
|
||||
|
||||
if timer.IsReady() {
|
||||
switch {
|
||||
// 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())
|
||||
_, render := query.Get()
|
||||
if render.Animate.Active {
|
||||
if render.Animate.Timer.IsReady() {
|
||||
switch {
|
||||
// animation shows from earlier tick, animate
|
||||
case render.Animate.Index > -1 && render.Animate.Index < len(render.Animate.Tiles)-1:
|
||||
render.Animate.Index += 1
|
||||
render.Animate.Timer.Start(config.ANIMATION_LOOPWAIT)
|
||||
default:
|
||||
// last sprite reached
|
||||
if render.Animate.Loop {
|
||||
render.Animate.Index = 0
|
||||
} else {
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
render.Animate.Timer.Update()
|
||||
}
|
||||
} else {
|
||||
timer.Update()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for _, entity := range EntitiesToRemove {
|
||||
@@ -66,12 +66,12 @@ func (system *AnimationSystem) Draw(screen *ebiten.Image) {
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
for query.Next() {
|
||||
pos, animation, _ := query.Get()
|
||||
pos, render := query.Get()
|
||||
|
||||
if animation.Show {
|
||||
if render.Animate.Active {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
screen.DrawImage(animation.Tiles[animation.Index], op)
|
||||
screen.DrawImage(render.Animate.Tiles[render.Animate.Index], op)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package systems
|
||||
|
||||
import (
|
||||
"openquell/assets"
|
||||
"log/slog"
|
||||
"openquell/components"
|
||||
. "openquell/components"
|
||||
"openquell/config"
|
||||
. "openquell/config"
|
||||
"openquell/observers"
|
||||
|
||||
@@ -15,13 +14,15 @@ import (
|
||||
|
||||
type CollectibleSystem struct {
|
||||
World *ecs.World
|
||||
Cellsize int
|
||||
Selector *generic.Filter3[Position, Collectible, Renderable]
|
||||
}
|
||||
|
||||
func NewCollectibleSystem(world *ecs.World) System {
|
||||
func NewCollectibleSystem(world *ecs.World, cellsize int) System {
|
||||
system := &CollectibleSystem{
|
||||
Selector: generic.NewFilter3[Position, Collectible, Renderable](),
|
||||
World: world,
|
||||
Cellsize: cellsize,
|
||||
}
|
||||
|
||||
return system
|
||||
@@ -33,9 +34,6 @@ func (system *CollectibleSystem) Update() error {
|
||||
posID := ecs.ComponentID[components.Position](system.World)
|
||||
veloID := ecs.ComponentID[components.Velocity](system.World)
|
||||
|
||||
animationpositions := []*components.Position{}
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
|
||||
query := system.Selector.Query(system.World)
|
||||
numcollectibles := query.Count()
|
||||
|
||||
@@ -45,7 +43,7 @@ func (system *CollectibleSystem) Update() error {
|
||||
}
|
||||
|
||||
for query.Next() {
|
||||
colposition, _, _ := query.Get()
|
||||
colposition, _, render := query.Get()
|
||||
|
||||
for _, player := range observer.GetPlayers() {
|
||||
if !system.World.Alive(player) {
|
||||
@@ -56,23 +54,22 @@ func (system *CollectibleSystem) Update() error {
|
||||
playervelocity := (*Velocity)(system.World.Get(player, veloID))
|
||||
|
||||
ok, _ := colposition.Intersects(playerposition, playervelocity)
|
||||
if ok {
|
||||
//slog.Debug("bumped into collectible", "collectible", collectible)
|
||||
animationpositions = append(animationpositions, colposition)
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
if ok && !render.Hidden {
|
||||
slog.Debug("bumped into collectible", "colpos", colposition)
|
||||
|
||||
render.StartAnimation()
|
||||
|
||||
// position the animation relative to the middle of the current entity
|
||||
colposition.Update(
|
||||
colposition.X-(system.Cellsize/2),
|
||||
colposition.Y-(system.Cellsize/2),
|
||||
64,
|
||||
)
|
||||
numcollectibles--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, pos := range animationpositions {
|
||||
system.AddAnimation(pos)
|
||||
}
|
||||
|
||||
for _, entity := range EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
numcollectibles--
|
||||
}
|
||||
|
||||
if numcollectibles == 0 {
|
||||
// winner, winner, chicken dinner!
|
||||
timer := observers.GetGameObserver(system.World).StopTimer
|
||||
@@ -92,36 +89,11 @@ func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
|
||||
for query.Next() {
|
||||
pos, _, sprite := query.Get()
|
||||
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
if !sprite.Hidden {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
|
||||
screen.DrawImage(sprite.Image, op)
|
||||
screen.DrawImage(sprite.Image, op)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (system *CollectibleSystem) AddAnimation(position *components.Position) {
|
||||
observer := observers.GetGameObserver(system.World)
|
||||
|
||||
ptmapper := generic.NewMap3[
|
||||
components.Position,
|
||||
components.Animation,
|
||||
components.Timer,
|
||||
](system.World)
|
||||
|
||||
animationID := ecs.ComponentID[components.Animation](system.World)
|
||||
|
||||
entity := ptmapper.New()
|
||||
pos, animation, timer := ptmapper.Get(entity)
|
||||
observer.AddEntity(entity, animationID)
|
||||
|
||||
animation.Index = assets.Tiles["Animation"].Animation
|
||||
animation.Tiles = assets.Tiles["Animation"].Tiles
|
||||
|
||||
pos.Update(
|
||||
position.X-(16), // FIXME: use global tilesize!
|
||||
position.Y-(16),
|
||||
64,
|
||||
)
|
||||
|
||||
timer.Start(config.ANIMATION_STARTWAIT)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package systems
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
. "openquell/components"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
@@ -58,7 +57,6 @@ func (system *GridSystem) Draw(screen *ebiten.Image) {
|
||||
counter++
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
slog.Debug("rendering tile", "sprite", sprite)
|
||||
system.Cache.DrawImage(sprite.Image, op)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user