Class: Motel::Callbacks::Movement

Inherits:
Base
  • Object
show all
Defined in:
lib/motel/callbacks.rb

Overview

Invoked upon specified minimum location movement

Instance Attribute Summary collapse

Attributes inherited from Base

#handler

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ Movement

Returns a new instance of Movement.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/motel/callbacks.rb', line 42

def initialize(args = {}, &block)
  @min_distance = 0
  @min_x = 0
  @min_y = 0
  @min_z = 0

  @min_distance = args[:min_distance] if args.has_key?(:min_distance)
  @min_x = args[:min_x] if args.has_key?(:min_x)
  @min_y = args[:min_y] if args.has_key?(:min_y)
  @min_z = args[:min_z] if args.has_key?(:min_z)

  # store original coordinates internally,
  # until minimum distances are satified
  # and callback is invoked, then clear
  @orig_x = @orig_y = @orig_z = nil

  super(args, &block)
end

Instance Attribute Details

#min_distanceObject

Minimum distance the location needs to move to trigger event



37
38
39
# File 'lib/motel/callbacks.rb', line 37

def min_distance
  @min_distance
end

#min_xObject

Minimum x,y,z distance the location needs to move to trigger the event



40
41
42
# File 'lib/motel/callbacks.rb', line 40

def min_x
  @min_x
end

#min_yObject

Minimum x,y,z distance the location needs to move to trigger the event



40
41
42
# File 'lib/motel/callbacks.rb', line 40

def min_y
  @min_y
end

#min_zObject

Minimum x,y,z distance the location needs to move to trigger the event



40
41
42
# File 'lib/motel/callbacks.rb', line 40

def min_z
  @min_z
end

Instance Method Details

#invoke(new_location, old_x, old_y, old_z) ⇒ Object

Calculate distance between location and old coordinates, invoke handler w/ location if minimums are true



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/motel/callbacks.rb', line 62

def invoke(new_location, old_x, old_y, old_z)
   # unless original coordinates is nil, ignore old coordinates passed in
   if @orig_x.nil?
     @orig_x = old_x
     @orig_y = old_y
     @orig_z = old_z
   end

   dx = new_location.x - @orig_x
   dy = new_location.y - @orig_y
   dz = new_location.z - @orig_z
   d  = Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2)

   if d >= @min_distance && dx.abs >= @min_x && dy.abs >= @min_y && dz.abs >= @min_z
     super([new_location, d, dx, dy, dz])
     @orig_x = @orig_y = @orig_z = nil
   end
end