Class: Carbuncle::AnimatedSprite::AnimationSet

Inherits:
Object
  • Object
show all
Defined in:
gems/carbuncle-graphics/mrblib/animated_sprite.rb

Overview

This class handles the animation cicle for the AnimatedSprite class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sprite) ⇒ AnimationSet

Returns a new instance of AnimationSet.



13
14
15
16
17
18
19
20
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 13

def initialize(sprite)
  @sprite = sprite
  @time = 0
  @index = 0
  @current = nil
  @loops = -1
  @animations = {}
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



10
11
12
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 10

def current
  @current
end

Instance Method Details

#<<(animation) ⇒ Object



44
45
46
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 44

def <<(animation)
  add(animation)
end

#add(animation) ⇒ Object



40
41
42
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 40

def add(animation)
  @animations[animation.name] = animation
end

#frameObject



34
35
36
37
38
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 34

def frame
  return Caruncle::Point.new if current.blank?

  @animations[@current].frames[@index]
end

#last_frame?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 66

def last_frame?
  @index < (@animations[current].frames.size - 1)
end

#push(animation) ⇒ Object



48
49
50
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 48

def push(animation)
  add(animation)
end

#running?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 59

def running?
  return false if @animations[current].blank?
  return true if @loops < 0

  @loops >= 0 && last_frame?
end

#start(animation, loops = -1)) ⇒ Object



52
53
54
55
56
57
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 52

def start(animation, loops = -1)
  @index = 0
  @time = 0
  @loops = loops
  @current = animation
end

#update(dt) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'gems/carbuncle-graphics/mrblib/animated_sprite.rb', line 22

def update(dt)
  return unless running?

  current_animation = @animations[current]
  @time += dt
  while @time > current_animation.delay
    @time -= current_animation.delay
    @index = (@index + 1) % current_animation.frames.size
    @loops -= 1 if @index.zero? && @loops > 0
  end
end