diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/camera/camera.rb | 108 | ||||
| -rw-r--r-- | lib/camera/wrappers/image_wrapper.rb | 73 | ||||
| -rw-r--r-- | lib/camera/wrappers/square_wrapper.rb | 73 | ||||
| -rw-r--r-- | lib/camera/wrappers/triangle_wrapper.rb | 90 |
4 files changed, 344 insertions, 0 deletions
diff --git a/lib/camera/camera.rb b/lib/camera/camera.rb new file mode 100644 index 0000000..e5fd254 --- /dev/null +++ b/lib/camera/camera.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +Dir[File.join(__dir__, 'wrappers', '*.rb')].sort.each { |file| require file } + +# Handles rendering objects relative +# to a camera location +module Camera + class <<self + + private + # Contains all objects that are tracked + def objects + @objects ||= [] + end + end + + def self.debug_x + @debug_x ||= 0 + end + + def self.debug_x=(debug_x) + @debug_x = debug_x + end + + def self.debug_y + @debug_y ||= 0 + end + + def self.debug_y=(debug_y) + @debug_y = debug_y + end + + # Adding objects so they are + # tracked by the Camera + def self.<<(item) + case item + when Triangle + item.extend Camera::TriangleWrapped + puts item.class + when Square + item.extend Camera::SquareWrapped + when Quad + item.extend Camera::QuadWrapped + when Line + item.extend Camera::LineWrapped + when Circle + item.extend Camera::CircleWrapped + when Image + item.extend Camera::ImageWrapped + when Sprite + item.extend Camera::SpriteWrapped + when Text + item.extend Camera::TextWrapped + else + puts 'Warning: Non-standard Object added to Camera' + puts 'Object may not behave as expected' + end + objects.push(item) unless objects.include?(item) + end + + # Redraw all objects that + # are tracked by the Camera + def self.update + objects.each(&:update) + end + + # Variables changing Camera properties + def self._x(x) + @x += x + end + + def self._y(y) + @y += y + end + + def self.x + @x ||= 0 + end + + def self.x=(x) + @x = x + end + + def self.y + @y ||= 0 + end + + def self.y=(y) + @y = y + end + + def self.zoom + @zoom ||= 1.0 + end + + def self.zoom=(zoom) + @zoom = zoom + end + + def self.angle + @angle ||= 0 + end + + def self.angle=(angle) + angle %= 360 + @angle = angle + end +end diff --git a/lib/camera/wrappers/image_wrapper.rb b/lib/camera/wrappers/image_wrapper.rb new file mode 100644 index 0000000..3f603d4 --- /dev/null +++ b/lib/camera/wrappers/image_wrapper.rb @@ -0,0 +1,73 @@ +# 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 ImageWrapped + # Recalculates real coordiantes + # Use after changing variables + def update + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + offset_x = x + (width / 2) + offset_y = y + (height / 2) + @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \ + * Camera.zoom + half_width - (width * Camera.zoom / 2) + @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \ + * Camera.zoom + half_height - (height * Camera.zoom / 2) + @rotate = rotate + Camera.angle + @width = width * Camera.zoom + @height = height * Camera.zoom + end + + #Methods for moving the shape + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def x + @virtual_x ||= @x + end + + def rotate + @virtual_rotate ||= @rotate + end + + def rotate=(rotate) + @virtual_rotate = rotate + end + + def width + @virtual_width ||= @width + end + + def width=(width) + @virtual_width = width + end + + def height + @virtual_height ||= @height + end + + def height=(height) + @virtual_height = height + end + end +end diff --git a/lib/camera/wrappers/square_wrapper.rb b/lib/camera/wrappers/square_wrapper.rb new file mode 100644 index 0000000..6cfd3f7 --- /dev/null +++ b/lib/camera/wrappers/square_wrapper.rb @@ -0,0 +1,73 @@ +# 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 + # Recalculates real coordiantes + # Use after changing variables + def update + angle = Camera.angle * (Math::PI / 180) + half_width = Window.width * 0.5 + half_height = Window.height * 0.5 + offset_x = x + (width / 2) + offset_y = y + (height / 2) + @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \ + * Camera.zoom + half_width - (width * Camera.zoom / 2) + @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \ + * Camera.zoom + half_height - (height * Camera.zoom / 2) + @rotate = rotate + Camera.angle + @width = width * Camera.zoom + @height = height * Camera.zoom + end + + #Methods for moving the shape + + # Wrappers for the coordinates so that + # they are handled by the Camera instead + # and for ease of programming + def x + @virtual_x ||= @x + end + + def x=(x) + @virtual_x = x + end + + def y + @virtual_y ||= @y + end + + def y=(y) + @virtual_y = y + end + + def x + @virtual_x ||= @x + end + + def rotate + @virtual_rotate ||= @rotate + end + + def rotate=(rotate) + @virtual_rotate = rotate + end + + def width + @virtual_width ||= @width + end + + def width=(width) + @virtual_width = width + end + + def height + @virtual_height ||= @height + end + + def height=(height) + @virtual_height = height + end + end +end diff --git a/lib/camera/wrappers/triangle_wrapper.rb b/lib/camera/wrappers/triangle_wrapper.rb new file mode 100644 index 0000000..46d8f35 --- /dev/null +++ b/lib/camera/wrappers/triangle_wrapper.rb @@ -0,0 +1,90 @@ +# 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 TriangleWrapped + # Recalculates real coordiantes + # Use after changing variables + def update + 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 + end + + #Methods for moving the shape + def x + @x ||= x1 + end + + def x=(x) + @x = x + end + + def y + @y ||= y1 + 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 + end +end + |
