Class: Zyps::WrapAround

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

Overview

When an object crosses its boundary, warps object to opposite boundary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WrapAround

Takes a hash with these keys and defaults: :left => 0 :top => 0 :right => 0 :bottom => 0



91
92
93
94
95
96
97
98
99
# File 'lib/zyps/environmental_factors.rb', line 91

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.



84
85
86
# File 'lib/zyps/environmental_factors.rb', line 84

def bottom
  @bottom
end

#leftObject

X coordinate of left boundary.



78
79
80
# File 'lib/zyps/environmental_factors.rb', line 78

def left
  @left
end

#rightObject

X coordinate of right boundary.



82
83
84
# File 'lib/zyps/environmental_factors.rb', line 82

def right
  @right
end

#topObject

Y coordinate of top boundary.



80
81
82
# File 'lib/zyps/environmental_factors.rb', line 80

def top
  @top
end

Instance Method Details

#act(environment) ⇒ Object

If object is beyond a boundary, set its position to that of opposite boundary.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/zyps/environmental_factors.rb', line 102

def act(environment)
	environment.objects.each do |object|
		if (object.location.x < @left) then
			object.location.x = @right
		elsif (object.location.x > @right) then
			object.location.x = @left
		end
		if (object.location.y > @top) then
			object.location.y = @bottom
		elsif (object.location.y < @bottom) then
			object.location.y = @top
		end
	end
end