Class: Zyps::Enclosure
- Inherits:
-
EnvironmentalFactor
- Object
- EnvironmentalFactor
- Zyps::Enclosure
- Defined in:
- lib/zyps/environmental_factors.rb
Overview
Keeps all objects within a set of walls.
Instance Attribute Summary collapse
-
#bottom ⇒ Object
Y coordinate of bottom boundary.
-
#left ⇒ Object
X coordinate of left boundary.
-
#right ⇒ Object
X coordinate of right boundary.
-
#top ⇒ Object
Y coordinate of top boundary.
Instance Method Summary collapse
-
#act(environment) ⇒ Object
If object is beyond a boundary, set its position equal to the boundary and reflect it.
-
#initialize(options = {}) ⇒ Enclosure
constructor
Takes a hash with these keys and defaults: :left => 0 :top => 0 :right => 0 :bottom => 0.
Constructor Details
#initialize(options = {}) ⇒ Enclosure
Takes a hash with these keys and defaults: :left => 0 :top => 0 :right => 0 :bottom => 0
41 42 43 44 45 46 47 48 49 |
# File 'lib/zyps/environmental_factors.rb', line 41 def initialize( = {}) = { :left => 0, :top => 0, :right => 0, :bottom => 0 }.merge() self.left, self.top, self.right, self.bottom = [:left], [:top], [:right], [:bottom] end |
Instance Attribute Details
#bottom ⇒ Object
Y coordinate of bottom boundary.
34 35 36 |
# File 'lib/zyps/environmental_factors.rb', line 34 def bottom @bottom end |
#left ⇒ Object
X coordinate of left boundary.
28 29 30 |
# File 'lib/zyps/environmental_factors.rb', line 28 def left @left end |
#right ⇒ Object
X coordinate of right boundary.
32 33 34 |
# File 'lib/zyps/environmental_factors.rb', line 32 def right @right end |
#top ⇒ Object
Y coordinate of top boundary.
30 31 32 |
# File 'lib/zyps/environmental_factors.rb', line 30 def top @top end |
Instance Method Details
#act(environment) ⇒ Object
If object is beyond a boundary, set its position equal to the boundary and reflect it.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zyps/environmental_factors.rb', line 52 def act(environment) environment.objects.each do |object| if (object.location.x < @left) then object.location.x = @left object.vector.pitch = Utility.find_reflection_angle(90, object.vector.pitch) elsif (object.location.x > @right) then object.location.x = @right object.vector.pitch = Utility.find_reflection_angle(270, object.vector.pitch) end if (object.location.y > @top) then object.location.y = @top object.vector.pitch = Utility.find_reflection_angle(0, object.vector.pitch) elsif (object.location.y < @bottom) then object.location.y = @bottom object.vector.pitch = Utility.find_reflection_angle(180, object.vector.pitch) end end end |