Class: Chingu::Animation
- Inherits:
-
Object
- Object
- Chingu::Animation
- Defined in:
- lib/chingu/animation.rb
Overview
The Animation-class helps you load and manage a tileanimation. A Tileanimation is a file where all the frames are put after eachother.
An easy to use program to create tileanimations is tilestudio.sourceforge.net/ or www.humanbalance.net/gale/us/
TODO: Support frames in invidual image-files? Is autodetection of width / height possible?
Instance Attribute Summary collapse
-
#bounce ⇒ Object
Returns the value of attribute bounce.
-
#delay ⇒ Object
Returns the value of attribute delay.
-
#frames ⇒ Object
Returns the value of attribute frames.
-
#index ⇒ Object
Returns the value of attribute index.
-
#loop ⇒ Object
Returns the value of attribute loop.
-
#step ⇒ Object
Returns the value of attribute step.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Fetch a frame or frames:.
-
#[]=(name, animation) ⇒ Object
Manually initialize a frame-range with an Animation-instance.
- #animations ⇒ Object
-
#first ⇒ Object
Returns the first frame (Gosu::Image) from animation.
-
#frame_names ⇒ Object
Return frame names in { :name => range } -form.
-
#frame_names=(names) ⇒ Object
Put name on specific ranges of frames.
-
#image ⇒ Object
Get the current frame (a Gosu::Image).
-
#initialize(options) ⇒ Animation
constructor
Create a new Animation.
-
#last ⇒ Object
Returns the last frame (Gosu::Image) from animation.
-
#last_frame? ⇒ Boolean
Returns true if the current frame is the last.
-
#new_from_frames(range) ⇒ Object
Returns a new animation with the frames from the original animation.
-
#next(recursion = true) ⇒ Object
(also: #next!)
Propelles the animation forward.
-
#on_frame(frames, &block) ⇒ Object
Execute a certain block of code when a certain frame in the animation is active.
-
#reset ⇒ Object
(also: #reset!)
Resets the animation, re-starts it at frame 0 returns itself.
-
#retrofy ⇒ Object
Initialize non-blurry zoom on frames in animation.
-
#size ⇒ Object
- width, height
-
for each frame in the animation.
-
#trim ⇒ Object
Remove transparent space from each frame so the actual sprite is touching the border of the image.
Constructor Details
#initialize(options) ⇒ Animation
Create a new Animation.
- loop: [true|false]. After the last frame is used, start from the beginning.
- bounce: [true|false]. After the last frame is used, play it backwards untill the first frame is used again, then start playing forwards again.
- file: Tile-file to cut up animation frames from. Could be a full path or just a name -- then it will look for media_path(file)
- width: width of each frame in the tileanimation
- height: width of each frame in the tileanimation
- size: [width, height]-Array or just one fixnum which will spez both height and width
- delay: milliseconds between each frame
- step: [steps] move animation forward [steps] frames each time we call #next
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/chingu/animation.rb', line 27 def initialize() #options = {:step => 1, :loop => true, :bounce => false, :width => 32, :height => 32, :index => 0, :delay => 100}.merge(options) = {:step => 1, :loop => true, :bounce => false, :index => 0, :delay => 100}.merge() @loop = [:loop] @bounce = [:bounce] @file = [:file] @index = [:index] @delay = [:delay] @step = [:step] || 1 @dt = 0 @sub_animations = {} @frame_actions = [] unless File.exists?(@file) Gosu::Image.autoload_dirs.each do |autoload_dir| full_path = File.join(autoload_dir, @file) if File.exists?(full_path) @file = full_path break end end end # # Various ways of determening the framesize # if [:height] && [:width] @height = [:height] @width = [:width] elsif [:size] && [:size].is_a?(Array) @width = [:size][0] @height = [:size][1] elsif [:size] @width = [:size] @height = [:size] elsif @file =~ /_(\d+)x(\d+)/ # Auto-detect width/height from filename # Tilefile foo_10x25.png would mean frame width 10px and height 25px @width = $1.to_i @height = $2.to_i else # Assume the shortest side is the width/height for each frame @image = Gosu::Image.new($window, @file) @width = @height = (@image.width < @image.height) ? @image.width : @image.height end @frames = Gosu::Image.load_tiles($window, @file, @width, @height, true) end |
Instance Attribute Details
#bounce ⇒ Object
Returns the value of attribute bounce.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def bounce @bounce end |
#delay ⇒ Object
Returns the value of attribute delay.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def delay @delay end |
#frames ⇒ Object
Returns the value of attribute frames.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def frames @frames end |
#index ⇒ Object
Returns the value of attribute index.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def index @index end |
#loop ⇒ Object
Returns the value of attribute loop.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def loop @loop end |
#step ⇒ Object
Returns the value of attribute step.
13 14 15 |
# File 'lib/chingu/animation.rb', line 13 def step @step end |
Instance Method Details
#[](index) ⇒ Object
Fetch a frame or frames:
@animation[0] # returns first frame
@animation[0..2] # returns a new Animation-instance with first, second and third frame
@animation[:explode] # returns a cached Animation-instance with frames earlier set with @animation.frame_names = { ... }
155 156 157 158 159 |
# File 'lib/chingu/animation.rb', line 155 def [](index) return @frames[index] if index.is_a?(Fixnum) return self.new_from_frames(index) if index.is_a?(Range) return @sub_animations[index] if index.is_a?(Symbol) end |
#[]=(name, animation) ⇒ Object
Manually initialize a frame-range with an Animation-instance
@animation[:scan] = Animation.new(...)
166 167 168 169 |
# File 'lib/chingu/animation.rb', line 166 def []=(name, animation) @sub_animations[name] = animation return @sub_animations[name] end |
#animations ⇒ Object
116 117 118 |
# File 'lib/chingu/animation.rb', line 116 def animations @sub_animations.keys end |
#first ⇒ Object
Returns the first frame (Gosu::Image) from animation
123 124 125 |
# File 'lib/chingu/animation.rb', line 123 def first @frames.first end |
#frame_names ⇒ Object
Return frame names in { :name => range } -form
112 113 114 |
# File 'lib/chingu/animation.rb', line 112 def frame_names @frame_names end |
#frame_names=(names) ⇒ Object
Put name on specific ranges of frames. Eg. name_frames(:default => 0..3, :explode => 3..8)
Can then be accessed with @animation
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/chingu/animation.rb', line 98 def frame_names=(names) names.each do |key, value| @sub_animations[key] = self.new_from_frames(value) if value.is_a? Range @sub_animations[key] = @frames[value] if value.is_a? Fixnum # # TODO: Add support for [1,4,5] array frame selection # # @frame_names[key] = self.new_from_frames(value) if value.is_a? Array end end |
#image ⇒ Object
Get the current frame (a Gosu::Image)
174 175 176 |
# File 'lib/chingu/animation.rb', line 174 def image @frames[@index] end |
#last ⇒ Object
Returns the last frame (Gosu::Image) from animation
130 131 132 |
# File 'lib/chingu/animation.rb', line 130 def last @frames.last end |
#last_frame? ⇒ Boolean
Returns true if the current frame is the last
144 145 146 |
# File 'lib/chingu/animation.rb', line 144 def last_frame? @previous_index == @index end |
#new_from_frames(range) ⇒ Object
Returns a new animation with the frames from the original animation. Specify which frames you want with “range”, for example “0..3” for the 4 first frames.
192 193 194 195 196 197 198 199 |
# File 'lib/chingu/animation.rb', line 192 def new_from_frames(range) new_animation = self.dup new_animation.frames = [] range.each do |nr| new_animation.frames << self.frames[nr] end return new_animation end |
#next(recursion = true) ⇒ Object Also known as: next!
Propelles the animation forward. Usually called in #update within the class which holds the animation. Animation#next() will look at bounce and loop flags to always return a correct frame (a Gosu#Image)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/chingu/animation.rb', line 205 def next(recursion = true) if (@dt += $window.milliseconds_since_last_tick) >= @delay @dt = 0 @previous_index = @index @index += @step # Has the animation hit end or beginning... time for bounce or loop? if (@index >= @frames.size || @index < 0) if @bounce @step *= -1 # invert number @index += @step @index += @step elsif @loop @index = 0 else @index = @previous_index # no bounce or loop, use previous frame end end @frame_actions[@index].call if @frame_actions[@index] end @frames[@index] end |
#on_frame(frames, &block) ⇒ Object
Execute a certain block of code when a certain frame in the animation is active. This could be used for pixel perfect animation/movement.
242 243 244 245 246 247 248 |
# File 'lib/chingu/animation.rb', line 242 def on_frame(frames, &block) if frames.kind_of? Array frames.each { |frame| @frame_actions[frame] = block } else @frame_actions[frames] = block end end |
#reset ⇒ Object Also known as: reset!
Resets the animation, re-starts it at frame 0 returns itself.
182 183 184 185 |
# File 'lib/chingu/animation.rb', line 182 def reset @index = 0 self end |
#retrofy ⇒ Object
Initialize non-blurry zoom on frames in animation
233 234 235 236 |
# File 'lib/chingu/animation.rb', line 233 def retrofy frames.each { |frame| frame.retrofy } self end |
#size ⇒ Object
- width, height
-
for each frame in the animation
137 138 139 |
# File 'lib/chingu/animation.rb', line 137 def size [@width, @height] end |
#trim ⇒ Object
Remove transparent space from each frame so the actual sprite is touching the border of the image. This requires TexPlay
82 83 84 85 86 87 88 89 90 |
# File 'lib/chingu/animation.rb', line 82 def trim #@frames.each do |frame| # y = 0 # x2, y2, color = frame.line 0,y,frame.width,y, :trace => { :while_color => :alpha } # puts "final y: #{y}" # #frame.image.trace 0,0,image # #frame.splice(frame,0,0, :crop => [10, 10, 20, 20]) #end end |