mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-02-07 20:46:45 +01:00
Update emitter to use Entity
This commit is contained in:
parent
c8753f51a3
commit
0846fd525b
4 changed files with 46 additions and 21 deletions
27
examples/particles.cr
Normal file
27
examples/particles.cr
Normal 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!
|
|
@ -1,5 +1,5 @@
|
||||||
name: pixelfaucet
|
name: pixelfaucet
|
||||||
version: 0.0.2
|
version: 0.0.3
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- Alex Clink <alexclink@gmail.com>
|
- Alex Clink <alexclink@gmail.com>
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
require "./sprite"
|
require "./entity"
|
||||||
require "./particle"
|
require "./particle"
|
||||||
|
|
||||||
module PF
|
module PF
|
||||||
class Emitter < Sprite
|
class Emitter < Entity
|
||||||
property emitting : Bool = true
|
property emitting : Bool = true
|
||||||
property particles = [] of Particle
|
property particles = [] of Particle
|
||||||
property max_age : Float64 = 1.0
|
property max_age : Float64 = 1.0
|
||||||
|
@ -12,25 +12,23 @@ module PF
|
||||||
property emit_angle : Float64 = 2 * Math::PI
|
property emit_angle : Float64 = 2 * Math::PI
|
||||||
property size : Float64 = 0.0
|
property size : Float64 = 0.0
|
||||||
|
|
||||||
# property color : Pixel = Pixel.new
|
|
||||||
|
|
||||||
def generate_particle
|
def generate_particle
|
||||||
Particle.build do |particle|
|
particle = Particle.new
|
||||||
particle.position = @position
|
particle.position = @position
|
||||||
|
|
||||||
if @size > 0.0
|
if @size > 0.0
|
||||||
particle.position.x += rand(-@size..@size)
|
particle.position.x += rand(-@size..@size)
|
||||||
particle.position.y += 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
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def update(dt : Float64)
|
def update(dt : Float64)
|
||||||
update_position(dt)
|
super(dt)
|
||||||
|
|
||||||
@last_emitted += dt
|
@last_emitted += dt
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
require "./sprite"
|
require "./entity"
|
||||||
require "./sprite/age"
|
require "./entity/entity_age"
|
||||||
|
|
||||||
module PF
|
module PF
|
||||||
class Particle < Sprite
|
class Particle < Entity
|
||||||
include SpriteAge
|
include EntityAge
|
||||||
|
|
||||||
def update(dt : Float64)
|
def update(dt : Float64)
|
||||||
update_age(dt)
|
update_age(dt)
|
||||||
return if dead?
|
return if dead?
|
||||||
update_position(dt)
|
super(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw(engine)
|
def draw(engine)
|
||||||
|
|
Loading…
Add table
Reference in a new issue