Class: MiniGL::Ramp

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



87
88
89
90
91
92
93
94
95
# File 'lib/minigl/movement.rb', line 87

def initialize(x, y, w, h, left)
  @x = x
  @y = y
  @w = w
  @h = h
  @left = left
  @ratio = @h.to_f / @w
  @factor = Math.cos(@w / Math.sqrt(@w**2 + @h**2))
end

Instance Attribute Details

#hObject (readonly)

The height of the ramp.



63
64
65
# File 'lib/minigl/movement.rb', line 63

def h
  @h
end

#leftObject (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

#ratioObject (readonly)

:nodoc:



69
70
71
# File 'lib/minigl/movement.rb', line 69

def ratio
  @ratio
end

#wObject (readonly)

The width of the ramp.



60
61
62
# File 'lib/minigl/movement.rb', line 60

def w
  @w
end

#xObject (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

#yObject (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:



117
118
119
120
# File 'lib/minigl/movement.rb', line 117

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/minigl/movement.rb', line 122

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.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 and h accessible attributes determining its bounding box.

Returns:

  • (Boolean)


102
103
104
# File 'lib/minigl/movement.rb', line 102

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



139
140
141
142
143
144
# File 'lib/minigl/movement.rb', line 139

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



146
147
148
149
150
151
# File 'lib/minigl/movement.rb', line 146

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 and h accessible attributes determining its bounding box.

Returns:

  • (Boolean)


112
113
114
# File 'lib/minigl/movement.rb', line 112

def intersect?(obj)
  obj.x + obj.w > @x && obj.x < @x + @w && obj.y > get_y(obj) && obj.y <= @y + @h - obj.h
end