Class: Rubygame::EventTriggers::OrTrigger

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygame/event_triggers.rb

Overview

OrTrigger is an event trigger which contains one or more other triggers, and fires when an event matches one or more of its triggers.

Contrast with AndTrigger.

Instance Method Summary collapse

Constructor Details

#initialize(*triggers) ⇒ OrTrigger

Initialize a new instance of OrTrigger, containing the given triggers.

*triggers

The triggers to contain. (Array of triggers, required)

Example:

is_red = AttrTrigger.new( :color => :red )
is_blue = AttrTrigger.new( :color => :blue )

# Matches only an event which has EITHER:
#  1. #color == :red, OR
#  2. #color == :blue
is_red_or_blue = OrTrigger.new( is_red, is_blue )

# More complex example with nested logic triggers:

changed = InstanceOfTrigger.new( ColorChanged )

changed_to_red_or_blue = 
  AndTrigger.new( changed, is_red_or_blue )


157
158
159
# File 'lib/rubygame/event_triggers.rb', line 157

def initialize( *triggers )
	@triggers = triggers
end

Instance Method Details

#match?(event) ⇒ Boolean

Returns true if the event matches one or more of the triggers that the OrTrigger contains.

Returns:

  • (Boolean)


164
165
166
# File 'lib/rubygame/event_triggers.rb', line 164

def match?( event )
	@triggers.any? { |trigger| trigger.match? event }
end