Class: Checkers::GUI::Scene::PieceAnimation

Inherits:
Object
  • Object
show all
Defined in:
lib/checkers/gui/scene/piece_animation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePieceAnimation

Returns a new instance of PieceAnimation.



10
11
12
# File 'lib/checkers/gui/scene/piece_animation.rb', line 10

def initialize
  @finished = false
end

Instance Attribute Details

#animation_proc=(value) ⇒ Object (writeonly)

Sets the attribute animation_proc

Parameters:

  • value

    the value to set the attribute animation_proc to.



7
8
9
# File 'lib/checkers/gui/scene/piece_animation.rb', line 7

def animation_proc=(value)
  @animation_proc = value
end

#finishedObject

Returns the value of attribute finished.



8
9
10
# File 'lib/checkers/gui/scene/piece_animation.rb', line 8

def finished
  @finished
end

Class Method Details

.animate(board, move) ⇒ Object



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
# File 'lib/checkers/gui/scene/piece_animation.rb', line 19

def animate(board, move)
  object = board.piece_at(*move.start_square)&.piece
  square = board.square_at(*move.end_square)
  x = square.x + Checkers::GUI::CIRCLE_TRANSLATION - object.x
  y = square.y + Checkers::GUI::CIRCLE_TRANSLATION - object.y
  object.z = 10

  animation = PieceAnimation.new

  animation.animation_proc = proc do
    unless x.zero?
      if x.negative?
        x += 1
        object.x -= 1
      else
        x -= 1
        object.x += 1
      end
    end

    unless y.zero?
      if y.negative?
        y += 1
        object.y -= 1
      else
        y -= 1
        object.y += 1
      end
    end

    if x.zero? && y.zero?
      yield if block_given?
      animation.finished = true
    end
  end

  animation
end

Instance Method Details

#callObject



14
15
16
# File 'lib/checkers/gui/scene/piece_animation.rb', line 14

def call
  @animation_proc.call
end