Update emitter to use Entity

This commit is contained in:
Alex Clink 2022-01-03 20:55:39 -05:00
parent c8753f51a3
commit 0846fd525b
4 changed files with 46 additions and 21 deletions

27
examples/particles.cr Normal file
View file

@ -0,0 +1,27 @@
require "../src/game"
require "../src/emitter"
module PF
class Example < Game
@emitter : Emitter
def initialize(*args, **kwargs)
super
@emitter = Emitter.new
@emitter.position = Point.new(@width / 2, @height / 2)
end
def update(dt, event)
@emitter.update(dt)
end
def draw
clear(0, 0, 0)
@emitter.draw(self)
end
end
end
example = PF::Example.new(200, 200, 2)
example.run!

View file

@ -1,5 +1,5 @@
name: pixelfaucet
version: 0.0.2
version: 0.0.3
authors:
- Alex Clink <alexclink@gmail.com>

View file

@ -1,8 +1,8 @@
require "./sprite"
require "./entity"
require "./particle"
module PF
class Emitter < Sprite
class Emitter < Entity
property emitting : Bool = true
property particles = [] of Particle
property max_age : Float64 = 1.0
@ -12,25 +12,23 @@ module PF
property emit_angle : Float64 = 2 * Math::PI
property size : Float64 = 0.0
# property color : Pixel = Pixel.new
def generate_particle
Particle.build do |particle|
particle.position = @position
particle = Particle.new
particle.position = @position
if @size > 0.0
particle.position.x += rand(-@size..@size)
particle.position.y += rand(-@size..@size)
end
direction = rand((@rotation - @emit_angle)..(@rotation + @emit_angle))
particle.velocity = @velocity + Vector2.new(Math.cos(direction), Math.sin(direction)) * @strength
particle.lifespan = @max_age
if @size > 0.0
particle.position.x += rand(-@size..@size)
particle.position.y += rand(-@size..@size)
end
direction = rand((@rotation - @emit_angle)..(@rotation + @emit_angle))
particle.velocity = @velocity + Point.new(Math.cos(direction), Math.sin(direction)) * @strength
particle.lifespan = @max_age
particle
end
def update(dt : Float64)
update_position(dt)
super(dt)
@last_emitted += dt

View file

@ -1,14 +1,14 @@
require "./sprite"
require "./sprite/age"
require "./entity"
require "./entity/entity_age"
module PF
class Particle < Sprite
include SpriteAge
class Particle < Entity
include EntityAge
def update(dt : Float64)
update_age(dt)
return if dead?
update_position(dt)
super(dt)
end
def draw(engine)