From a5aec01b6a2664c8280821fe14f9c151f7cfb424 Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Wed, 17 Nov 2021 22:44:36 -0500 Subject: [PATCH] Fix explosions --- src/asteroids.cr | 2 +- src/bullet.cr | 5 ++++- src/explosion.cr | 5 +++++ src/lx_game/emitter.cr | 4 ++-- src/lx_game/particle.cr | 11 +++-------- src/lx_game/sprite/age.cr | 7 ++++++- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/asteroids.cr b/src/asteroids.cr index c7f636a..d5bd06a 100644 --- a/src/asteroids.cr +++ b/src/asteroids.cr @@ -99,7 +99,7 @@ class Asteroids < Game bullet.position = wrap(bullet.position) end - @bullets = @bullets.reject { |b| b.age >= 4.0 } + @bullets = @bullets.reject(&.dead?) @asteroids.each do |a| a.update(dt) a.position = wrap(a.position) diff --git a/src/bullet.cr b/src/bullet.cr index eb3233a..16d3240 100644 --- a/src/bullet.cr +++ b/src/bullet.cr @@ -3,8 +3,11 @@ require "./lx_game/sprite/age" class Bullet < Sprite include LxGame::SpriteAge + @lifespan = 4.0 + def update(dt) - super + update_age(dt) + return if dead? update_position(dt) end diff --git a/src/explosion.cr b/src/explosion.cr index 9aaf39b..e174395 100644 --- a/src/explosion.cr +++ b/src/explosion.cr @@ -2,4 +2,9 @@ require "./lx_game/sprite/age" class Explosion < LxGame::Emitter include LxGame::SpriteAge + + def update(dt) + update_age(dt) + super + end end diff --git a/src/lx_game/emitter.cr b/src/lx_game/emitter.cr index df12a4c..974aabe 100644 --- a/src/lx_game/emitter.cr +++ b/src/lx_game/emitter.cr @@ -8,7 +8,7 @@ module LxGame property max_age : Float64 = 1.0 property emit_freq : Float64 = 0.05 property strength : Float64 = 50.0 - @last_emitted : Float64 = 0.0 + getter last_emitted : Float64 = 0.0 property emit_angle : Float64 = 2 * Math::PI property size : Float64 = 0.0 @@ -41,7 +41,7 @@ module LxGame end @particles.each { |particle| particle.update(dt) } - @particles.reject! { |particle| particle.dead? } + @particles.reject!(&.dead?) end def draw(renderer : SDL::Renderer) diff --git a/src/lx_game/particle.cr b/src/lx_game/particle.cr index 476b2f8..2ef6a9f 100644 --- a/src/lx_game/particle.cr +++ b/src/lx_game/particle.cr @@ -1,19 +1,14 @@ require "./sprite" +require "./sprite/age" module LxGame class Particle < Sprite - property age : Float64 = 0.0 - property lifespan : Float64 = 4.0 - @dead : Bool = false - - def dead? - @age >= @lifespan - end + include SpriteAge def update(dt : Float64) + update_age(dt) return if dead? update_position(dt) - @age += dt end def draw(renderer : SDL::Renderer) diff --git a/src/lx_game/sprite/age.cr b/src/lx_game/sprite/age.cr index c8ecba7..352d158 100644 --- a/src/lx_game/sprite/age.cr +++ b/src/lx_game/sprite/age.cr @@ -1,8 +1,13 @@ module LxGame module SpriteAge + property lifespan : Float64 = Float64::INFINITY property age : Float64 = 0.0 - def update(dt : Float64) + def dead? + self.age >= self.lifespan + end + + def update_age(dt : Float64) self.age += dt end end