Class: Lotu::TransformationSystem

Inherits:
System
  • Object
show all
Defined in:
lib/lotu/systems/transformation_system.rb

Defined Under Namespace

Modules: UserMethods

Instance Method Summary collapse

Methods inherited from System

#draw, #dt

Constructor Details

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

Returns a new instance of TransformationSystem.



4
5
6
7
8
9
# File 'lib/lotu/systems/transformation_system.rb', line 4

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

Instance Method Details

#tag_for_deletion(transform) ⇒ Object



64
65
66
# File 'lib/lotu/systems/transformation_system.rb', line 64

def tag_for_deletion(transform)
  @tagged_for_deletion << transform
end

#to_sObject



68
69
70
71
# File 'lib/lotu/systems/transformation_system.rb', line 68

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

#transform(object, property, opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lotu/systems/transformation_system.rb', line 11

def transform(object, property, opts)
  transformation = {
    :object => object,
    :property_getter => property,
    :property_setter => "#{property}=",
    :accum_time => 0,
    :calc => 0,
    :init => opts[:init],
    :end => opts[:end],
    :duration => opts[:duration] || 1,
    :start_in => opts[:start_in] || 0,
    :on_result => opts[:on_result],
    :loop => opts[:loop]
  }
  @transformations << transformation
end

#updateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lotu/systems/transformation_system.rb', line 28

def update
  @transformations.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
      if step > 0
        if t[:init] + t[:calc] > t[:end]
          if t[:loop]
            t[:calc] = 0
          else
            t[:calc] = t[:end] - t[:init]
            tag_for_deletion(t)
          end
        end
      else
        if t[:init] + t[:calc] < t[:end]
          if t[:loop]
            t[:calc] = 0
          else
            t[:calc] = t[:end] - t[:init]
            tag_for_deletion(t)
          end
        end
      end
      value = t[:init] + t[:calc]
      value = value.send(t[:on_result]) if t[:on_result]
      t[:object].send(t[:property_setter], value)
    end
  end

  @tagged_for_deletion.each do |to_delete|
    @transformations.delete(to_delete)
  end.clear
end