summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-01-19 18:55:59 -0500
committerrealtradam <[email protected]>2021-01-19 18:55:59 -0500
commit344daa131d2343283e9e33e454b328f3e233f6b4 (patch)
tree1a10595dc9dc00e828a2990e73f40f2ddb7324e1
downloadruby2d-camera-old-344daa131d2343283e9e33e454b328f3e233f6b4.tar.gz
ruby2d-camera-old-344daa131d2343283e9e33e454b328f3e233f6b4.zip
initial
-rw-r--r--animator.rb42
-rw-r--r--background.pngbin0 -> 29939 bytes
-rw-r--r--camera.rb65
-rw-r--r--player.pngbin0 -> 1044 bytes
-rw-r--r--run.rb100
5 files changed, 207 insertions, 0 deletions
diff --git a/animator.rb b/animator.rb
new file mode 100644
index 0000000..4a94a87
--- /dev/null
+++ b/animator.rb
@@ -0,0 +1,42 @@
+
+class AnimatedSquare
+ def initialize(x: '0', y: '0', size: 10, color: 'random')
+ @speed = (1..5).to_a.sample
+ @square = Square.new(x: x, y: y, size: size, color: color)
+ @axis = (0..1).to_a.sample
+ if @axis == 0
+ @range = [(((x-(250..500).to_a.sample)..x).to_a.sample),((x..(x+(250..500).to_a.sample)).to_a.sample)]
+ else
+ @range = [(((y-(250..500).to_a.sample)..y).to_a.sample),((y..(y+(250..500).to_a.sample)).to_a.sample)]
+ end
+ 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 update offset
+ if @axis == 0
+ @square.x += @speed
+ if @square.x > (@range[1] - offset[0]) or @square.x < (@range[0] - offset[0])
+ @speed = -@speed
+ end
+ else
+ @square.y += @speed
+ if @square.y > (@range[1] - offset[1]) or @square.y < (@range[0] - offset[1])
+ @speed = -@speed
+ end
+ end
+ puts offset
+ end
+end
diff --git a/background.png b/background.png
new file mode 100644
index 0000000..e12baab
--- /dev/null
+++ b/background.png
Binary files differ
diff --git a/camera.rb b/camera.rb
new file mode 100644
index 0000000..7e93f7e
--- /dev/null
+++ b/camera.rb
@@ -0,0 +1,65 @@
+
+class Camera
+
+ def self.elasticity
+ @elasticity ||= 1
+ end
+
+ def self.elasticity= speed
+ @elasticity = speed
+ end
+
+ def self.follow item
+ self.move_to((item.x - (Window.width / 2))/self.elasticity,
+ (item.y - (Window.height / 2))/self.elasticity)
+ end
+
+ def self.objects
+ @objects ||= []
+ end
+
+ def self.<< item
+ unless self.objects.include?(item)
+ self.objects.push(item)
+ end
+ end
+
+ def self.add item
+ self << item
+ end
+
+ def self.remove item
+ if self.objects.include?(item)
+ self.objects.delete(item)
+ end
+ end
+
+
+ def self.move_by(x,y)
+ self.camera_position[0] += x
+ self.camera_position[1] += y
+ objects.each do |object|
+ object.x -= x
+ object.y -= y
+ end
+ end
+ def self.move_to(x,y)
+ self.camera_position = [x+camera_position[0],y+camera_position[1]]
+ objects.each do |object|
+ object.x -= x
+ object.y -= y
+ end
+ end
+
+ def self.camera_position
+ @camera_position ||= [0,0]
+ end
+
+ private
+
+ def self.camera_position= array
+ @camera_position = array
+ end
+
+
+end
diff --git a/player.png b/player.png
new file mode 100644
index 0000000..485e972
--- /dev/null
+++ b/player.png
Binary files differ
diff --git a/run.rb b/run.rb
new file mode 100644
index 0000000..66827ee
--- /dev/null
+++ b/run.rb
@@ -0,0 +1,100 @@
+require 'ruby2d'
+require_relative 'camera'
+require_relative 'animator'
+
+@background = Image.new('background.png')
+@player = Image.new('player.png')
+@squares = []
+
+# 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 fast the camera will follow an object
+# 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
+(1..25).each 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
+
+
+# How fast the player can move
+@speed = 10
+
+# Initializing
+@x_move = 0
+@y_move = 0
+@cam_x_move = 0
+@cam_y_move = 0
+@is_follow = true
+
+on :key do |event|
+ if event.key == 'a'
+ @x_move -= @speed
+ end
+ if event.key == 'd'
+ @x_move += @speed
+ end
+ if event.key == 'w'
+ @y_move -= @speed
+ end
+ if event.key == 's'
+ @y_move += @speed
+ end
+
+
+ if event.key == 'j'
+ @cam_x_move -= @speed
+ @is_follow = false
+ end
+ if event.key == 'l'
+ @cam_x_move += @speed
+ @is_follow = false
+ end
+ if event.key == 'i'
+ @cam_y_move -= @speed
+ @is_follow = false
+ end
+ if event.key == 'k'
+ @cam_y_move += @speed
+ @is_follow = false
+ end
+
+ if event.key == 'f'
+ @is_follow = true
+ end
+end
+
+update do
+ @player.x += @x_move
+ @player.y += @y_move
+ @x_move = 0
+ @y_move = 0
+
+ # Need to use the cameras position as an offset to keep the shapes range of movement working
+ @squares.each do |square|
+ square.update Camera.camera_position
+ end
+
+
+ if @is_follow
+ Camera.follow @player
+ else
+ Camera.move_by(@cam_x_move,@cam_y_move)
+ end
+
+
+ # 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)
+ @cam_x_move = 0
+ @cam_y_move = 0
+
+end
+
+show
+