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.
-
#inverted ⇒ Object
readonly
Whether the ramp is vertically inverted, i.e., the face that is parallel to the x-axis faces up and the sloped face faces down.
-
#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
xattribute.
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, inverted = false) ⇒ 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, inverted = false) ⇒ 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
falsefor a ramp that goes down from left to right. - inverted
-
Whether the ramp is vertically inverted, i.e., the face that is parallel to the x-axis faces up and the sloped face faces down.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/minigl/movement.rb', line 95 def initialize(x, y, w, h, left, inverted = false) @x = x @y = y @w = w @h = h @left = left @inverted = inverted @ratio = @h.to_f / @w @factor = @w / Math.sqrt(@w**2 + @h**2) end |
Instance Attribute Details
#factor ⇒ Object (readonly)
:nodoc:
74 75 76 |
# File 'lib/minigl/movement.rb', line 74 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 |
#inverted ⇒ Object (readonly)
Whether the ramp is vertically inverted, i.e., the face that is parallel to the x-axis faces up and the sloped face faces down.
71 72 73 |
# File 'lib/minigl/movement.rb', line 71 def inverted @inverted 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:
73 74 75 |
# File 'lib/minigl/movement.rb', line 73 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:
128 129 130 131 |
# File 'lib/minigl/movement.rb', line 128 def check_can_collide(m) y = get_y(m) + (@inverted ? 0 : 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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/minigl/movement.rb', line 133 def check_intersection(obj) return unless @can_collide && intersect?(obj) counter = @left && obj.prev_speed.x > 0 || !@left && obj.prev_speed.x < 0 if counter && (@inverted ? obj.prev_speed.y < 0 : obj.prev_speed.y > 0) 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 if counter && obj.bottom != self obj.speed.x *= @factor end obj.speed.y = 0 obj.y = get_y(obj) 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,wandhaccessible attributes determining its bounding box.
111 112 113 114 |
# File 'lib/minigl/movement.rb', line 111 def contact?(obj) return false if @inverted 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
151 152 153 154 155 |
# File 'lib/minigl/movement.rb', line 151 def get_x(obj) return obj.x if @left && obj.x + obj.w > @x + @w || !@left && obj.x < @x offset = (@inverted ? obj.y - @y : @y + @h - obj.y - obj.h).to_f * @w / @h @left ? @x + offset - obj.w : @x + @w - offset end |
#get_y(obj) ⇒ Object
157 158 159 160 161 |
# File 'lib/minigl/movement.rb', line 157 def get_y(obj) return @y + (@inverted ? @h : -obj.h) if @left && obj.x + obj.w > @x + @w || !@left && obj.x < @x offset = (@left ? @x + @w - obj.x - obj.w : obj.x - @x).to_f * @h / @w @inverted ? @y + @h - offset : @y + offset - 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,wandhaccessible attributes determining its bounding box.
122 123 124 125 |
# File 'lib/minigl/movement.rb', line 122 def intersect?(obj) obj.x + obj.w > @x && obj.x < @x + @w && (@inverted ? obj.y < get_y(obj) && obj.y + obj.h > @y : obj.y > get_y(obj) && obj.y < @y + @h) end |