Sha256: 0cbc67dcfacd25ac102b4bd1fe6e696bd325eb7fa4152cf4c302f15e5c48ee3c

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module OrangeZest
  # An animation which can be attached to an `Entity`.
  class Animation
    # The number of ticks to display each frame for. If -1, the current frame will be displayed
    # forever.
    # @return [Integer]
    attr_accessor :ticks_per_image

    # The images to cycle through as part of this animation.
    # @return [<Gosu::Image>]
    attr_accessor :images

    def initialize(images, ticks_per_image)
      @images = images
      @ticks_per_image = ticks_per_image
  
      reset
    end
  
    # A helper method to create an animation with a single static frame.
    # @param [Gosu::Image] image
    def self.static(image)
      new([image], -1)
    end

    # Resets this animation to its first frame.
    def reset
      @ticks = 0
      @image_idx = 0
    end
  
    # 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.)
    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
  
    # The current frame.
    # @return [Gosu::Image]
    def image
      @images[@image_idx]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
orange_zest-0.1.0 lib/orange_zest/animation.rb