Class: Pongo::AbstractCollection

Inherits:
AbstractItem show all
Defined in:
lib/pongo/abstract_collection.rb

Direct Known Subclasses

Composite, Group

Instance Attribute Summary collapse

Attributes inherited from AbstractItem

#always_repaint, #display_object, #display_object_offset, #display_object_rotation, #fill_alpha, #fill_color, #line_alpha, #line_color, #line_thickness, #renderer, #solid, #user_data, #visible

Instance Method Summary collapse

Methods inherited from AbstractItem

#add_event_listener, #always_redraw!, #always_redraw=, #always_redraw?, #dispatch_event, #has_event_listener, #set_fill, #set_line, #set_style, #visible!, #visible?

Constructor Details

#initializeAbstractCollection

Returns a new instance of AbstractCollection.



7
8
9
10
11
12
# File 'lib/pongo/abstract_collection.rb', line 7

def initialize
  super
  self.is_parented = false
  self.particles = []
  self.constraints = []
end

Instance Attribute Details

#constraintsObject

Returns the value of attribute constraints.



5
6
7
# File 'lib/pongo/abstract_collection.rb', line 5

def constraints
  @constraints
end

#is_parentedObject

Returns the value of attribute is_parented.



5
6
7
# File 'lib/pongo/abstract_collection.rb', line 5

def is_parented
  @is_parented
end

#particlesObject

Returns the value of attribute particles.



5
6
7
# File 'lib/pongo/abstract_collection.rb', line 5

def particles
  @particles
end

#spriteObject

Returns the value of attribute sprite.



5
6
7
# File 'lib/pongo/abstract_collection.rb', line 5

def sprite
  @sprite
end

Instance Method Details

#<<(item) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/pongo/abstract_collection.rb', line 14

def <<(item)
  case item
  when AbstractParticle
    add_particle(item)
  when AbstractConstraint
    add_constraint(item)
  end
end

#add_constraint(constraint) ⇒ Object



34
35
36
37
# File 'lib/pongo/abstract_collection.rb', line 34

def add_constraint(constraint)
  constraint.init if parented?
  constraints << constraint
end

#add_particle(particle) ⇒ Object



23
24
25
26
# File 'lib/pongo/abstract_collection.rb', line 23

def add_particle(particle)
  particle.init if parented?
  particles << particle
end

#allObject Also known as: get_all



69
70
71
# File 'lib/pongo/abstract_collection.rb', line 69

def all
  particles + constraints
end

#check_collisions_vs_collection(collection) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pongo/abstract_collection.rb', line 111

def check_collisions_vs_collection(collection)
  # every particle in this collection...
  collidable_particles.each do |pga|
    # ...vs every particle in the other collection
    collection.collidable_particles.each do |pgb|
      CollisionDetector.test(pga, pgb)
    end

    # ...vs every constraint in the other collection
    collection.collidable_constraints(pga).each do |cgb|
      cgb.scp.update_position
      CollisionDetector.test(pga, cgb.scp)
    end
  end

  # every constraint in this collection...
  collidable_constraints.each do |cga|
    collection.collidable_particles(cga).each do |pgb|
      cga.scp.update_position
      CollisionDetector.test(pgb, cga.scp)
    end
  end
end

#check_internal_collisionsObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pongo/abstract_collection.rb', line 98

def check_internal_collisions
  collidable_particles.each do |pa|
    collidable_particles.each do |pb|
      CollisionDetector.test(pa, pb)
    end

    collidable_constraints(pa).each do |c|
      c.scp.update_position
      CollisionDetector.test(pa, c.scp)
    end
  end
end

#cleanupObject



60
61
62
63
64
65
66
67
# File 'lib/pongo/abstract_collection.rb', line 60

def cleanup
  if renderer
    renderer.cleanup(self)
  else
    particles.each {|p| p.cleanup}
    constraints.each {|c| c.cleanup}
  end
end

#collidable_constraints(particle = nil) ⇒ Object



92
93
94
95
96
# File 'lib/pongo/abstract_collection.rb', line 92

def collidable_constraints(particle=nil)
  constraints.select do |c| 
    c.collidable? and (particle.nil? or not c.is_connected_to?(particle))
  end
end

#collidable_particles(constraint = nil) ⇒ Object



86
87
88
89
90
# File 'lib/pongo/abstract_collection.rb', line 86

def collidable_particles(constraint=nil)
  particles.select do |p| 
    p.collidable? and (constraint.nil? or not constraint.is_connected_to?(p))
  end
end

#drawObject Also known as: paint



50
51
52
53
54
55
56
57
# File 'lib/pongo/abstract_collection.rb', line 50

def draw
  if renderer
    renderer.draw(self)
  else
    particles.each {|p| p.draw if p.always_redraw? or not p.fixed?}
    constraints.each {|c| c.draw if c.always_redraw? or not c.fixed?}
  end
end

#initObject



45
46
47
48
# File 'lib/pongo/abstract_collection.rb', line 45

def init
  particles.each {|p| p.init}
  constraints.each {|c| c.init}
end

#integrate(dt2) ⇒ Object



78
79
80
# File 'lib/pongo/abstract_collection.rb', line 78

def integrate(dt2)
  particles.each {|p| p.update(dt2)}
end

#parented?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/pongo/abstract_collection.rb', line 74

def parented?
  @is_parented
end

#remove_constraint(constraint) ⇒ Object



39
40
41
42
43
# File 'lib/pongo/abstract_collection.rb', line 39

def remove_constraint(constraint)
  if constraints.delete(constraint)
    constraint.cleanup
  end
end

#remove_particle(particle) ⇒ Object



28
29
30
31
32
# File 'lib/pongo/abstract_collection.rb', line 28

def remove_particle(particle)
  if particles.delete(particle)
    particle.cleanup
  end
end

#satisfy_constraintsObject



82
83
84
# File 'lib/pongo/abstract_collection.rb', line 82

def satisfy_constraints
  constraints.each {|c| c.resolve}
end