summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-04-19 21:58:14 -0400
committerrealtradam <[email protected]>2021-04-19 21:58:14 -0400
commit69d5a3c712028b0448489c89243618b864dd02ea (patch)
tree706f872e20692d7fddb1c0c4c4a0952159374f76
parent1966075b705877353cb9b5d61e71c17f9cef4c80 (diff)
downloadruby2d-camera-69d5a3c712028b0448489c89243618b864dd02ea.tar.gz
ruby2d-camera-69d5a3c712028b0448489c89243618b864dd02ea.zip
.
-rw-r--r--README.md10
-rw-r--r--adapt_triangle.rb133
-rw-r--r--animator.rb80
-rw-r--r--camera.rb144
-rw-r--r--quad_camera_tracker.rb248
-rw-r--r--run.rb157
6 files changed, 224 insertions, 548 deletions
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 <<self
- attr_writer :elasticity
+module Camera
+ class <<self
private
-
- attr_writer :camera_position_x, :camera_position_y, :zoom_level, :rotation_degrees
- end
-
- def self.elasticity
- @elasticity ||= 1
- end
-
- def self.camera_position_x
- @camera_position_x ||= 0
- end
-
- def self.camera_position_y
- @camera_position_y ||= 0
- end
-
- def self.camera_position
- [camera_position_x, camera_position_y]
- end
-
- def self.zoom_level
- @zoom_level ||= 1
- end
-
- def self.rotation_degrees
- @rotation_degrees ||= 0
- end
-
- def self.rotate_by(degrees)
- # offset rotation to world
- objects.each do |object|
- unless object.is_a?(Image) or object.is_a?(AnimatedSquare)
- object.update(rotate: degrees)
- end
+ def objects
+ @objects ||= []
end
- self.rotation_degrees += degrees
- self.rotation_degrees %= 360
end
- def self.zoom_by(zoom)
- objects.each do |object|
- 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),
- Window.height / 2 * (zoom - 1))
+ def self.<<(item)
+ item.extend AdaptTriangle if item.is_a? Triangle
+ objects.push(item) unless objects.include?(item)
end
- def self.zoom_to(zoom)
- zoom_by(zoom / self.zoom_level)
+ def self.x
+ @x ||= 0
end
- def self.follow(item)
- move_by(((item.x - camera_position_x - (Window.width / 2) / zoom_level) /
- (elasticity / (zoom_level * zoom_level))),
- ((item.y - camera_position_y - (Window.height / 2) / zoom_level) /
- (elasticity / (zoom_level * zoom_level))))
+ def self.x=(x)
+ angle = Camera.angle * (Math::PI / 180)
+ @y -= (-self.x + x) * Math.sin(-angle)
+ @x -= (-self.x + x) * Math.cos(-angle)
+ objects.each(&:_update)
end
- def self.objects
- @objects ||= []
+ def self.y
+ @y ||= 0
end
- def self.<<(item)
- item.extend QuadCameraTracker if item.is_a? Quad
- objects.push(item) unless objects.include?(item)
+ def self.y=(y)
+ angle = Camera.angle * (Math::PI / 180)
+ @x -= -(-self.y + y) * Math.sin(-angle)
+ @y -= (-self.y + y) * Math.cos(-angle)
+ objects.each(&:_update)
end
- def self.add(item)
- self << item
+ def self.zoom
+ @zoom ||= 1.0
end
- def self.remove(item)
- objects.delete(item) if objects.include?(item)
+ def self.zoom=(zoom)
+ #TODO
+ @zoom = zoom
end
- def self.move_by(x, y)
- objects.each do |object|
- 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
- self.camera_position_x += x / zoom_level
- self.camera_position_y += y / zoom_level
+ def self.angle
+ @angle ||= 0
end
-# SOMETHING MESSED UP
- # with the camera position thing
- def self.move_to(x, y)
- objects.each do |object|
- if object.is_a?(Image) or object.is_a?(AnimatedSquare)
- object.x -= object.x + x
- object.y -= object.y + y
- else
- object.update(x: -Camera.camera_position_x + x, y: -Camera.camera_position_y + y)
- end
- end
- self.camera_position_x = x / zoom_level
- self.camera_position_y = y / zoom_level
-=begin
- self.camera_position_x = x / zoom_level + camera_position[0]
- self.camera_position_y = y / zoom_level + camera_position[1]
- objects.each do |object|
- 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
+
+ def self.angle=(angle)
+ @angle = angle
+ objects.each(&:_update)
end
end
diff --git a/quad_camera_tracker.rb b/quad_camera_tracker.rb
deleted file mode 100644
index 95f6cbd..0000000
--- a/quad_camera_tracker.rb
+++ /dev/null
@@ -1,248 +0,0 @@
-# frozen_string_literal: true
-
-module QuadCameraTracker
- def update(options = {})
- if options[:x] and options[:y]
- @x1 -= options[:x]
- @x2 -= options[:x]
- @x3 -= options[:x]
- @x4 -= options[:x]
- @y1 -= options[:y]
- @y2 -= options[:y]
- @y3 -= options[:y]
- @y4 -= options[:y]
- end
- if options[:zoom]
- @x1 *= options[:zoom]
- @x2 *= options[:zoom]
- @x3 *= options[:zoom]
- @x4 *= options[:zoom]
- @y1 *= options[:zoom]
- @y2 *= options[:zoom]
- @y3 *= options[:zoom]
- @y4 *= options[:zoom]
- end
- if options[:rotate]
- x_pivot = -x
- y_pivot = -y
- calc_angle = (Math::PI * options[:rotate]) / 180
- x_shifted1 = x1 - x_pivot
- y_shifted1 = y1 - y_pivot
- x_shifted2 = x2 - x_pivot
- y_shifted2 = y2 - y_pivot
- x_shifted3 = x3 - x_pivot
- y_shifted3 = y3 - y_pivot
- x_shifted4 = x4 - x_pivot
- y_shifted4 = y4 - y_pivot
-
- self.x1 = x_pivot + (x_shifted1 * Math.cos(calc_angle) - y_shifted1 * Math.sin(calc_angle))
- self.y1 = y_pivot + (x_shifted1 * Math.sin(calc_angle) + y_shifted1 * Math.cos(calc_angle))
-
- self.x2 = x_pivot + (x_shifted2 * Math.cos(calc_angle) - y_shifted2 * Math.sin(calc_angle))
- self.y2 = y_pivot + (x_shifted2 * Math.sin(calc_angle) + y_shifted2 * Math.cos(calc_angle))
-
- self.x3 = x_pivot + (x_shifted3 * Math.cos(calc_angle) - y_shifted3 * Math.sin(calc_angle))
- self.y3 = y_pivot + (x_shifted3 * Math.sin(calc_angle) + y_shifted3 * Math.cos(calc_angle))
-
- self.x4 = x_pivot + (x_shifted4 * Math.cos(calc_angle) - y_shifted4 * Math.sin(calc_angle))
- self.y4 = y_pivot + (x_shifted4 * Math.sin(calc_angle) + y_shifted4 * Math.cos(calc_angle))
- end
- end
-
- # Uses a 'fast' method of getting the center
- # perfectly accurate for squares and rectangles
- # may be inaccurate for other quadrilaterals
- def lazy_center
- [([@x1, @x2, @x3, @x4].min + [@x1, @x2, @x3, @x4].max) / 2,
- ([@y1, @y2, @y3, @y4].min + [@y1, @y2, @y3, @y4].max) / 2]
- 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_debug
- @x1
- end
-
- def x1
- temp_angle = (Math::PI * -Camera.rotation_degrees) / 180
- rotatedx = (@x1 * Math.cos(temp_angle) - @y1 * Math.sin(temp_angle))
-
- #rotatedx / Camera.zoom_level + Camera.camera_position[0] - x
- temp_move_x = @x1 / Camera.zoom_level + Camera.camera_position[0] - x
- temp_move_y = @y1 / Camera.zoom_level + Camera.camera_position[1] - y
- temp_move_x * Math.cos(temp_angle) - temp_move_y * Math.sin(temp_angle)
- #@y = (x_shifted * Math.sin(@angle) + @y1 * Math.cos(@angle))
- # 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)
- temp_angle = (Math::PI * -Camera.rotation_degrees) / 180
- @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_relative(x_pivot, y_pivot, angle)
- self.rotation_degrees += angle
- self.rotation_degrees %= 360
- calc_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(calc_angle) - y_shifted1 * Math.sin(calc_angle))
- self.y1 = y_pivot + (x_shifted1 * Math.sin(calc_angle) + y_shifted1 * Math.cos(calc_angle))
-
- self.x2 = x_pivot + (x_shifted2 * Math.cos(calc_angle) - y_shifted2 * Math.sin(calc_angle))
- self.y2 = y_pivot + (x_shifted2 * Math.sin(calc_angle) + y_shifted2 * Math.cos(calc_angle))
-
- self.x3 = x_pivot + (x_shifted3 * Math.cos(calc_angle) - y_shifted3 * Math.sin(calc_angle))
- self.y3 = y_pivot + (x_shifted3 * Math.sin(calc_angle) + y_shifted3 * Math.cos(calc_angle))
-
- self.x4 = x_pivot + (x_shifted4 * Math.cos(calc_angle) - y_shifted4 * Math.sin(calc_angle))
- self.y4 = y_pivot + (x_shifted4 * Math.sin(calc_angle) + y_shifted4 * Math.cos(calc_angle))
-
- # Updates x and y to be on the origin correctly
- @x += -x1_old + self.x1
- @y += -y1_old + self.y1
- end
-
- def rotate(angle)
- rotate_relative(0, 0, angle)
- end
-end
diff --git a/run.rb b/run.rb
index 9bf436e..21bc0ea 100644
--- a/run.rb
+++ b/run.rb
@@ -1,16 +1,7 @@
# frozen_string_literal: true
-#require 'ruby2d'
+require 'ruby2d'
require_relative 'camera'
-require_relative 'animator'
-
-@background = Image.new('assets/background.png')
-#@player = Image.new('assets/player.png')
-@player = Quad.new(x1: 0, y1: 0,
- x2: 20, y2: 0,
- x3: 20, y3: 20,
- x4: 0, y4: 20)
-@squares = []
# Use this to add a few extra methods to an Image
module SizableImage
@@ -24,45 +15,29 @@ module SizableImage
end
end
[email protected] SizableImage
-#@player.extend SizableImage
-
# There is 2 ways you can add objects to be known and controlled by the camera, both do the same thing
-Camera << @background
-Camera.add @player
-
-# This controls how slowly the camera will follow an object
-# Set to 1 to not have any elasticity
-# it is not used by the .move_to or .move_by
-Camera.elasticity = 10
-
-# If you want to use a camera, you need all elements in the world to be known to it
-# Exeptions would be things such as UI elements where you want them statically placed on the screen
25.times do
tempx = (0..1920).to_a.sample
tempy = (0..1080).to_a.sample
tempsize = (25..100).to_a.sample
- Camera << Quad.new(x1: tempx,
- y1: tempy,
- x2: tempx,
- y2: tempy + tempsize,
- x3: tempx + tempsize,
- y3: tempy + tempsize,
- x4: tempx + tempsize,
- y4: tempy,
- size: (10..50).to_a.sample,
- color: 'random')
+ Camera << Triangle.new(x1: tempx,
+ y1: tempy,
+ x2: tempx,
+ y2: tempy + tempsize,
+ x3: tempx + tempsize,
+ y3: tempy + tempsize,
+ size: (10..50).to_a.sample,
+ color: 'random')
end
-#25.times do
-# @squares << AnimatedSquare.new(x: (0..1920).to_a.sample,
-# y: (0..1080).to_a.sample,
-# size: (10..50).to_a.sample,
-# color: 'random')
-# Camera << @squares.last
-#end
-
-# An example of static elements on the screen that
-# do not follow the camera movement
+@player = Triangle.new(x1: 0,
+ y1: 0,
+ x2: 0,
+ y2: 0 + 25,
+ x3: 0 + 25,
+ y3: 0 + 25,
+ size: (10..50).to_a.sample,
+ color: 'random')
+Camera << @player
Rectangle.new(
width: 350,
height: 105,
@@ -114,20 +89,20 @@ Rectangle.new(
on :key do |event|
if event.key == 'a'
- @y_move += Math.sin(Math::PI * (180 + @player.rotation_degrees) / 180) * @speed
- @x_move += Math.cos(Math::PI * (180 + @player.rotation_degrees) / 180) * @speed
+ @y_move += 0
+ @x_move += -@speed
end
if event.key == 'd'
- @y_move += Math.sin(Math::PI * @player.rotation_degrees / 180) * @speed
- @x_move += Math.cos(Math::PI * @player.rotation_degrees / 180) * @speed
+ @y_move += 0
+ @x_move += @speed
end
if event.key == 'w'
- @y_move += Math.sin(Math::PI * (-90 + @player.rotation_degrees) / 180) * @speed
- @x_move += Math.cos(Math::PI * (-90 + @player.rotation_degrees) / 180) * @speed
+ @y_move += -@speed
+ @x_move += 0
end
if event.key == 's'
- @y_move += Math.sin(Math::PI * (90 + @player.rotation_degrees) / 180) * @speed
- @x_move += Math.cos(Math::PI * (90 + @player.rotation_degrees) / 180) * @speed
+ @y_move += @speed
+ @x_move += 0
end
if event.key == 'j'
@cam_x_move -= @speed
@@ -158,81 +133,55 @@ on :key do |event|
@zoom_by = 0.985
end
if event.key == 'c'
- Camera.zoom_to 1
+ #Camera.zoom_to 1
end
if event.key == 'm'
- Camera.move_to(-10,-10)
+ #Camera.move_to(-10,-10)
end
if event.key == 'q'
- temp_cam = Camera.camera_position
- Camera.rotate_by(-1)
- Camera.move_to(@player.lazy_center[0] - (Window.width / 2),
- @player.lazy_center[1] - (Window.height / 2))
+ Camera.angle += 1
end
if event.key == 'e'
- Camera.rotate_by(1)
- Camera.move_to(@player.lazy_center[0] - (Window.width / 2),
- @player.lazy_center[1] - (Window.height / 2))
+ Camera.angle -= 1
end
if event.key == 'r'
- unless Camera.rotation_degrees.zero?
- Camera.rotate_by(-Camera.rotation_degrees)
- Camera.move_to(@player.lazy_center[0] - (Window.width / 2),
- @player.lazy_center[1] - (Window.height / 2))
- end
+ Camera.angle = 0
+ #unless Camera.rotation_degrees.zero?
+ #Camera.rotate_by(-Camera.rotation_degrees)
+ #Camera.move_to(@player.lazy_center[0] - (Window.width / 2),
+ # @player.lazy_center[1] - (Window.height / 2))
+ #end
end
end
-@quad = Quad.new(x1: 0, y1: 0,
- x2: 20, y2: 0,
- x3: 20, y3: 20,
- x4: 0, y4: 20)
-Camera << @quad
-@frame = 0
- -12)
update do
- @frame += 1
- @frame %= 60
@player.x += @x_move
@player.y += @y_move
@x_move = 0
@y_move = 0
- if @zoom_by != 1
- Camera.zoom_by @zoom_by
- @zoom_by = 1
- end
- # Need to use the cameras position as an offset to keep the shapes range of movement working
- # Need to make the zoom also give an offset
- @squares.each do |square|
- square.update(Camera.camera_position, Camera.zoom_level)
- end
- @quad.rotate_relative(50 - @quad.x,50 - @quad.y,1)
- @quad.color = 'random' if @frame.zero?
- #puts @quad.rotation_degrees
- # Alternating between follow and manual control
if @is_follow
- Camera.follow @player
+ Camera.x = @player.x
+ Camera.y = @player.y
else
- Camera.move_by(@cam_x_move, @cam_y_move)
- end
- puts @player.x1_debug
- #Camera.rotate_by(2)
- #puts Math.cos(Math::PI * @player.rotation_degrees.to_f / 180)
- #@player.rotate_relative(Math.cos(Math::PI * (@player.rotation_degrees + 1 * 180 / 4) / 180) * (10 * 1.41421356237),
- # Math.cos(Math::PI * (@player.rotation_degrees - 1 * 180 / 4) / 180) * (10 * 1.41421356237),
- # 2)
- # This function will teleport the camera directory to those coordinates
- # It is used by Camera.follow but you can use it yourself too!
- #Camera.move_to(50,50)
+ Camera.x += @cam_x_move
+ Camera.y += @cam_y_move
+ end
@cam_x_move = 0
@cam_y_move = 0
- @ui_pos.text = "Camera Position: #{Camera.camera_position[0].round(1)}, #{Camera.camera_position[1].round(1)}"
- @ui_zoom.text = "Zoom: #{Camera.zoom_level.round(3)}"
+ @ui_pos.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}"
+ @ui_zoom.text = "Zoom: 'N/A'" #{Camera.zoom_level.round(3)}"
@ui_fps.text = "FPS: #{Window.fps.round(2)}"
- @ui_rotation.text = "Angle: #{Camera.rotation_degrees}"
+ @ui_rotation.text = "Angle: #{Camera.angle}"
+=begin
+ puts 'player position'
+ puts @player.x
+ puts @player.y
+ puts 'camera position'
+ puts "y: #{Camera.y}"
+ puts "x: #{Camera.x}"
+=end
+
end
show