summaryrefslogtreecommitdiffhomepage
path: root/animator.rb
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-01-21 13:44:15 -0500
committerrealtradam <[email protected]>2021-01-21 13:44:15 -0500
commit463b68e50e0c703f8e171168dc5e771daf1d375c (patch)
tree1e6bbbc7cb336d76e0f0a32d680f73edbded743e /animator.rb
parentadaba8c8b299f5ad9e5c33965e42a30ec65c77d6 (diff)
downloadruby2d-camera-old-463b68e50e0c703f8e171168dc5e771daf1d375c.tar.gz
ruby2d-camera-old-463b68e50e0c703f8e171168dc5e771daf1d375c.zip
linted code
Diffstat (limited to 'animator.rb')
-rw-r--r--animator.rb93
1 files changed, 64 insertions, 29 deletions
diff --git a/animator.rb b/animator.rb
index 9fae771..f1cacd0 100644
--- a/animator.rb
+++ b/animator.rb
@@ -1,55 +1,90 @@
+# frozen_string_literal: true
+# A square but it moves on its own
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
+ 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 ||= (1..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
+
+ def x=(x)
@square.x = x
end
def y
@square.y
end
- def y= y
+
+ def y=(y)
@square.y = y
end
def size
@square.size
end
- def size= size
+
+ def size=(size)
@square.size = size
end
-
-
def update(offset, zoom)
- width_zoom_offset = Window.width/2 * (zoom-1)
- height_zoom_offset = Window.height/2 * (zoom-1)
- if @axis == 0
- @square.x += @speed * zoom
- if @square.x >= ((@range[1] - offset[0]))
- @speed = -(@speed.abs)
- elsif @square.x <= ((@range[0] - offset[0]))
- @speed = @speed.abs
- end
+ #width_zoom_offset = Window.width/2 * (zoom-1)
+ #height_zoom_offset = Window.height/2 * (zoom-1)
+ puts
+ p @square
+ puts
+ p @speed
+ puts
+ p zoom
+ puts
+ puts
+ if axis.zero?
+ @square.x += speed * zoom
+ self.speed = if @square.x >= ((range[1] - offset[0]))
+ -speed.abs
+ elsif @square.x <= ((range[0] - offset[0]))
+ speed.abs
+ end
else
- @square.y += @speed * zoom
- if @square.y >= ((@range[1] - offset[1]))
- @speed = -(@speed.abs)
- elsif @square.y <= ((@range[0] - offset[1]))
- @speed = @speed.abs
- end
+ @square.y += speed * zoom
+ self.speed = if @square.y >= ((range[1] - offset[1]))
+ -speed.abs
+ elsif @square.y <= ((range[0] - offset[1]))
+ speed.abs
+ end
end
end
end