diff options
| author | realtradam <[email protected]> | 2021-01-25 00:09:51 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-01-25 00:09:51 -0500 |
| commit | c1476cb451e93a54df8521ad5572732539f345dd (patch) | |
| tree | 2379fb6d3c1d5bd6f2cd667421f8f2604f6c520c /camera.rb | |
| parent | b3c67e70aa97c6ad6176917427b5a192b407dbd6 (diff) | |
| download | ruby2d-camera-old-c1476cb451e93a54df8521ad5572732539f345dd.tar.gz ruby2d-camera-old-c1476cb451e93a54df8521ad5572732539f345dd.zip | |
finished implementing quads
Diffstat (limited to 'camera.rb')
| -rw-r--r-- | camera.rb | 178 |
1 files changed, 21 insertions, 157 deletions
@@ -1,158 +1,14 @@ # frozen_string_literal: true +require 'ruby2d' +require_relative 'quad_camera_tracker' + # Simulates a moving camera by manipulating objects it knows class Camera # Is added to quads to manage how they show and modify # it should convert or undo the camera modification # such that it is as if the camera wasnt there and everything # is relative to the origin - module QuadCameraTracker - def update(x, y) - @x1 -= x - @x2 -= x - @x3 -= x - @x4 -= x - @y1 -= y - @y2 -= y - @y3 -= y - @y4 -= y - end - - def x - @x ||= 0 - end - - def x=(x) - diff = @x - x - self.x1 += diff - self.x2 += diff - self.x3 += diff - self.x4 += diff - @x = x - end - - def y - @y ||= 0 - end - - def y=(y) - diff = @y - 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 + x) / Camera.zoom_level + Camera.camera_position[0] - # undo rotation/translation/zoom - end - - 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 + y) / Camera.zoom_level + Camera.camera_position[1] - # 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 + x) / Camera.zoom_level + Camera.camera_position[0] - # 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 + y) / Camera.zoom_level + Camera.camera_position[1] - # 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 + x) / Camera.zoom_level + Camera.camera_position[0] - # undo rotation/translation/zoom - end - - def x3=(x3) - @x1 = ((x3 + x) - Camera.camera_position[0]) * Camera.zoom_level - # add rotation level - # apply rotation/translation/zoom then pass to super - end - - def y3 - (@y3 + y) / Camera.zoom_level + Camera.camera_position[1] - # 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 + x) / Camera.zoom_level + Camera.camera_position[0] - # 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 + y) / Camera.zoom_level + Camera.camera_position[1] - # 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 - - def rotation_degrees - @rotation_degrees ||= 0 - end - - def rotate_degrees_by(degrees) - # offset rotation to shape - end - - def rotate_degree_to(degrees) - # set rotation - end - end class <<self attr_writer :elasticity @@ -195,9 +51,13 @@ class Camera def self.zoom_by(zoom) objects.each do |object| - object.size *= zoom - object.x *= zoom - object.y *= zoom + if object.is_a?(Image) or object.is_a?(AnimatedSquare) + object.size *= zoom + object.x *= zoom + object.y *= zoom + else + object.update(zoom: zoom) + end end self.zoom_level *= zoom move_by(Window.width / 2 * (zoom - 1), @@ -209,8 +69,8 @@ class Camera end def self.follow(item) - move_to(((item.x + item.size / 2) - (Window.width / 2)) / elasticity, - ((item.y + item.size / 2) - (Window.height / 2)) / elasticity) + move_to(((item.true_center[0] + item.width / 2) - (Window.width / 2)) / elasticity, + ((item.true_center[1] + item.height / 2) - (Window.height / 2)) / elasticity) end def self.objects @@ -236,7 +96,7 @@ class Camera object.x -= x object.y -= y else - object.update(x,y) + object.update(x: x, y: y) end end self.camera_position_x += x / zoom_level @@ -244,11 +104,15 @@ class Camera end def self.move_to(x, y) - self.camera_position = [x / zoom_level + camera_position[0], - y / zoom_level + camera_position[1]] + self.camera_position_x = x / zoom_level + camera_position[0] + self.camera_position_y = y / zoom_level + camera_position[1] objects.each do |object| - object.x -= x - object.y -= y + if object.is_a?(Image) or object.is_a?(AnimatedSquare) + object.x -= x + object.y -= y + else + object.update(x: x, y: y) + end end end end |
