Class: Metro::Animation

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

Overview

The animation is an wrapper object for an array of Gosu::Images that also contains the additional information on the path, height, width, and tileability.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Animation

Returns a new instance of Animation.



11
12
13
14
15
16
17
18
# File 'lib/metro/animation.rb', line 11

def initialize(params = {})
  @images = Array(params[:images])
  @path = params[:path]
  @height = params[:height]
  @width = params[:width]
  @tileable = params[:tileable]
  @time_per_image = params[:time_per_image]
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/metro/animation.rb', line 9

def height
  @height
end

#imagesObject

Returns the value of attribute images.



9
10
11
# File 'lib/metro/animation.rb', line 9

def images
  @images
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/metro/animation.rb', line 9

def path
  @path
end

#tileableObject

Returns the value of attribute tileable.



9
10
11
# File 'lib/metro/animation.rb', line 9

def tileable
  @tileable
end

#time_per_imageObject

Returns the value of attribute time_per_image.



9
10
11
# File 'lib/metro/animation.rb', line 9

def time_per_image
  @time_per_image
end

#widthObject

Returns the value of attribute width.



9
10
11
# File 'lib/metro/animation.rb', line 9

def width
  @width
end

Class Method Details

.create(options) ⇒ Object

Create an animation image given the window, path, width, height,

and tileability.

Examples:

Creating an Animation Image


Metro::Animation.create window: model.window,
  path: "asset_path", tileable: tileable, width: 64, height: 64,
  time_per_image: 50


104
105
106
# File 'lib/metro/animation.rb', line 104

def self.create(options)
  new options.merge(images: create_gosu_images(options))
end

.find_or_create(options) ⇒ Object

Finds an existing image or creates a new image given the window, path, width, height and tileablilty.

Examples:

Finding or creating an Animation image


Metro::Animation.find_or_create window: model.window,
  path: "asset_path", tileable: tileable, width: 64, height: 64,
  time_per_image: 50


90
91
92
# File 'lib/metro/animation.rb', line 90

def self.find_or_create(options)
  new options.merge(images: find_or_create_gosu_images(options))
end

Instance Method Details

#ageFixnum

Returns the age of the animation.

Returns:

  • (Fixnum)

    the age of the animation.



47
48
49
# File 'lib/metro/animation.rb', line 47

def age
  current_time - start_time
end

#complete?Boolean

Returns the animation is complete if it the current index exceeds the number of images.

Returns:

  • (Boolean)

    the animation is complete if it the current index exceeds the number of images.



69
70
71
# File 'lib/metro/animation.rb', line 69

def complete?
  current_index > (images.size - 1)
end

#current_image_indexObject

Returns the current animation image to display.

Returns:

  • the current animation image to display.



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

def current_image_index
  current_index % images.size
end

#current_indexObject

Returns the current animation image count.

Returns:

  • the current animation image count.



54
55
56
# File 'lib/metro/animation.rb', line 54

def current_index
  age / time_per_image
end

#current_timeFixnum

Returns the current time in the game.

Returns:

  • (Fixnum)

    the current time in the game.



40
41
42
# File 'lib/metro/animation.rb', line 40

def current_time
  Gosu::milliseconds
end

#imageObject

Returns a Gosu::Image to be displayed in a animation sequence.

Returns:

  • a Gosu::Image to be displayed in a animation sequence.



76
77
78
# File 'lib/metro/animation.rb', line 76

def image
  images[current_image_index]
end

#start_timeFixnum

Returns the game time when the animation started to display.

Returns:

  • (Fixnum)

    the game time when the animation started to display.



33
34
35
# File 'lib/metro/animation.rb', line 33

def start_time
  @start_time ||= current_time
end

#to_hashObject

Returns a hash representation of the Animation.

Returns:

  • a hash representation of the Animation



23
24
25
26
27
28
# File 'lib/metro/animation.rb', line 23

def to_hash
  { path: path,
    width: width.to_i, height: height.to_i,
    tileable: !!tileable,
    time_per_image: time_per_image }
end