Class: Lotu::AnimationSystem

Inherits:
BaseSystem show all
Defined in:
lib/lotu/systems/animation_system.rb

Defined Under Namespace

Modules: UserMethods

Instance Method Summary collapse

Methods inherited from BaseSystem

#draw, #dt

Constructor Details

#initialize(user, opts = {}) ⇒ AnimationSystem

Returns a new instance of AnimationSystem.



4
5
6
7
# File 'lib/lotu/systems/animation_system.rb', line 4

def initialize(user, opts={})
  super
  @user.extend(UserMethods)
end

Instance Method Details

#animated?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/lotu/systems/animation_system.rb', line 9

def animated?
  defined?(@length)
end

#play_animation(name, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lotu/systems/animation_system.rb', line 13

def play_animation(name, opts={})
  default_opts = {
    :fps => 30
  }
  opts = default_opts.merge!(opts)
  @name = name
  @length = $lotu.animation(@name).length
  @time_per_frame = 1.0/opts[:fps]
  @current_frame = 0
  @accum_time = 0
  @user.set_gosu_image($lotu.animation(@name)[0], opts)
end

#to_sObject



40
41
42
43
44
45
46
# File 'lib/lotu/systems/animation_system.rb', line 40

def to_s
  ["Name: #{@name}",
   "Current frame: #{@current_frame}",
   "Accumulated time: #{format('%.2f', @accum_time || 0)}",
   "Time per frame: #{format('%.2f', @time_per_frame || 0)}",
   "Length: #{@length}"]
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lotu/systems/animation_system.rb', line 26

def update
  if animated?
    @accum_time += dt
    if @accum_time >= @time_per_frame
      frames = @accum_time/@time_per_frame
      @current_frame += frames
      @accum_time -= @time_per_frame * frames
      @current_frame = 0 if @current_frame >= @length
      image = $lotu.animation(@name)[@current_frame]
      @user.image = image
    end
  end
end