Class: MiniGL::Ramp
- Inherits:
-
Object
- Object
- MiniGL::Ramp
- Defined in:
- lib/minigl/movement.rb
Overview
Represents a ramp, i.e., an inclined structure which allows walking over it while automatically going up or down. It can be imagined as a right triangle, with a side parallel to the x axis and another one parallel to the y axis. You must provide instances of this class (or derived classes) to the ramps
array parameter of the move
method.
Instance Attribute Summary collapse
-
#factor ⇒ Object
readonly
:nodoc:.
-
#h ⇒ Object
readonly
The height of the ramp.
-
#left ⇒ Object
readonly
Whether the height of the ramp increases from left to right (decreases from left to right when
false
). -
#ratio ⇒ Object
readonly
:nodoc:.
-
#w ⇒ Object
readonly
The width of the ramp.
-
#x ⇒ Object
readonly
The x-coordinate of the top left corner of a rectangle that completely (and precisely) encloses the ramp (thought of as a right triangle).
-
#y ⇒ Object
readonly
The y-coordinate of the top left corner of the rectangle described in the
x
attribute.
Instance Method Summary collapse
-
#check_can_collide(m) ⇒ Object
:nodoc:.
- #check_intersection(obj) ⇒ Object
-
#contact?(obj) ⇒ Boolean
Checks if an object is in contact with this ramp (standing over it).
- #get_x(obj) ⇒ Object
- #get_y(obj) ⇒ Object
-
#initialize(x, y, w, h, left) ⇒ Ramp
constructor
Creates a new ramp.
-
#intersect?(obj) ⇒ Boolean
Checks if an object is intersecting this ramp (inside the corresponding right triangle and at the floor level or above).
Constructor Details
#initialize(x, y, w, h, left) ⇒ Ramp
Creates a new ramp.
Parameters:
- x
-
The x-coordinate of the top left corner of a rectangle that completely (and precisely) encloses the ramp (thought of as a right triangle).
- y
-
The y-coordinate of the top left corner of the rectangle described above.
- w
-
The width of the ramp (which corresponds to the width of the rectangle described above).
- h
-
The height of the ramp (which corresponds to the height of the rectangle described above, and to the difference between the lowest point of the ramp, where it usually meets the floor, and the highest).
- left
-
Whether the height of the ramp increases from left to right. Use
false
for a ramp that goes down from left to right.
88 89 90 91 92 93 94 95 96 |
# File 'lib/minigl/movement.rb', line 88 def initialize(x, y, w, h, left) @x = x @y = y @w = w @h = h @left = left @ratio = @h.to_f / @w @factor = @w / Math.sqrt(@w**2 + @h**2) end |
Instance Attribute Details
#factor ⇒ Object (readonly)
:nodoc:
70 71 72 |
# File 'lib/minigl/movement.rb', line 70 def factor @factor end |
#h ⇒ Object (readonly)
The height of the ramp.
63 64 65 |
# File 'lib/minigl/movement.rb', line 63 def h @h end |
#left ⇒ Object (readonly)
Whether the height of the ramp increases from left to right (decreases from left to right when false
).
67 68 69 |
# File 'lib/minigl/movement.rb', line 67 def left @left end |
#ratio ⇒ Object (readonly)
:nodoc:
69 70 71 |
# File 'lib/minigl/movement.rb', line 69 def ratio @ratio end |
#w ⇒ Object (readonly)
The width of the ramp.
60 61 62 |
# File 'lib/minigl/movement.rb', line 60 def w @w end |
#x ⇒ Object (readonly)
The x-coordinate of the top left corner of a rectangle that completely (and precisely) encloses the ramp (thought of as a right triangle).
53 54 55 |
# File 'lib/minigl/movement.rb', line 53 def x @x end |
#y ⇒ Object (readonly)
The y-coordinate of the top left corner of the rectangle described in the x
attribute.
57 58 59 |
# File 'lib/minigl/movement.rb', line 57 def y @y end |
Instance Method Details
#check_can_collide(m) ⇒ Object
:nodoc:
118 119 120 121 |
# File 'lib/minigl/movement.rb', line 118 def check_can_collide(m) y = get_y(m) + m.h @can_collide = m.x + m.w > @x && @x + @w > m.x && m.y < y && m.y + m.h > y end |
#check_intersection(obj) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/minigl/movement.rb', line 123 def check_intersection(obj) if @can_collide and intersect? obj counter = @left && obj.prev_speed.x > 0 || !@left && obj.prev_speed.x < 0 if obj.prev_speed.y > 0 && counter dx = get_x(obj) - obj.x s = (obj.prev_speed.y.to_f / obj.prev_speed.x).abs dx /= s + @ratio obj.x += dx end obj.y = get_y obj if counter && obj.bottom != self obj.speed.x *= @factor end obj.speed.y = 0 end end |
#contact?(obj) ⇒ Boolean
Checks if an object is in contact with this ramp (standing over it).
Parameters:
- obj
-
The object to check contact with. It must have the
x
,y
,w
andh
accessible attributes determining its bounding box.
103 104 105 |
# File 'lib/minigl/movement.rb', line 103 def contact?(obj) obj.x + obj.w > @x && obj.x < @x + @w && obj.x.round(6) == get_x(obj).round(6) && obj.y.round(6) == get_y(obj).round(6) end |
#get_x(obj) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/minigl/movement.rb', line 140 def get_x(obj) return obj.x if @left && obj.x + obj.w > @x + @w return @x + (1.0 * (@y + @h - obj.y - obj.h) * @w / @h) - obj.w if @left return obj.x if obj.x < @x @x + (1.0 * (obj.y + obj.h - @y) * @w / @h) end |
#get_y(obj) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/minigl/movement.rb', line 147 def get_y(obj) return @y - obj.h if @left && obj.x + obj.w > @x + @w return @y + (1.0 * (@x + @w - obj.x - obj.w) * @h / @w) - obj.h if @left return @y - obj.h if obj.x < @x @y + (1.0 * (obj.x - @x) * @h / @w) - obj.h end |
#intersect?(obj) ⇒ Boolean
Checks if an object is intersecting this ramp (inside the corresponding right triangle and at the floor level or above).
Parameters:
- obj
-
The object to check intersection with. It must have the
x
,y
,w
andh
accessible attributes determining its bounding box.
113 114 115 |
# File 'lib/minigl/movement.rb', line 113 def intersect?(obj) obj.x + obj.w > @x && obj.x < @x + @w && obj.y > get_y(obj) && obj.y <= @y + @h - obj.h end |