Module: Chingu::Traits::Animation

Defined in:
lib/chingu/traits/animation.rb

Overview

A chingu trait providing automatic loading of tile-animations

For example: class FireBall; traits :animation; end;

Will automatically load:

Adds accessors animations -> Hash with all animations, hash-key is the name of the animation

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#animationObject



71
72
73
# File 'lib/chingu/traits/animation.rb', line 71

def animation
  @animations[:default]
end

#animationsObject

Returns all animations, then access invidual states with animations etc.



78
79
80
# File 'lib/chingu/traits/animation.rb', line 78

def animations
  @animations
end

#load_animationsObject

Try loading animation from class-name



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chingu/traits/animation.rb', line 55

def load_animations
  animations = {}
  glob = "#{trait_options[:animation][:directory]}/#{self.filename}*"
  puts "Animations? #{glob}" if trait_options[:animation][:debug]
  Dir[glob].each do |tile_file|
    puts tile_file if trait_options[:animation][:debug]
    if tile_file =~ /[a-zA-Z\_+]_*(\d+)x(\d+)_*([a-zA-Z]*)\.(bmp|png)/
      state = $3.length > 0 ? $3 : "default"
      animations[state.to_sym] = Chingu::Animation.new(trait_options[:animation].merge(:file => tile_file))
    elsif tile_file =~ /[a-zA-Z\_+]\.(bmp|png)/
      animations[:default] = Chingu::Animation.new(trait_options[:animation].merge(:file => tile_file))
    end
  end
  return animations
end

#setup_trait(options) ⇒ Object



46
47
48
49
50
# File 'lib/chingu/traits/animation.rb', line 46

def setup_trait(options)			
  @animation_options = {:debug => false}.merge(options)        
  @animations = load_animations
  super
end