summaryrefslogtreecommitdiffhomepage
path: root/animator.rb
blob: bf23fed2538bdb12c985f201e09e9ef549272829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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')
    @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)
    @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)
    #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
      if @square.x >= ((range[1] - offset[0]))
        self.speed = -speed.abs
      elsif @square.x <= ((range[0] - offset[0]))
        self.speed = speed.abs
      end
    else
      @square.y += speed * zoom
      if @square.y >= ((range[1] - offset[1]))
        self.speed = -speed.abs
      elsif @square.y <= ((range[0] - offset[1]))
        self.speed = speed.abs
      end
    end
  end
end