Class: OrangeZest::Animation

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

Overview

An animation which can be attached to an Entity.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (<Gosu::Image>)


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

def images
  @images
end

#ticks_per_imageInteger

The number of ticks to display each frame for. If -1, the current frame will be displayed forever.

Returns:

  • (Integer)


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.

Parameters:

  • width (Numeric)
  • height (Numeric)
  • colour (Gosu::Color)

Returns:



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.

Parameters:

  • image (Gosu::Image)


22
23
24
# File 'lib/orange_zest/animation.rb', line 22

def self.static(image)
  new([image], -1)
end

Instance Method Details

#imageGosu::Image

The current frame.

Returns:

  • (Gosu::Image)


61
62
63
# File 'lib/orange_zest/animation.rb', line 61

def image
  @images[@image_idx]
end

#resetObject

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

#updateObject

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