Class: OrangeZest::Animation
- Inherits:
-
Object
- Object
- OrangeZest::Animation
- Defined in:
- lib/orange_zest/animation.rb
Overview
An animation which can be attached to an Entity.
Instance Attribute Summary collapse
-
#images ⇒ <Gosu::Image>
The images to cycle through as part of this animation.
-
#ticks_per_image ⇒ Integer
The number of ticks to display each frame for.
Class Method Summary collapse
-
.placeholder(width, height, colour) ⇒ Animation
A helper method to create a placeholder animation, with a single frame of static colour in a chosen size.
-
.static(image) ⇒ Object
A helper method to create an animation with a single static frame.
Instance Method Summary collapse
-
#image ⇒ Gosu::Image
The current frame.
-
#initialize(images, ticks_per_image) ⇒ Animation
constructor
A new instance of Animation.
-
#reset ⇒ Object
Resets this animation to its first frame.
-
#update ⇒ Object
Ticks this animation, advancing it to the next frame if enough ticks have passed.
Constructor Details
#initialize(images, ticks_per_image) ⇒ Animation
Returns a new instance of Animation.
13 14 15 16 17 18 |
# File 'lib/orange_zest/animation.rb', line 13 def initialize(images, ticks_per_image) @images = images @ticks_per_image = ticks_per_image reset end |
Instance Attribute Details
#images ⇒ <Gosu::Image>
The images to cycle through as part of this animation.
11 12 13 |
# File 'lib/orange_zest/animation.rb', line 11 def images @images end |
#ticks_per_image ⇒ Integer
The number of ticks to display each frame for. If -1, the current frame will be displayed forever.
7 8 9 |
# File 'lib/orange_zest/animation.rb', line 7 def ticks_per_image @ticks_per_image end |
Class Method Details
.placeholder(width, height, colour) ⇒ Animation
A helper method to create a placeholder animation, with a single frame of static colour in a chosen size.
32 33 34 35 36 37 |
# File 'lib/orange_zest/animation.rb', line 32 def self.placeholder(width, height, colour) image = Gosu.render(width, height) do Gosu.draw_rect(0, 0, width, height, colour) end Animation.static(image) end |
.static(image) ⇒ Object
A helper method to create an animation with a single static frame.
22 23 24 |
# File 'lib/orange_zest/animation.rb', line 22 def self.static(image) new([image], -1) end |
Instance Method Details
#image ⇒ Gosu::Image
The current frame.
61 62 63 |
# File 'lib/orange_zest/animation.rb', line 61 def image @images[@image_idx] end |
#reset ⇒ Object
Resets this animation to its first frame.
40 41 42 43 |
# File 'lib/orange_zest/animation.rb', line 40 def reset @ticks = 0 @image_idx = 0 end |
#update ⇒ Object
Ticks this animation, advancing it to the next frame if enough ticks have passed. (Despite implementing this method, this is not a Component, as it does not make sense for it to exist on its own.)
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/orange_zest/animation.rb', line 48 def update return if @ticks_per_image == -1 @ticks += 1 if @ticks >= @ticks_per_image @image_idx += 1 @image_idx %= @images.length @ticks = 0 end end |