Class: Chingu::Animation

Inherits:
Object
  • Object
show all
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/

Instance Attribute Summary collapse

Instance Method Summary collapse

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.
- width:  width of each frame in the tileanimation
- height:  width of each frame in the tileanimation


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chingu/animation.rb', line 21

def initialize(options)
  options = {:loop => true, :bounce => false, :width => 32, :height => 32, :index => 0, :delay => 100}.merge(options)
  
  @loop = options[:loop]
  @bounce = options[:bounce]
  @file = options[:file]
  @height = options[:height]
  @width = options[:width]
  @index = options[:index]
  @delay = options[:delay]
  @dt = 0
  
  if options[:size]
    @width = options[:size][0]
    @height = options[:size][1]
  end
  
  @frame_actions = []
  @frames = Gosu::Image.load_tiles($window, @file, @width, @height, true)
  @step = 1
end

Instance Attribute Details

#delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

#framesObject

Returns the value of attribute frames.



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

def frames
  @frames
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


63
64
65
66
# File 'lib/chingu/animation.rb', line 63

def [](index)
  return @frames[index]               if  index.is_a?(Fixnum)
  return self.new_from_frames(index)  if  index.is_a?(Range)
end

#firstObject

Returns first frame (GOSU::Image) from animation



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

def first
  @frames.first
end

#imageObject

Get the current frame (a Gosu#Image)



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

def image
  @frames[@index]
end

#lastObject

Returns last frame (GOSU::Image) from animation



53
54
55
# File 'lib/chingu/animation.rb', line 53

def last
  @frames.first
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.



88
89
90
91
92
93
94
95
# File 'lib/chingu/animation.rb', line 88

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!Object

Propelles the animation forward. Usually called in #update within the class which holds the animation. #next! will look at bounce and loop flags to always return a correct frame (a Gosu#Image)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chingu/animation.rb', line 101

def next!
  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
      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.



135
136
137
138
139
140
141
# File 'lib/chingu/animation.rb', line 135

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

Resets the animation, re-starts it at frame 0 returns itself.



79
80
81
82
# File 'lib/chingu/animation.rb', line 79

def reset!
  @index = 0
  self
end

#retrofyObject

Initialize non-blurry zoom on frames in animation



126
127
128
129
# File 'lib/chingu/animation.rb', line 126

def retrofy
  frames.each { |frame| frame.retrofy }
  self
end