module Draco::Events::WorldPlugin

Internal: The Events plugin for Draco Worlds.

Public Instance Methods

after_initialize() click to toggle source

Internal: The callback after the world is initialized

Returns nothing.

Calls superclass method
# File lib/draco/events.rb, line 24
def after_initialize
  super
  @events = []
end
before_tick(context) click to toggle source

Internal: The callback before tick is called on the World.

This method looks up the dispatched events and adds them to the list of systems to run.

context - The game engine's context for the current tick.

Returns the systems to run on the tick.

Calls superclass method
# File lib/draco/events.rb, line 37
def before_tick(context)
  systems = super
  dispatch_table = Hash.new { |h, k| h[k] = [] }

  @events.each { |event| dispatch_table[event.system] += event.entities || []  }

  dispatched = dispatch_table.map do |system, entities|
    entities = entities.any? ? entities : filter(system.filter)

    system.new(entities: entities, world: self)
  end

  @events = []

  dispatched + systems
end
component_added(entity, component) click to toggle source

Internal: The callback when a Component is added to an Event.

entity - The Entity that a component was added to component - The Component that was added to the entity

Returns nothing.

Calls superclass method
# File lib/draco/events.rb, line 60
def component_added(entity, component)
  super

  observers
    .select { |observer| observer.match?(entity, component, :add) }
    .each { |observer| dispatch(observer.system, [entity]) }
end
component_removed(entity, component) click to toggle source

Internal: The callback when a Component is removed from an Event.

entity - The Entity that a component was removed from component - The Component that was removed from the entity

Returns nothing.

Calls superclass method
# File lib/draco/events.rb, line 74
def component_removed(entity, component)
  super

  observers
    .select { |observer| observer.match?(entity, component, :remove) }
    .each { |observer| dispatch(observer.system, [entity]) }
end
dispatch(system, entities=nil) click to toggle source

Public: Dispatches an event to be run on the next tick.

system - The system to run entities - The entities to pass to the system. If this is nil, the

system's filter will check all of the world's entities

Returns nothing.

# File lib/draco/events.rb, line 89
def dispatch(system, entities=nil)
  @events << Event.new(system: system, entities: entities)
end
observers() click to toggle source

Internal: The list of observers added to the world.

Returns an array of Observers.

# File lib/draco/events.rb, line 96
def observers
  self.class.instance_variable_get(:@observers) || []
end