Class: Lotu::InterpolationSystem

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

Defined Under Namespace

Modules: UserMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSystem

#draw, #dt

Constructor Details

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

Returns a new instance of InterpolationSystem.



7
8
9
10
11
12
13
# File 'lib/lotu/systems/interpolation_system.rb', line 7

def initialize(user, opts={})
  super
  # TODO: cambiar esto por un include en la clase mejor
  user.extend(UserMethods)
  @interpolations = []
  @tagged_for_deletion = []
end

Instance Attribute Details

#interpolationsObject

Returns the value of attribute interpolations.



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

def interpolations
  @interpolations
end

Instance Method Details

#interpolate(object, property, opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lotu/systems/interpolation_system.rb', line 15

def interpolate(object, property, opts)
  interpolation = {
    :object => object,
    :property_getter => property,
    :property_setter => "#{property}=",
    :accum_time => 0,
    :calc => 0,
    :init => Float(opts[:init]),
    :end => Float(opts[:end]),
    :duration => opts[:duration] || 1,
    :start_in => opts[:start_in] || 0,
    :on_stop => opts[:on_stop] || :current,
    :every_value => opts[:every_value],
    # TODO: implement callback mechanism
    :callback => opts[:callback],
    :loop => opts[:loop],
    :bounce => opts[:bounce],
    :bouncing_back => false
  }
  @interpolations << interpolation
end

#tag_for_deletion(interpolation) ⇒ Object



73
74
75
# File 'lib/lotu/systems/interpolation_system.rb', line 73

def tag_for_deletion(interpolation)
  @tagged_for_deletion << interpolation
end

#to_sObject



77
78
79
80
# File 'lib/lotu/systems/interpolation_system.rb', line 77

def to_s
  ["@interpolations.length #{@interpolations.length}",
   "@tagged_for_deletion.length #{@tagged_for_deletion.length}"]
end

#updateObject

TODO: incluir código para :loop_for => n TODO: incluir soporte para “encimar” varias interpolaciones sobre una misma propiedad, de esa manera podemos “acumular” varias interpolaciones y el resultado se suma y se aplica a la propiedad una vez que todo el grupo ha sido calculado



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
# File 'lib/lotu/systems/interpolation_system.rb', line 42

def update
  @interpolations.each do |t|
    t[:accum_time] += dt
    if t[:accum_time] > t[:start_in]
      step = ( t[:end] - t[:init] )/t[:duration] * dt
      t[:calc] += step
      tag_for_deletion( t ) if step == 0
      if( t[:init] + t[:calc] > t[:end] && step > 0 ) || ( t[:init] + t[:calc] < t[:end] && step < 0 )
        if t[:loop] || ( t[:bounce] && !t[:bouncing_back] )
          t[:calc] = 0
        else
          t[:calc] = t[:end] - t[:init]
          tag_for_deletion(t)
        end
        if t[:bounce]
          t[:bouncing_back] = !t[:bouncing_back]
          t[:init], t[:end] = t[:end], t[:init]
        end
      end
      value = t[:init] + t[:calc]
      value = value.send(t[:every_value]) if t[:every_value]
      t[:object].send( t[:property_setter], value )
    end
  end

  @tagged_for_deletion.each do |to_delete|
    #@interpolations.delete( to_delete )
    @interpolations.delete_if{ |i| i.object_id == to_delete.object_id }
  end.clear
end