Refactor event bus to use non-retrievable events;
update collision handling and add new inventory initialization; add torch lighting in fog shader;
This commit is contained in:
+19
-16
@@ -2,12 +2,11 @@ class EventBus
|
||||
def initialize
|
||||
# store callbacks with owner: {event => [ {callback:, owner:} ] }
|
||||
@events = Hash.new { |h, k| h[k] = [] }
|
||||
@retrievable_events = {}
|
||||
end
|
||||
|
||||
# Emit an event
|
||||
def emit(event, *args)
|
||||
@events[event].each do |h|
|
||||
@events[event].dup.each do |h|
|
||||
h[:callback].call(*args)
|
||||
end
|
||||
end
|
||||
@@ -28,30 +27,34 @@ class EventBus
|
||||
on(event, owner: owner, &wrapper)
|
||||
end
|
||||
|
||||
# Unsubscribe from an event
|
||||
def off(event, &callback)
|
||||
@events[event].reject! { |h| h[:callback] == callback }
|
||||
end
|
||||
|
||||
# Register a retrievable event (only one allowed)
|
||||
def on_retrievable(event, owner = nil, &callback)
|
||||
owner ||= callback.binding.eval("self")
|
||||
@retrievable_events[event] = { callback: callback, owner: owner }
|
||||
end
|
||||
|
||||
# Get data from a retrievable event
|
||||
# For events that return a value (e.g. queries), get the first non-nil response
|
||||
def get(event, *args)
|
||||
if @retrievable_events[event]
|
||||
@retrievable_events[event][:callback].call(*args)
|
||||
else
|
||||
nil
|
||||
@events[event].dup.each do |h|
|
||||
result = h[:callback].call(*args)
|
||||
return result unless result.nil?
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# Unsubscribe from an object's events
|
||||
def unlisten_owner(owner)
|
||||
# For events that return multiple values (e.g. queries), get all non-nil responses
|
||||
def get_all(event, *args)
|
||||
results = []
|
||||
@events[event].dup.each do |h|
|
||||
result = h[:callback].call(*args)
|
||||
results << result unless result.nil?
|
||||
end
|
||||
results
|
||||
end
|
||||
|
||||
# Unsubscribe all events for a given owner (e.g. when an object is destroyed)
|
||||
def remove_owner(owner)
|
||||
@events.each do |event, handlers|
|
||||
handlers.reject! { |h| h[:owner] == owner }
|
||||
end
|
||||
@retrievable_events.delete_if { |_, h| h[:owner] == owner }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user