class Draco::Events::Observer

Internal: An Observer checks when a component is added or removed from an entity and dispatches an event if the component matches the observer.

Attributes

actions[R]

Internal: The list of actions to match on. Supports `:add`, and `:remove`

components[R]

Internal: The Components to observe changes for. The observer matches if the modified component is any of the components in this variable.

system[R]

Internal: The System to dispatch when the observer matches

Public Class Methods

new(system, options = {}) click to toggle source

Internal: Initializes an Observer.

system - The system to run when the observer matches. options - The Hash options to pass to the initializer (default: {}).

:component - The component to observe (default: The system's filter).
:on - The actions to observe (default: [:add, :remove]).
# File lib/draco/events.rb, line 133
def initialize(system, options = {})
  @actions = Array(options[:on] || [:add, :remove])
  @components = Array(options[:component] || system.filter)
  @system = system
end

Public Instance Methods

match?(entity, component, action) click to toggle source

Internal: Says whether the current action matches the observer.

entity - The Entity to match against the System's filter. component - The Component that was added or removed from the Entity. action - Either :add or :remove.

Returns true or false.

# File lib/draco/events.rb, line 146
def match?(entity, component, action)
  actions.include?(action) &&
    components.include?(component.class) &&
    entity.components.map(&:class) & system.filter == system.filter
end