Class: Motel::Location
- Inherits:
-
Object
- Object
- Motel::Location
- Defined in:
- lib/motel/location.rb
Overview
A Location defines an optional parent location and the x,y,z cartesian coordinates of the location relative to that parent. If parent is not specified x,y,z are ignored and this location is assumed to be the ‘center’ of the system to which it belongs Also is related to a movable object defining the parameters of the locations movement
Instance Attribute Summary collapse
-
#children ⇒ Object
handle to parent location and array of children.
-
#entity ⇒ Object
a generic association which this location can belong to.
-
#id ⇒ Object
id of location and parent, and coordinates relative to that parent.
-
#movement_callbacks ⇒ Object
array of callbacks to be invoked on movement.
-
#movement_strategy ⇒ Object
movement strategy which location move in accordance to.
-
#parent ⇒ Object
handle to parent location and array of children.
-
#parent_id ⇒ Object
id of location and parent, and coordinates relative to that parent.
-
#proximity_callbacks ⇒ Object
Array of callbacks to be invoked on proximity.
-
#x ⇒ Object
id of location and parent, and coordinates relative to that parent.
-
#y ⇒ Object
id of location and parent, and coordinates relative to that parent.
-
#z ⇒ Object
id of location and parent, and coordinates relative to that parent.
Instance Method Summary collapse
-
#-(location) ⇒ Object
return the distance between this location and specified other.
-
#coordinates ⇒ Object
return this locations coordinates in an array.
-
#initialize(args = {}) ⇒ Location
constructor
A new instance of Location.
-
#root ⇒ Object
return this location’s root location.
-
#total_x ⇒ Object
return sum of the x values of this location and all its parents, eg the absolute ‘x’ of the location.
-
#total_y ⇒ Object
return sum of the y values of this location and all its parents eg the absolute ‘y’ of the location.
-
#total_z ⇒ Object
return sum of the z values of this location and all its parents eg the absolute ‘z’ of the location.
-
#traverse_descendants(&bl) ⇒ Object
traverse all chilren recursively, calling block for each.
-
#update(location) ⇒ Object
update this location’s attributes to match other’s set attributes.
Constructor Details
#initialize(args = {}) ⇒ Location
Returns a new instance of Location.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/motel/location.rb', line 38 def initialize(args = {}) # default to the stopped movement strategy @movement_strategy = MovementStrategies::Stopped.instance @movement_callbacks = [] @proximity_callbacks = [] @children = [] @x = nil @y = nil @z = nil @id = args[:id] if args.has_key? :id @parent_id = args[:parent_id] if args.has_key? :parent_id @x = args[:x] if args.has_key? :x @y = args[:y] if args.has_key? :y @z = args[:z] if args.has_key? :z @parent = args[:parent] if args.has_key? :parent @parent.children.push self unless @parent.nil? || @parent.children.include?(self) @movement_strategy = args[:movement_strategy] if args.has_key? :movement_strategy end |
Instance Attribute Details
#children ⇒ Object
handle to parent location and array of children
24 25 26 |
# File 'lib/motel/location.rb', line 24 def children @children end |
#entity ⇒ Object
a generic association which this location can belong to
36 37 38 |
# File 'lib/motel/location.rb', line 36 def entity @entity end |
#id ⇒ Object
id of location and parent, and coordinates relative to that parent
21 22 23 |
# File 'lib/motel/location.rb', line 21 def id @id end |
#movement_callbacks ⇒ Object
array of callbacks to be invoked on movement
30 31 32 |
# File 'lib/motel/location.rb', line 30 def movement_callbacks @movement_callbacks end |
#movement_strategy ⇒ Object
movement strategy which location move in accordance to
27 28 29 |
# File 'lib/motel/location.rb', line 27 def movement_strategy @movement_strategy end |
#parent ⇒ Object
handle to parent location and array of children
24 25 26 |
# File 'lib/motel/location.rb', line 24 def parent @parent end |
#parent_id ⇒ Object
id of location and parent, and coordinates relative to that parent
21 22 23 |
# File 'lib/motel/location.rb', line 21 def parent_id @parent_id end |
#proximity_callbacks ⇒ Object
Array of callbacks to be invoked on proximity
33 34 35 |
# File 'lib/motel/location.rb', line 33 def proximity_callbacks @proximity_callbacks end |
#x ⇒ Object
id of location and parent, and coordinates relative to that parent
21 22 23 |
# File 'lib/motel/location.rb', line 21 def x @x end |
#y ⇒ Object
id of location and parent, and coordinates relative to that parent
21 22 23 |
# File 'lib/motel/location.rb', line 21 def y @y end |
#z ⇒ Object
id of location and parent, and coordinates relative to that parent
21 22 23 |
# File 'lib/motel/location.rb', line 21 def z @z end |
Instance Method Details
#-(location) ⇒ Object
return the distance between this location and specified other
110 111 112 113 114 115 |
# File 'lib/motel/location.rb', line 110 def -(location) dx = x - location.x dy = y - location.y dz = z - location.z Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2) end |
#coordinates ⇒ Object
return this locations coordinates in an array
70 71 72 |
# File 'lib/motel/location.rb', line 70 def coordinates [@x, @y, @z] end |
#root ⇒ Object
return this location’s root location
75 76 77 78 |
# File 'lib/motel/location.rb', line 75 def root return self if parent.nil? return parent.root end |
#total_x ⇒ Object
return sum of the x values of this location and all its parents, eg the absolute ‘x’ of the location
90 91 92 93 |
# File 'lib/motel/location.rb', line 90 def total_x return 0 if parent.nil? return parent.total_x + x end |
#total_y ⇒ Object
return sum of the y values of this location and all its parents eg the absolute ‘y’ of the location
97 98 99 100 |
# File 'lib/motel/location.rb', line 97 def total_y return 0 if parent.nil? return parent.total_y + y end |
#total_z ⇒ Object
return sum of the z values of this location and all its parents eg the absolute ‘z’ of the location
104 105 106 107 |
# File 'lib/motel/location.rb', line 104 def total_z return 0 if parent.nil? return parent.total_z + z end |
#traverse_descendants(&bl) ⇒ Object
traverse all chilren recursively, calling block for each
81 82 83 84 85 86 |
# File 'lib/motel/location.rb', line 81 def traverse_descendants(&bl) children.each { |child| bl.call child child.traverse_descendants &bl } end |
#update(location) ⇒ Object
update this location’s attributes to match other’s set attributes
60 61 62 63 64 65 66 67 |
# File 'lib/motel/location.rb', line 60 def update(location) @x = location.x unless location.x.nil? @y = location.y unless location.y.nil? @z = location.z unless location.z.nil? @movement_strategy = location.movement_strategy unless location.movement_strategy.nil? @parent = location.parent unless location.parent.nil? @parent_id = location.parent_id unless location.parent_id.nil? end |