module AdventureRL
module EventHandlers
# An EventHandler can have multiple Event s.
# You can subscribe an object to a specific Event using its name,
# through the EventHandler.
class EventHandler
def initialize settings = {}
@events = []
end
# Add an Event to this EventHandler.
def add_event event
Helpers::Error.error(
"Passed `event' is not an instance of `Event'.",
"Got `#{event.inspect}:#{event.class.name}'."
) unless (event.is_a? Event)
@events << event
end
alias_method :add, :add_event
alias_method :<<, :add_event
# Subscribe an object to all Event s in this EventHandler.
def subscribe object
Helpers::Error.error(
"Object `#{object.inspect}:#{object.class.name}' cannot subscribe",
"to this EventHandler `#{self.inspect}:#{self.class.name}'."
) unless (valid_object? object)
@events.each do |event|
event.add_object object
end
end
# Unsubscribe an object from all Event s in this EventHandler.
def unsubscribe object
@events.each do |event|
event.remove_object object
end
end
# Trigger the Event with the name event_name.
# Returns true if the Event was found and false if not.
# Optionally, additional args arguments can be passed,
# which will be passed to the trigger methods on the Event s.
def trigger event_name, *args
event = @events.detect do |evnt|
evnt.get_name == event_name
end
event.trigger *args if (event)
return !!event
end
private
# Returns true if the passed object can be subscribed to this EventHandler,
# and false if not.
# This method should be overwritten to fit specific requirements.
def valid_object? object
return !!object
end
end
end
end