Class: Motel::Callbacks::Proximity
Overview
Invoked upon specified maximum distance between locations
Instance Attribute Summary collapse
-
#event ⇒ Object
proximity event which to trigger on.
-
#max_distance ⇒ Object
Max distance the locations needs to be apart to trigger event.
-
#max_x ⇒ Object
Max x,y,z distance the locations need to be to trigger the event.
-
#max_y ⇒ Object
Max x,y,z distance the locations need to be to trigger the event.
-
#max_z ⇒ Object
Max x,y,z distance the locations need to be to trigger the event.
-
#to_location ⇒ Object
location which to compare to.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(args = {}, &block) ⇒ Proximity
constructor
A new instance of Proximity.
-
#invoke(location) ⇒ Object
Calculate distance between specified location and stored one, invoke handler w/ specified location if they are within proximity.
Constructor Details
#initialize(args = {}, &block) ⇒ Proximity
Returns a new instance of Proximity.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/motel/callbacks.rb', line 96 def initialize(args = {}, &block) @max_distance = 0 @max_x = 0 @max_y = 0 @max_z = 0 @to_location = nil @event = :proximity @max_distance = args[:max_distance] if args.has_key?(:max_distance) @max_x = args[:max_x] if args.has_key?(:max_x) @max_y = args[:max_y] if args.has_key?(:max_y) @max_z = args[:max_z] if args.has_key?(:max_z) @to_location = args[:to_location] if args.has_key?(:to_location) @event = args[:event].intern if args.has_key?(:event) # keep track of proximity state internally for different event types @locations_in_proximity = false super(args, &block) end |
Instance Attribute Details
#event ⇒ Object
proximity event which to trigger on
88 89 90 |
# File 'lib/motel/callbacks.rb', line 88 def event @event end |
#max_distance ⇒ Object
Max distance the locations needs to be apart to trigger event
91 92 93 |
# File 'lib/motel/callbacks.rb', line 91 def max_distance @max_distance end |
#max_x ⇒ Object
Max x,y,z distance the locations need to be to trigger the event
94 95 96 |
# File 'lib/motel/callbacks.rb', line 94 def max_x @max_x end |
#max_y ⇒ Object
Max x,y,z distance the locations need to be to trigger the event
94 95 96 |
# File 'lib/motel/callbacks.rb', line 94 def max_y @max_y end |
#max_z ⇒ Object
Max x,y,z distance the locations need to be to trigger the event
94 95 96 |
# File 'lib/motel/callbacks.rb', line 94 def max_z @max_z end |
#to_location ⇒ Object
location which to compare to
85 86 87 |
# File 'lib/motel/callbacks.rb', line 85 def to_location @to_location end |
Instance Method Details
#invoke(location) ⇒ Object
Calculate distance between specified location and stored one, invoke handler w/ specified location if they are within proximity
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/motel/callbacks.rb', line 119 def invoke(location) dx = (location.x - to_location.x).abs dy = (location.y - to_location.y).abs dz = (location.z - to_location.z).abs d = Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2) currently_in_proximity = (d <= @max_distance) || (dx <= @max_x && dy <= @max_y && dz <= @max_z) trigger_callback = (currently_in_proximity && (@event == :proximity || (@event == :entered_proximity && !@locations_in_proximity))) || (!currently_in_proximity && (@event == :left_proximity && @locations_in_proximity)) @locations_in_proximity = currently_in_proximity super([location, to_location]) if trigger_callback end |