Fix explosions

This commit is contained in:
Alex Clink 2021-11-17 22:44:36 -05:00
parent 0d1592bf03
commit a5aec01b6a
6 changed files with 21 additions and 13 deletions

View file

@ -99,7 +99,7 @@ class Asteroids < Game
bullet.position = wrap(bullet.position) bullet.position = wrap(bullet.position)
end end
@bullets = @bullets.reject { |b| b.age >= 4.0 } @bullets = @bullets.reject(&.dead?)
@asteroids.each do |a| @asteroids.each do |a|
a.update(dt) a.update(dt)
a.position = wrap(a.position) a.position = wrap(a.position)

View file

@ -3,8 +3,11 @@ require "./lx_game/sprite/age"
class Bullet < Sprite class Bullet < Sprite
include LxGame::SpriteAge include LxGame::SpriteAge
@lifespan = 4.0
def update(dt) def update(dt)
super update_age(dt)
return if dead?
update_position(dt) update_position(dt)
end end

View file

@ -2,4 +2,9 @@ require "./lx_game/sprite/age"
class Explosion < LxGame::Emitter class Explosion < LxGame::Emitter
include LxGame::SpriteAge include LxGame::SpriteAge
def update(dt)
update_age(dt)
super
end
end end

View file

@ -8,7 +8,7 @@ module LxGame
property max_age : Float64 = 1.0 property max_age : Float64 = 1.0
property emit_freq : Float64 = 0.05 property emit_freq : Float64 = 0.05
property strength : Float64 = 50.0 property strength : Float64 = 50.0
@last_emitted : Float64 = 0.0 getter last_emitted : Float64 = 0.0
property emit_angle : Float64 = 2 * Math::PI property emit_angle : Float64 = 2 * Math::PI
property size : Float64 = 0.0 property size : Float64 = 0.0
@ -41,7 +41,7 @@ module LxGame
end end
@particles.each { |particle| particle.update(dt) } @particles.each { |particle| particle.update(dt) }
@particles.reject! { |particle| particle.dead? } @particles.reject!(&.dead?)
end end
def draw(renderer : SDL::Renderer) def draw(renderer : SDL::Renderer)

View file

@ -1,19 +1,14 @@
require "./sprite" require "./sprite"
require "./sprite/age"
module LxGame module LxGame
class Particle < Sprite class Particle < Sprite
property age : Float64 = 0.0 include SpriteAge
property lifespan : Float64 = 4.0
@dead : Bool = false
def dead?
@age >= @lifespan
end
def update(dt : Float64) def update(dt : Float64)
update_age(dt)
return if dead? return if dead?
update_position(dt) update_position(dt)
@age += dt
end end
def draw(renderer : SDL::Renderer) def draw(renderer : SDL::Renderer)

View file

@ -1,8 +1,13 @@
module LxGame module LxGame
module SpriteAge module SpriteAge
property lifespan : Float64 = Float64::INFINITY
property age : Float64 = 0.0 property age : Float64 = 0.0
def update(dt : Float64) def dead?
self.age >= self.lifespan
end
def update_age(dt : Float64)
self.age += dt self.age += dt
end end
end end