Class: Lotu::AnimationSystem
- Inherits:
-
BaseSystem
- Object
- BaseSystem
- Lotu::AnimationSystem
- Defined in:
- lib/lotu/systems/animation_system.rb
Defined Under Namespace
Modules: UserMethods
Instance Method Summary collapse
- #animated? ⇒ Boolean
-
#initialize(user, opts = {}) ⇒ AnimationSystem
constructor
A new instance of AnimationSystem.
- #play_animation(name, opts = {}) ⇒ Object
- #to_s ⇒ Object
- #update ⇒ Object
Methods inherited from BaseSystem
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
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_s ⇒ Object
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 |
#update ⇒ Object
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 |