Class: CyberarmEngine::Background

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/background.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x: 0, y: 0, z: 0, width: 0, height: 0, background: Gosu::Color::BLACK, angle: 0, debug: false) ⇒ Background

Returns a new instance of Background.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cyberarm_engine/background.rb', line 6

def initialize(x: 0, y: 0, z: 0, width: 0, height: 0, background: Gosu::Color::BLACK, angle: 0, debug: false)
  @x = x
  @y = y
  @z = z
  @width = width
  @height = height
  @debug = debug

  @paint  = Paint.new(background)
  @angle  = angle

  @top_left     = Vector.new(@x, @y)
  @top_right    = Vector.new(@x + @width, @y)
  @bottom_left  = Vector.new(@x, @y + @height)
  @bottom_right = Vector.new(@x + @width, @y + @height)
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def angle
  @angle
end

#backgroundObject

Returns the value of attribute background.



4
5
6
# File 'lib/cyberarm_engine/background.rb', line 4

def background
  @background
end

#debugObject

Returns the value of attribute debug.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def debug
  @debug
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def height
  @height
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def width
  @width
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def y
  @y
end

#zObject

Returns the value of attribute z.



3
4
5
# File 'lib/cyberarm_engine/background.rb', line 3

def z
  @z
end

Instance Method Details

#debug_outlineObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cyberarm_engine/background.rb', line 81

def debug_outline
  # Top
  Gosu.draw_line(
    @x,          @y,           Gosu::Color::RED,
    @x + @width, @y,           Gosu::Color::RED,
    @z
  )

  # Right
  Gosu.draw_line(
    @x + @width, @y,           Gosu::Color::RED,
    @x + @width, @y + @height, Gosu::Color::RED,
    @z
  )

  # Bottom
  Gosu.draw_line(
    @x + @width, @y + @height, Gosu::Color::RED,
    @x, @y + @height,          Gosu::Color::RED,
    @z
  )

  # Left
  Gosu.draw_line(
    @x, @y + @height,          Gosu::Color::RED,
    @x, @y,                    Gosu::Color::RED,
    @z
  )
end

#drawObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cyberarm_engine/background.rb', line 23

def draw
  Gosu.clip_to(@x, @y, @width, @height) do
    Gosu.draw_quad(
      @top_left.x,     @top_left.y,     @paint.top_left,
      @top_right.x,    @top_right.y,    @paint.top_right,
      @bottom_right.x, @bottom_right.y, @paint.bottom_right,
      @bottom_left.x,  @bottom_left.y,  @paint.bottom_left,
      @z
    )
  end

  debug_outline if @debug
end

#shortest_distance(point, la, lb) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/cyberarm_engine/background.rb', line 70

def shortest_distance(point, la, lb)
  a = la.x - lb.x
  b = la.y - lb.y
  c = Gosu.distance(la.x, la.y, lb.x, lb.y)
  p a, b, c
  d = (a * point.x + b * point.y + c).abs / Math.sqrt(a * a + b * b)
  puts "Distance: #{d}"
  exit!
  d
end

#updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cyberarm_engine/background.rb', line 37

def update
  origin_x = (@x + (@width / 2))
  origin_y = (@y + (@height / 2))

  points = [
    @top_left     = Vector.new(@x, @y),
    @top_right    = Vector.new(@x + @width, @y),
    @bottom_left  = Vector.new(@x, @y + @height),
    @bottom_right = Vector.new(@x + @width, @y + @height)
  ]

  [@top_left, @top_right, @bottom_left, @bottom_right].each do |vector|
    temp_x = vector.x - origin_x
    temp_y = vector.y - origin_y

    # 90 is up here, while gosu uses 0 for up.
    radians = (@angle + 90).gosu_to_radians
    vector.x = (@x + (@width / 2))  + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
    vector.y = (@y + (@height / 2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
  end

  # [
  #   [:top,    @top_left, @top_right],
  #   [:right,  @top_right, @bottom_right],
  #   [:bottom, @bottom_right, @bottom_left],
  #   [:left,   @bottom_left, @top_left]
  # ].each do |edge|
  #   points.each do |point|
  #     puts "#{edge.first} -> #{shortest_distance(point, edge[1], edge[2])}"
  #   end
  # end
end