Class: Motel::MovementStrategies::Linear
- Inherits:
-
Motel::MovementStrategy
- Object
- Motel::MovementStrategy
- Motel::MovementStrategies::Linear
- Defined in:
- lib/motel/movement_strategies/linear.rb
Overview
The Linear MovementStrategy moves a location in a linear manner as defined by a unit direction vector and a floating point speed
Instance Attribute Summary collapse
-
#direction_vector_x ⇒ Object
Returns the value of attribute direction_vector_x.
-
#direction_vector_y ⇒ Object
Returns the value of attribute direction_vector_y.
-
#direction_vector_z ⇒ Object
Returns the value of attribute direction_vector_z.
-
#speed ⇒ Object
Returns the value of attribute speed.
Attributes inherited from Motel::MovementStrategy
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Linear
constructor
A new instance of Linear.
-
#move(location, elapsed_seconds) ⇒ Object
Motel::Models::MovementStrategy::move.
Constructor Details
#initialize(args = {}) ⇒ Linear
Returns a new instance of Linear.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/motel/movement_strategies/linear.rb', line 21 def initialize(args = {}) @direction_vector_x = args[:direction_vector_x] if args.has_key? :direction_vector_x @direction_vector_y = args[:direction_vector_y] if args.has_key? :direction_vector_y @direction_vector_z = args[:direction_vector_z] if args.has_key? :direction_vector_z @speed = args[:speed] if args.has_key? :speed super(args) # normalize direction vector @direction_vector_x, @direction_vector_y, @direction_vector_z = normalize(@direction_vector_x, @direction_vector_y, @direction_vector_z) end |
Instance Attribute Details
#direction_vector_x ⇒ Object
Returns the value of attribute direction_vector_x.
17 18 19 |
# File 'lib/motel/movement_strategies/linear.rb', line 17 def direction_vector_x @direction_vector_x end |
#direction_vector_y ⇒ Object
Returns the value of attribute direction_vector_y.
17 18 19 |
# File 'lib/motel/movement_strategies/linear.rb', line 17 def direction_vector_y @direction_vector_y end |
#direction_vector_z ⇒ Object
Returns the value of attribute direction_vector_z.
17 18 19 |
# File 'lib/motel/movement_strategies/linear.rb', line 17 def direction_vector_z @direction_vector_z end |
#speed ⇒ Object
Returns the value of attribute speed.
19 20 21 |
# File 'lib/motel/movement_strategies/linear.rb', line 19 def speed @speed end |
Instance Method Details
#move(location, elapsed_seconds) ⇒ Object
Motel::Models::MovementStrategy::move
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/motel/movement_strategies/linear.rb', line 35 def move(location, elapsed_seconds) #unless valid? # Logger.warn "linear movement strategy not valid, not proceeding with move" # return #end Logger.debug "moving location #{location.id} via linear movement strategy " + "#{speed} #{direction_vector_x}/#{direction_vector_y}/#{direction_vector_z}" # calculate distance and update x,y,z accordingly distance = speed * elapsed_seconds location.x += distance * direction_vector_x location.y += distance * direction_vector_y location.z += distance * direction_vector_z end |