summaryrefslogtreecommitdiffhomepage
path: root/quad_camera_tracker.rb
blob: 25c6071952151283aa80ec38b22a9dd7f184be47 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# frozen_string_literal: true

module QuadCameraTracker
  def update(zoom: 1, x: 0, y: 0)
    if x != 0 or y != 0
      @x1 -= x
      @x2 -= x
      @x3 -= x
      @x4 -= x
      @y1 -= y
      @y2 -= y
      @y3 -= y
      @y4 -= y
    end
    if zoom != 1
      @x1 *= zoom
      @x2 *= zoom
      @x3 *= zoom
      @x4 *= zoom
      @y1 *= zoom
      @y2 *= zoom
      @y3 *= zoom
      @y4 *= zoom
    end
  end

  def true_center
    [[@x1, @x2, @x3, @x4].min, [@y1, @y2, @y3, @y4].min]
  end

  def width
    [@x1, @x2, @x3, @x4].max - [@x1, @x2, @x3, @x4].min
  end

  def height
    [@y1, @y2, @y3, @y4].max - [@y1, @y2, @y3, @y4].min
  end

  def x
    @x ||= @x1 / Camera.zoom_level + Camera.camera_position[0]
  end

  def x=(x)
    diff = x - self.x
    self.x1 += diff
    self.x2 += diff
    self.x3 += diff
    self.x4 += diff
    @x = x
  end

  def y
    @y ||= @y1 / Camera.zoom_level + Camera.camera_position[1]
  end

  def y=(y)
    diff = y - self.y
    self.y1 += diff
    self.y2 += diff
    self.y3 += diff
    self.y4 += diff
    @y = y
  end

  def size
    @size ||= 1
  end

  def size=(size)
    # should resize based on the top left point
    # offset rotation to shape
  end

  def x1
    @x1 / Camera.zoom_level + Camera.camera_position[0] - x
    # undo rotation/translation/zoom
  end

  # Should modify the x1 methods so they move everything else isntead
  # this is so that x1 is always the "origin" aka 0,0 of the shape
  def x1=(x1)
    @x1 = ((x1 + x) - Camera.camera_position[0]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def y1
    @y1 / Camera.zoom_level + Camera.camera_position[1] - y
    # undo rotation/translation/zoom
  end

  def y1=(y1)
    @y1 = ((y1 + y) - Camera.camera_position[1]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def x2
    (@x2) / Camera.zoom_level + Camera.camera_position[0] - x
    # undo rotation/translation/zoom
  end

  def x2=(x2)
    @x2 = ((x2 + x) - Camera.camera_position[0]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def y2
    (@y2) / Camera.zoom_level + Camera.camera_position[1] - y
    # undo rotation/translation/zoom
  end

  def y2=(y2)
    @y2 = ((y2 + y) - Camera.camera_position[1]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def x3
    (@x3) / Camera.zoom_level + Camera.camera_position[0] - x
    # undo rotation/translation/zoom
  end

  def x3=(x3)
    @x3 = ((x3 + x) - Camera.camera_position[0]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def y3
    (@y3) / Camera.zoom_level + Camera.camera_position[1] - y
    # undo rotation/translation/zoom
  end

  def y3=(y3)
    @y3 = ((y3 + y) - Camera.camera_position[1]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def x4
    @x4 / Camera.zoom_level + Camera.camera_position[0] - x
    # undo rotation/translation/zoom
  end

  def x4=(x4)
    @x4 = ((x4 + x) - Camera.camera_position[0]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  def y4
    @y4 / Camera.zoom_level + Camera.camera_position[1] - y
    # undo rotation/translation/zoom
  end

  def y4=(y4)
    @y4 = ((y4 + y) - Camera.camera_position[1]) * Camera.zoom_level
    # add rotation level
    # apply rotation/translation/zoom then pass to super
  end

  attr_writer :rotation_degrees

  def rotation_degrees
    @rotation_degrees ||= 0
  end

  def rotate(x_pivot, y_pivot, angle)
    self.rotation_degrees += angle
    self.rotation_degrees %= 360
    @angle = (Math::PI * angle) / 180
    x_shifted1 = self.x1 - x_pivot
    y_shifted1 = self.y1 - y_pivot
    x_shifted2 = self.x2 - x_pivot
    y_shifted2 = self.y2 - y_pivot
    x_shifted3 = self.x3 - x_pivot
    y_shifted3 = self.y3 - y_pivot
    x_shifted4 = self.x4 - x_pivot
    y_shifted4 = self.y4 - y_pivot

    # Used to update x and y later in the code
    x1_old = self.x1
    y1_old = self.y1

    self.x1 = x_pivot + (x_shifted1 * Math.cos(@angle) - y_shifted1 * Math.sin(@angle))
    self.y1 = y_pivot + (x_shifted1 * Math.sin(@angle) + y_shifted1 * Math.cos(@angle))

    self.x2 = x_pivot + (x_shifted2 * Math.cos(@angle) - y_shifted2 * Math.sin(@angle))
    self.y2 = y_pivot + (x_shifted2 * Math.sin(@angle) + y_shifted2 * Math.cos(@angle))

    self.x3 = x_pivot + (x_shifted3 * Math.cos(@angle) - y_shifted3 * Math.sin(@angle))
    self.y3 = y_pivot + (x_shifted3 * Math.sin(@angle) + y_shifted3 * Math.cos(@angle))

    self.x4 = x_pivot + (x_shifted4 * Math.cos(@angle) - y_shifted4 * Math.sin(@angle))
    self.y4 = y_pivot + (x_shifted4 * Math.sin(@angle) + y_shifted4 * Math.cos(@angle))

    # Updates x and y to be on the origin correctly
    @x += -x1_old + self.x1
    @y += -y1_old + self.y1
  end
end