Events
Events are a way of communicating between systems, spaces and even scenes. Events can be subscribed to, unsubscribed and broadcasted via the App#subscribe
, App#unsubscribe
and App#broadcast
methods.
There is a wrapper for every SFML event:
app.subscribe(Event::Closed) { puts "window closed!"; exit(0) }
You can also define custom events:
class PlayerDeadEvent < Scar::Event::Event # Scar::Event is a module containing all builtin events!
property :pos
def initialize(@pos : Scar::Vec); end
end
class SomeSystem < Scar::System
include Scar
def init(app : App, space : Space)
app.subscribe(PlayerDeadEvent) { |e : PlayerDeadEvent | puts "Player died at #{e.pos.x} | #{e.pos.y}"}
end
end
class PlayerSystem < Scar::System
include Scar
def update(app : App, space : Space, dt)
pl = space["player"]
plc = pl[PlayerComponent]
app.broadcast(PlayerDeadEvent.new pl.position) if plc.health <= 0
end
end
Last updated
Was this helpful?