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 # #
# #### #
# #### #
# #
# o #
#o o#
########

View File

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

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

View File

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