Class: Zyps::Enclosure

Inherits:
EnvironmentalFactor
  • Object
show all
Defined in:
lib/zyps/environmental_factors.rb

Overview

Keeps all objects within a set of walls.

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
	options = {
		:left => 0,
		:top => 0,
		:right => 0,
		:bottom => 0
	}.merge(options)
	self.left, self.top, self.right, self.bottom = options[:left], options[:top], options[:right], options[:bottom]
end

Instance Attribute Details

#bottomObject

Y coordinate of bottom boundary.



34
35
36
# File 'lib/zyps/environmental_factors.rb', line 34

def bottom
  @bottom
end

#leftObject

X coordinate of left boundary.



28
29
30
# File 'lib/zyps/environmental_factors.rb', line 28

def left
  @left
end

#rightObject

X coordinate of right boundary.



32
33
34
# File 'lib/zyps/environmental_factors.rb', line 32

def right
  @right
end

#topObject

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