summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--animator.rb2
-rw-r--r--camera.rb178
-rw-r--r--quad_camera_tracker.rb173
-rw-r--r--run.rb35
4 files changed, 217 insertions, 171 deletions
diff --git a/animator.rb b/animator.rb
index 94f7770..0d713b1 100644
--- a/animator.rb
+++ b/animator.rb
@@ -15,7 +15,7 @@ class AnimatedSquare
attr_writer :speed
def speed
- @speed ||= (1..5).to_a.sample
+ @speed ||= (0..5).to_a.sample
end
def axis
diff --git a/camera.rb b/camera.rb
index 0044794..c361806 100644
--- a/camera.rb
+++ b/camera.rb
@@ -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
diff --git a/quad_camera_tracker.rb b/quad_camera_tracker.rb
new file mode 100644
index 0000000..d0bcd21
--- /dev/null
+++ b/quad_camera_tracker.rb
@@ -0,0 +1,173 @@
+# frozen_string_literal: true
+
+module QuadCameraTracker
+ def update(zoom: 1, x: 0, y: 0)
+ if x != 0 or y != 0
+ @x1 -= x
+ @x2 -= x
+ @x3 -= x
+ @x4 -= x
+ @y1 -= y
+ @y2 -= y
+ @y3 -= y
+ @y4 -= y
+ end
+ if zoom != 1
+ @x1 *= zoom
+ @x2 *= zoom
+ @x3 *= zoom
+ @x4 *= zoom
+ @y1 *= zoom
+ @y2 *= zoom
+ @y3 *= zoom
+ @y4 *= zoom
+ end
+ end
+
+ def true_center
+ [[@x1, @x2, @x3, @x4].min, [@y1, @y2, @y3, @y4].min]
+ 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 ||= 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 ||= 0
+ 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
+ @x1 / Camera.zoom_level + Camera.camera_position[0] - x
+ # 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 / 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
+
+ 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
diff --git a/run.rb b/run.rb
index 87d430a..5149f89 100644
--- a/run.rb
+++ b/run.rb
@@ -5,7 +5,11 @@ require_relative 'camera'
require_relative 'animator'
@background = Image.new('assets/background.png')
-@player = Image.new('assets/player.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
@@ -21,7 +25,7 @@ module SizableImage
end
@background.extend SizableImage
[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
@@ -76,16 +80,16 @@ Rectangle.new(
on :key do |event|
if event.key == 'a'
- @x_move -= @speed * Camera.zoom_level
+ @x_move -= @speed
end
if event.key == 'd'
- @x_move += @speed * Camera.zoom_level
+ @x_move += @speed
end
if event.key == 'w'
- @y_move -= @speed * Camera.zoom_level
+ @y_move -= @speed
end
if event.key == 's'
- @y_move += @speed * Camera.zoom_level
+ @y_move += @speed
end
if event.key == 'j'
@cam_x_move -= @speed
@@ -119,13 +123,15 @@ on :key do |event|
Camera.zoom_to 1
end
end
-@quad = Quad.new(x1: 100, y1: 100,
- x2: 120, y2: 100,
- x3: 120, y3: 120,
- x4: 100, y4: 120)
+@quad = Quad.new(x1: 0, y1: 0,
+ x2: 20, y2: 0,
+ x3: 20, y3: 20,
+ x4: 0, y4: 20)
Camera << @quad
-
+@frame = 0
update do
+ @frame += 1
+ @frame %= 60
@player.x += @x_move
@player.y += @y_move
@x_move = 0
@@ -140,10 +146,13 @@ update do
@squares.each do |square|
square.update(Camera.camera_position, Camera.zoom_level)
end
-
+ @quad.x += 1
+ @quad.color = 'random' if @frame.zero?
# Alternating between follow and manual control
+ puts @player.x
+ puts @player.y
if @is_follow
- #Camera.follow @player
+ Camera.follow @player
else
Camera.move_by(@cam_x_move, @cam_y_move)
end