added Timer, Particle Render Delay (via Particle.Show) +new sprites

This commit is contained in:
Thomas von Dein 2024-02-12 17:27:52 +01:00
parent 65ddec3fa4
commit 5a288035b3
14 changed files with 80 additions and 20 deletions

View File

@ -5,13 +5,13 @@ Background: background-lila
######## ########
# #o# #o o #o#
# S # # # S # #
# #### # # #### #
# #### # # #### #
# # # #
# o # #o o#
######## ########

View File

@ -110,8 +110,7 @@ func InitTiles() TileRegistry {
'S': NewTilePlayer(), 'S': NewTilePlayer(),
'o': NewTileCollectible("collectible-orange"), 'o': NewTileCollectible("collectible-orange"),
'*': NewTileParticle([]string{ '*': NewTileParticle([]string{
"particle-dust-0", "particle-dust-0", "particle-dust-1", "particle-dust-1", "particle-ring-1", "particle-ring-2", "particle-ring-3", "particle-ring-4", "particle-ring-5", "particle-ring-6",
"particle-dust-0", "particle-dust-2", "particle-dust-3", "particle-dust-3",
}), }),
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -11,6 +11,7 @@ type Renderable struct {
} }
type Particle struct { type Particle struct {
Show bool
Index int Index int
Particles []*ebiten.Image Particles []*ebiten.Image
} }

41
components/timer.go Normal file
View File

@ -0,0 +1,41 @@
package components
import (
"time"
"github.com/hajimehoshi/ebiten/v2"
)
type Timer struct {
currentTicks int
targetTicks int
}
func NewTimer(d time.Duration) *Timer {
return &Timer{}
}
func (timer *Timer) Start(d time.Duration) {
min := int(1000 / ebiten.TPS()) // 16.66ms == 1 tick
useD := d
if int(d.Milliseconds()) < min {
useD = time.Duration(1000/ebiten.TPS()) * time.Millisecond
}
timer.targetTicks = int(useD.Milliseconds()) / min // id d=50, then 50 / 16.6 =
}
func (t *Timer) Update() {
if t.currentTicks < t.targetTicks {
t.currentTicks++
}
}
func (t *Timer) IsReady() bool {
return t.currentTicks >= t.targetTicks
}
func (t *Timer) Reset() {
t.currentTicks = 0
}

View File

@ -1,5 +1,7 @@
package config package config
import "time"
const ( const (
Stop = iota Stop = iota
East East
@ -9,3 +11,4 @@ const (
) )
const PLAYERSPEED int = 4 const PLAYERSPEED int = 4
const PARTICLE_LOOPWAIT time.Duration = 250 * time.Millisecond

Binary file not shown.

View File

@ -5,6 +5,7 @@ import (
"openquell/assets" "openquell/assets"
"openquell/components" "openquell/components"
. "openquell/components" . "openquell/components"
"openquell/config"
"openquell/observers" "openquell/observers"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -31,6 +32,7 @@ func (system *CollectibleSystem) Update() {
posID := ecs.ComponentID[components.Position](system.World) posID := ecs.ComponentID[components.Position](system.World)
veloID := ecs.ComponentID[components.Velocity](system.World) veloID := ecs.ComponentID[components.Velocity](system.World)
particlepositions := []*components.Position{} particlepositions := []*components.Position{}
EntitiesToRemove := []ecs.Entity{} EntitiesToRemove := []ecs.Entity{}
@ -93,13 +95,14 @@ func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
func (system *CollectibleSystem) AddParticle(position *components.Position) { func (system *CollectibleSystem) AddParticle(position *components.Position) {
particleobserver := observers.GetParticleObserver(system.World) particleobserver := observers.GetParticleObserver(system.World)
ptmapper := generic.NewMap2[ ptmapper := generic.NewMap3[
components.Position, components.Position,
components.Particle, components.Particle,
components.Timer,
](system.World) ](system.World)
entity := ptmapper.New() entity := ptmapper.New()
pos, particle := ptmapper.Get(entity) pos, particle, timer := ptmapper.Get(entity)
particleobserver.AddEntity(entity) particleobserver.AddEntity(entity)
particle.Index = assets.Tiles['*'].Particle particle.Index = assets.Tiles['*'].Particle
@ -110,4 +113,6 @@ func (system *CollectibleSystem) AddParticle(position *components.Position) {
position.Y-(16), position.Y-(16),
64, 64,
) )
timer.Start(config.PARTICLE_LOOPWAIT)
} }

View File

@ -2,6 +2,7 @@ package systems
import ( import (
. "openquell/components" . "openquell/components"
"openquell/config"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/mlange-42/arche/ecs" "github.com/mlange-42/arche/ecs"
@ -10,13 +11,13 @@ import (
type ParticleSystem struct { type ParticleSystem struct {
World *ecs.World World *ecs.World
Selector *generic.Filter2[Position, Particle] Selector *generic.Filter3[Position, Particle, Timer]
Cellsize int Cellsize int
} }
func NewParticleSystem(world *ecs.World, cellsize int) *ParticleSystem { func NewParticleSystem(world *ecs.World, cellsize int) *ParticleSystem {
system := &ParticleSystem{ system := &ParticleSystem{
Selector: generic.NewFilter2[Position, Particle](), Selector: generic.NewFilter3[Position, Particle, Timer](),
World: world, World: world,
Cellsize: cellsize, Cellsize: cellsize,
} }
@ -32,16 +33,24 @@ func (system *ParticleSystem) Update() {
for query.Next() { for query.Next() {
// we loop, but it's only one anyway // we loop, but it's only one anyway
_, particle := query.Get() _, particle, timer := query.Get()
switch { particle.Show = true
// particle shows from earlier tick, animate
case particle.Index > -1 && particle.Index < len(particle.Particles)-1: if timer.IsReady() {
particle.Index++ switch {
default: // particle shows from earlier tick, animate
// last sprite reached, remove it case particle.Index > -1 && particle.Index < len(particle.Particles)-1:
EntitiesToRemove = append(EntitiesToRemove, query.Entity()) particle.Index++
timer.Start(config.PARTICLE_LOOPWAIT)
default:
// last sprite reached, remove it
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
}
} else {
timer.Update()
} }
} }
for _, entity := range EntitiesToRemove { for _, entity := range EntitiesToRemove {
@ -55,10 +64,12 @@ func (system *ParticleSystem) Draw(screen *ebiten.Image) {
query := system.Selector.Query(system.World) query := system.Selector.Query(system.World)
for query.Next() { for query.Next() {
pos, particle := query.Get() pos, particle, _ := query.Get()
op.GeoM.Reset() if particle.Show {
op.GeoM.Translate(float64(pos.X), float64(pos.Y)) op.GeoM.Reset()
screen.DrawImage(particle.Particles[particle.Index], op) op.GeoM.Translate(float64(pos.X), float64(pos.Y))
screen.DrawImage(particle.Particles[particle.Index], op)
}
} }
} }