Class: Nimo::Animation

Inherits:
Object show all
Defined in:
lib/nimo/representations/sprite_representation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, frame_indexes, options) ⇒ Animation

Returns a new instance of Animation.



55
56
57
58
59
60
# File 'lib/nimo/representations/sprite_representation.rb', line 55

def initialize(name, frame_indexes, options)
  @name = name
  @frame_indexes = frame_indexes
  @options = {:timeout => 0.1, :loop => true}.merge(options)
  reset_animation
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



53
54
55
# File 'lib/nimo/representations/sprite_representation.rb', line 53

def name
  @name
end

Instance Method Details

#frame_indexObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/nimo/representations/sprite_representation.rb', line 67

def frame_index
  if (Time.now - @time_since_last_draw) > @options[:timeout]
    @time_since_last_draw = Time.now
    @current_index += 1
    if @current_index == @frame_indexes.size
      @current_index = @options[:loop] ? 0 : @frame_indexes.size - 1
    end
  end
  @frame_indexes[@current_index]
end

#reset_animationObject



62
63
64
65
# File 'lib/nimo/representations/sprite_representation.rb', line 62

def reset_animation
  @current_index = 0
  @time_since_last_draw = Time.now
end