scar
  • The scar game library
  • Application Structure
    • App
    • Scenes
    • Spaces
    • Entities
    • Components
    • Systems
    • Objects
    • Drawing
    • Events
  • Builtin Features
    • Actions
    • Assets
    • Config
    • Input
    • Logger
    • Music
    • Tweens
    • Util
    • Vectors & Rects
  • Builtin Components
    • Text
    • Sprite
    • Animated Sprite
  • Builtin Systems
    • Animate Sprites
Powered by GitBook
On this page

Was this helpful?

  1. Application Structure

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
PreviousDrawingNextActions

Last updated 5 years ago

Was this helpful?