Class: OR2D::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/or2d/animation.rb

Overview

The Animation class represents a single animation. It is used to animate entities in the game.

Since:

  • 2023-04-26

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_id, &_) ⇒ Animation

Constructs a new animation.

Parameters:

  • entity_id (Integer)

    the ID of the entity to animate

  • _ (Proc)

    the block to execute

Since:

  • 2023-04-26



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/or2d/animation.rb', line 20

def initialize(entity_id, &_)
  @target = entity_id
  @playing = true
  @cache = {}
  @worker = Fiber.new do |animation|
    loop do
      break unless playing?

      block_given? && playing? ? yield(animation) : complete
    rescue StandardError => e
      puts "An error occurred while animating entity #{entity_id}: #{e.message}"
      puts e.backtrace if OR2D.debug?
    ensure
      Fiber.yield if playing?
    end
  end
end

Instance Attribute Details

#cacheHash (readonly)

Returns the cache of the animation.

Returns:

  • (Hash)

    the cache of the animation

Since:

  • 2023-04-26



15
16
17
# File 'lib/or2d/animation.rb', line 15

def cache
  @cache
end

#playingBoolean

Returns whether or not the animation is currently playing.

Returns:

  • (Boolean)

    whether or not the animation is currently playing



7
8
9
# File 'lib/or2d/animation.rb', line 7

def playing
  @playing
end

#targetInteger (readonly)

Returns the ID of the entity to animate.

Returns:

  • (Integer)

    the ID of the entity to animate

Since:

  • 2023-04-26



11
12
13
# File 'lib/or2d/animation.rb', line 11

def target
  @target
end

Instance Method Details

#completeObject

Completes the animation.

Since:

  • 2023-04-26



44
45
46
# File 'lib/or2d/animation.rb', line 44

def complete
  @playing = false
end

#complete?Boolean

Is the animation complete?

Returns:

  • (Boolean)

    whether or not the animation is complete

Since:

  • 2023-04-26



50
51
52
# File 'lib/or2d/animation.rb', line 50

def complete?
  !@playing || @playing == false || !@worker.alive?
end

#playing?Boolean

Is the animation playing?

Returns:

  • (Boolean)

    whether or not the animation is playing

Since:

  • 2023-04-26



56
57
58
# File 'lib/or2d/animation.rb', line 56

def playing?
  @playing == true && @worker.alive?
end

#resumeObject

Advances a single frame of the animation.

Since:

  • 2023-04-26



39
40
41
# File 'lib/or2d/animation.rb', line 39

def resume
  @worker.resume(self) if playing?
end