Class: GosuLighting::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/gosu_lighting/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, x, y, radius, att_sprite = nil) ⇒ Source

Returns a new instance of Source.



17
18
19
20
21
22
23
# File 'lib/gosu_lighting/source.rb', line 17

def initialize window, x, y, radius, att_sprite = nil
  @att_sprite = att_sprite || Gosu::Image.new(window, 'light.png', true)
  @window = window
  @x = x
  @y = y
  @radius = radius
end

Instance Attribute Details

#radiusObject

Returns the value of attribute radius.



15
16
17
# File 'lib/gosu_lighting/source.rb', line 15

def radius
  @radius
end

#xObject

Returns the value of attribute x.



15
16
17
# File 'lib/gosu_lighting/source.rb', line 15

def x
  @x
end

#yObject

Returns the value of attribute y.



15
16
17
# File 'lib/gosu_lighting/source.rb', line 15

def y
  @y
end

Instance Method Details

#drawObject



89
90
91
92
93
# File 'lib/gosu_lighting/source.rb', line 89

def draw
  @window.clip_to(*clip_rect) do
    yield self
  end
end

#draw_attenuation(depth) ⇒ Object



85
86
87
# File 'lib/gosu_lighting/source.rb', line 85

def draw_attenuation depth
  draw_as_rect @att_sprite, *clip_rect, depth, 0xff999999, :multiply
end

#shadow_circle(circle, depth = 1) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gosu_lighting/source.rb', line 25

def shadow_circle circle, depth = 1
  dist = Gosu::distance @x, @y, circle.x, circle.y
  depth = depth + 1.0 - dist / SHADOW_LENGTH

  bx1, by1, bx2, by2 = endpoints_facing circle.x, circle.y, @x, @y, circle.radius

  nx1, ny1 = normal @x, @y, bx1, by1
  nx2, ny2 = normal @x, @y, bx2, by2

  sx1 = bx1 + nx1 * SHADOW_LENGTH
  sy1 = by1 + ny1 * SHADOW_LENGTH
  sx2 = bx2 + nx2 * SHADOW_LENGTH
  sy2 = by2 + ny2 * SHADOW_LENGTH

  @window.gl depth do
    gl_draw_shadow bx1, by1, sx1, sy1, sx2, sy2, bx2, by2, 0.5
  end

  return depth
end

#shadow_rectangle(rect, depth = 2) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gosu_lighting/source.rb', line 46

def shadow_rectangle rect, depth = 2
  dist = Gosu::distance @x, @y, rect.center_x, rect.center_y
  depth = depth + 1.0 - dist / SHADOW_LENGTH

  cx1 = cx2 = rect.x
  cy1 = cy2 = rect.y
  if @x < rect.x
    cy1 += rect.height
  elsif @x < rect.x + rect.width
    cy1 += rect.height if @y > rect.center_y
    cy2 = cy1
  else
    cy2 += rect.height
  end

  if @y < rect.y
    cx2 += rect.width
  elsif @y < rect.y + rect.height
    cx1 += rect.width if @x > rect.center_x
    cx2 = cx1
  else
    cx1 += rect.width
  end

  nx1, ny1 = normal @x, @y, cx1, cy1
  nx2, ny2 = normal @x, @y, cx2, cy2

  sx1 = cx1 + nx1 * SHADOW_LENGTH
  sy1 = cy1 + ny1 * SHADOW_LENGTH
  sx2 = cx2 + nx2 * SHADOW_LENGTH
  sy2 = cy2 + ny2 * SHADOW_LENGTH

  @window.gl depth do
    gl_draw_shadow cx1, cy1, sx1, sy1, sx2, sy2, cx2, cy2, 1.0
  end

  return depth
end