Class: AGL::Ramp

Inherits:
Object
  • Object
show all
Defined in:
lib/minigl/movement.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, y, w, h, left) ⇒ Ramp

Returns a new instance of Ramp.



18
19
20
21
22
23
24
# File 'lib/minigl/movement.rb', line 18

def initialize x, y, w, h, left
  @x = x
  @y = y
  @w = w
  @h = h
  @left = left
end

Instance Method Details

#can_collide?(obj) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/minigl/movement.rb', line 26

def can_collide? obj
  @can_collide = (obj.speed.y >= 0 and not intersects(obj))
end

#check_intersection(obj) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/minigl/movement.rb', line 30

def check_intersection obj
  if @can_collide and intersects obj
    obj.y = get_y obj
    obj.speed.y = 0
#       a = @w / @h
#       x = get_x(obj)
#       y = get_y(obj)
#       w = obj.x - x
#       h = obj.y - y
#       dx = w * h / (w * a + h)
#       dy = dx * a
#       
#       obj.x -= dx
#       obj.y -= dy
#       obj.speed.x *= (@w / (@w + @h))
#       obj.speed.y = 0
  end
end

#contact?(obj) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/minigl/movement.rb', line 49

def contact? obj
  obj.x.round(6) == get_x(obj).round(6) && obj.y.round(6) == get_y(obj).round(6)
end

#get_x(obj) ⇒ Object



57
58
59
60
# File 'lib/minigl/movement.rb', line 57

def get_x obj
  return @x + (1.0 * (@y + @h - obj.y - obj.h) * @w / @h) - obj.w if @left
  @x + (1.0 * (obj.y + obj.h - @y) * @w / @h)
end

#get_y(obj) ⇒ Object



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

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

#intersects(obj) ⇒ Object



53
54
55
# File 'lib/minigl/movement.rb', line 53

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