From 69d5a3c712028b0448489c89243618b864dd02ea Mon Sep 17 00:00:00 2001 From: realtradam Date: Mon, 19 Apr 2021 21:58:14 -0400 Subject: . --- README.md | 10 +- adapt_triangle.rb | 133 ++++++++++++++++++++++++++ animator.rb | 80 ---------------- camera.rb | 144 +++++++--------------------- quad_camera_tracker.rb | 248 ------------------------------------------------- run.rb | 157 +++++++++++-------------------- 6 files changed, 224 insertions(+), 548 deletions(-) create mode 100644 adapt_triangle.rb delete mode 100644 animator.rb delete mode 100644 quad_camera_tracker.rb diff --git a/README.md b/README.md index 4b9deba..2069ab1 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,9 @@ Controls: WASD to move character -IJKL to switch camera into manual mode, and to move it around -F to switch camera back into follow mode -Q/E to zoom change the zoom -R to reset the zoom +IJKL to move camera +Q/E to rotate camera +R to reset the rotation --- @@ -18,7 +17,8 @@ I wanted to make a proof of concept to see how feasible it is to implement a cam How it works: -A single `Camera` class exists which keeps track of most objects that exists. Whenever you want the camera to move it simply move the entire game world. +A single `Camera` module exists which keeps track of objects that you add to it. When you add an object to the camera (currently only triangles) it creates a wrapper for the object and modifys it to work with the camera. + If you want to use this little demo in your own projects feel free to do so! All you need is the camera.rb file and then just `require` or `require_relative` it into your project. See the code as an example for how it is used. diff --git a/adapt_triangle.rb b/adapt_triangle.rb new file mode 100644 index 0000000..e091ec1 --- /dev/null +++ b/adapt_triangle.rb @@ -0,0 +1,133 @@ +# frozen_string_literal: true + +module AdaptTriangle + + def _translate_x(x) + @x1 += x + @x2 += x + @x3 += x + end + + def _translate_y(y) + @y1 += y + @y2 += y + @y3 += y + end + + def _rotate(angle) + @x1 = ((@x1) * Math.cos(angle)) - ((@y1) * Math.sin(angle)) + @y1 = ((@x1) * Math.sin(angle)) + ((@y1) * Math.cos(angle)) + @x2 = ((@x2) * Math.cos(angle)) - ((@y2) * Math.sin(angle)) + @y2 = ((@x2) * Math.sin(angle)) + ((@y2) * Math.cos(angle)) + @x3 = ((@x3) * Math.cos(angle)) - ((@y3) * Math.sin(angle)) + @y3 = ((@x3) * Math.sin(angle)) + ((@y3) * Math.cos(angle)) + end + + def _update + angle = Camera.angle * (Math::PI / 180) + @x1 = ((x + x1 + Camera.x) * Math.cos(angle)) - ((y + y1 + Camera.y) * Math.sin(angle)) + @y1 = ((x + x1 + Camera.x) * Math.sin(angle)) + ((y + y1 + Camera.y) * Math.cos(angle)) + @x2 = ((x + x2 + Camera.x) * Math.cos(angle)) - ((y + y2 + Camera.y) * Math.sin(angle)) + @y2 = ((x + x2 + Camera.x) * Math.sin(angle)) + ((y + y2 + Camera.y) * Math.cos(angle)) + @x3 = ((x + x3 + Camera.x) * Math.cos(angle)) - ((y + y3 + Camera.y) * Math.sin(angle)) + @y3 = ((x + x3 + Camera.x) * Math.sin(angle)) + ((y + y3 + Camera.y) * Math.cos(angle)) + end + + def _x1 + @x1 + end + + def _y1 + @y1 + end + + def x + @x ||= x1 + end + + def x=(x) + #diff = x - self.x + #self.x1 += diff + #self.x2 += diff + #self.x3 += diff + @x = x + _update + end + + def y + @y ||= y1 + end + + def y=(y) + #diff = y - self.y + #self.y1 += diff + #self.y2 += diff + #self.y3 += diff + @y = y + end + + #undo rotation + def x1 + @virtual_x1 ||= @x1 + #(@x1 * Math.cos(-Camera.angle)) - (@y1 * Math.sin(-Camera.angle)) - Camera.x + end + + #difference between 'x1' and undone rotation > then rotated + def x1=(x1) + @virtual_x1 = x1 + _update + #temp = self.x1 + #@x1 += ((x1 - temp) * Math.cos(Camera.angle)) + #@y1 += ((x1 - temp) * Math.sin(Camera.angle)) + end + + def y1 + @virtual_y1 ||= @y1 + #(@x1 * Math.sin(-Camera.angle)) + (@y1 * Math.cos(-Camera.angle)) - Camera.y + end + + def y1=(y1) + @virtual_y1 = y1 + _update + #temp = self.y1 + #@x1 += -((y1 - temp) * Math.sin(Camera.angle)) + #@y1 += ((y1 - temp) * Math.cos(Camera.angle)) + end + + def x2 + @virtual_x2 ||= @x2 + end + + def x2=(x2) + @virtual_x2 = x2 + _update + end + + def y2 + @virtual_y2 ||= @y2 + end + + def y2=(y2) + @virtual_y2 = y2 + _update + end + + def x3 + @virtual_x3 ||= @x3 + end + + def x3=(x3) + @virtual_x3 = x3 + _update + end + + def y3 + @virtual_y3 ||= @y3 + end + + def y3=(y3) + @virtual_y3 = y3 + _update + end +end + diff --git a/animator.rb b/animator.rb deleted file mode 100644 index 0d713b1..0000000 --- a/animator.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -# A square but it moves on its own -class AnimatedSquare - def initialize(x: '0', - y: '0', - size: 10, - color: 'random') - @square = Square.new(x: x, - y: y, - size: size, - color: color) - end - - attr_writer :speed - - def speed - @speed ||= (0..5).to_a.sample - end - - def axis - @axis ||= (0..1).to_a.sample - end - - def range - @range ||= if axis.zero? - [((x - (250..500).to_a.sample)..x).to_a.sample, - (x..(x + (250..500).to_a.sample)).to_a.sample] - else - [((y - (250..500).to_a.sample)..y).to_a.sample, - (y..(y + (250..500).to_a.sample)).to_a.sample] - end - end - - def square - @square = Square.new - end - - def x - @square.x - end - - def x=(x) - @square.x = x - end - - def y - @square.y - end - - def y=(y) - @square.y = y - end - - def size - @square.size - end - - def size=(size) - @square.size = size - end - - def update(offset, zoom) - if axis.zero? - @square.x += speed * zoom - if @square.x >= ((range[1] - offset[0])) * zoom - self.speed = -speed.abs - elsif @square.x <= ((range[0] - offset[0])) * zoom - self.speed = speed.abs - end - else - @square.y += speed * zoom - if @square.y >= ((range[1] - offset[1])) * zoom - self.speed = -speed.abs - elsif @square.y <= ((range[0] - offset[1])) * zoom - self.speed = speed.abs - end - end - end -end diff --git a/camera.rb b/camera.rb index 8623e34..8748ce5 100644 --- a/camera.rb +++ b/camera.rb @@ -1,136 +1,58 @@ # frozen_string_literal: true -require 'ruby2d' -require_relative 'quad_camera_tracker' +require_relative 'adapt_triangle' -# 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 - class <