Class: OR2D::Animation
- Inherits:
-
Object
- Object
- OR2D::Animation
- Defined in:
- lib/or2d/animation.rb
Overview
The Animation class represents a single animation. It is used to animate entities in the game.
Instance Attribute Summary collapse
-
#cache ⇒ Hash
readonly
The cache of the animation.
-
#playing ⇒ Boolean
Whether or not the animation is currently playing.
-
#target ⇒ Integer
readonly
The ID of the entity to animate.
Instance Method Summary collapse
-
#complete ⇒ Object
Completes the animation.
-
#complete? ⇒ Boolean
Is the animation complete?.
-
#initialize(entity_id, &_) ⇒ Animation
constructor
Constructs a new animation.
-
#playing? ⇒ Boolean
Is the animation playing?.
-
#resume ⇒ Object
Advances a single frame of the animation.
Constructor Details
#initialize(entity_id, &_) ⇒ Animation
Constructs a new animation.
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 block_given? && ? yield(animation) : complete rescue StandardError => e puts "An error occurred while animating entity #{entity_id}: #{e.}" puts e.backtrace if OR2D.debug? ensure Fiber.yield if end end end |
Instance Attribute Details
#cache ⇒ Hash (readonly)
Returns the cache of the animation.
15 16 17 |
# File 'lib/or2d/animation.rb', line 15 def cache @cache end |
#playing ⇒ Boolean
Returns whether or not the animation is currently playing.
7 8 9 |
# File 'lib/or2d/animation.rb', line 7 def @playing end |
#target ⇒ Integer (readonly)
Returns the ID of the entity to animate.
11 12 13 |
# File 'lib/or2d/animation.rb', line 11 def target @target end |
Instance Method Details
#complete ⇒ Object
Completes the animation.
44 45 46 |
# File 'lib/or2d/animation.rb', line 44 def complete @playing = false end |
#complete? ⇒ Boolean
Is the animation complete?
50 51 52 |
# File 'lib/or2d/animation.rb', line 50 def complete? !@playing || @playing == false || !@worker.alive? end |
#playing? ⇒ Boolean
Is the animation playing?
56 57 58 |
# File 'lib/or2d/animation.rb', line 56 def @playing == true && @worker.alive? end |
#resume ⇒ Object
Advances a single frame of the animation.
39 40 41 |
# File 'lib/or2d/animation.rb', line 39 def resume @worker.resume(self) if end |