summaryrefslogtreecommitdiffhomepage
path: root/camera.rb
blob: 67a537f0f262bb8e028e22ea3f23631676dc205c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

require_relative 'adapt_triangle'

module Camera

  class <<self
    private
    def objects
      @objects ||= []
    end
  end

  def self.<<(item)
    item.extend AdaptTriangle if item.is_a? Triangle
    objects.push(item) unless objects.include?(item)
  end

  def self.center_x
    x + ((Window.width / 2) * (1/zoom))
  end

  def self.center_x=(center_x)
    @x = center_x - ((Window.width / 2) * (1/zoom))
    objects.each(&:_update)
  end

  def self.center_y
    y + ((Window.height / 2) * (1/zoom))
  end

  def self.center_y=(center_y)
    @y = center_y - ((Window.height / 2) * (1/zoom))
    objects.each(&:_update)
  end
  def self.center(center_x, center_y)
    self.center_y = center_y
    self.center_x = center_y
    #@y = center_y
    #@x = center_x
    angle = Math::PI / 180 * (Camera.angle + (1 * 45))
    offset_angle = Math::PI / 180 * 45
    x = (Math.cos(angle) - Math.cos(offset_angle)) * (Window.width/2) * Math.sqrt(2) * (1 / zoom)
    y = (Math.sin(angle) - Math.sin(offset_angle)) * (Window.height/2) * Math.sqrt(2) * (1 / zoom)
    @x += x
    @y += y
    #@x = x + ((pivot_x - x) * Math.cos(angle)) - ((pivot_y - y) * Math.sin(angle))
    #@y = y + ((pivot_x - x) * Math.sin(angle)) + ((pivot_y - y) * Math.cos(angle))
    objects.each(&:_update)
  end
  def self.x
    @x ||= 0
  end

  def self.x=(x)
    angle = Camera.angle * (Math::PI / 180)
    @y += (x - self.x) * Math.sin(-angle)
    @x += (x - self.x) * Math.cos(-angle)
    objects.each(&:_update)
  end

  def self.y
    @y ||= 0
  end

  def self.y=(y)
    angle = Camera.angle * (Math::PI / 180)
    @x += -(y - self.y) * Math.sin(-angle)
    @y += (y - self.y) * Math.cos(-angle)
    objects.each(&:_update)
  end

  def self.zoom
    @zoom ||= 1.0
  end

  def self.zoom=(zoom)
    if zoom != self.zoom
      temp = [center_x, center_y]
      @zoom = zoom
      self.center_x = temp[0]
      self.center_y = temp[1]
      objects.each(&:_update)
    end
  end

  def self.angle
    @angle ||= 0
  end

  def self.angle=(angle)
    @angle = angle
    objects.each(&:_update)
  end
end