diff options
Diffstat (limited to 'lib/ruby2d/camera/wrappers/square_wrapper.rb')
| -rw-r--r-- | lib/ruby2d/camera/wrappers/square_wrapper.rb | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/lib/ruby2d/camera/wrappers/square_wrapper.rb b/lib/ruby2d/camera/wrappers/square_wrapper.rb new file mode 100644 index 0000000..2215a30 --- /dev/null +++ b/lib/ruby2d/camera/wrappers/square_wrapper.rb @@ -0,0 +1,146 @@ +# frozen_string_literal: true + +module Camera + # Wraps existing variables as well as adding new methods + # so that it can be handled by the Camera Module + module SquareWrapped + # Squares are part of the exception where + # their x and y variables need to be reset + # when wrapped + def self.extended(obj) + obj.instance_exec do + @x = 0 + @y = 0 + end + end + # Recalculates real coordiantes + # Use after changing variables + def redraw + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + @x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width + @y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height + end + + #Methods for moving the shape + def x + @x ||= 0 + end + + def x=(x) + @x = x + end + + def y + @y ||= 0 + end + + def y=(y) + @y = y + end + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x1 + @virtual_x1 ||= @x1 + end + + def x1=(x1) + @virtual_x1 = x1 + end + + def y1 + @virtual_y1 ||= @y1 + end + + def y1=(y1) + @virtual_y1 = y1 + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + end + + def x4 + @virtual_x4 ||= @x4 + end + + def x4=(x4) + @virtual_x4 = x4 + end + + def y4 + @virtual_y4 ||= @y4 + end + + def y4=(y4) + @virtual_y4 = y4 + end + + + def width + @width ||= 200 + end + + def width=(width) + self.x2 = width + self.x3 = width + @width = width + end + + def height + @height ||= 100 + end + + def height=(height) + self.y3 = height + self.y4 = height + @height = height + end + + def size + @size ||= 100 + end + + def size=(size) + @size = self.width = self.height = size + end + end +end + |
