Class: Pongo::Group

Inherits:
AbstractCollection show all
Defined in:
lib/pongo/group.rb

Overview

The Group class can contain Particles, Constraints, and Composites. Groups can be assigned to be checked for collision with other Groups or internally.

Instance Attribute Summary collapse

Attributes inherited from AbstractCollection

#constraints, #is_parented, #particles, #sprite

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 AbstractCollection

#add_constraint, #add_particle, #check_collisions_vs_collection, #check_internal_collisions, #cleanup, #collidable_constraints, #collidable_particles, #parented?, #remove_constraint, #remove_particle

Methods inherited from AbstractItem

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

Constructor Details

#initialize(collide_internal = false) ⇒ Group

Returns a new instance of Group.



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

def initialize(collide_internal=false)
  super()
  @composites = []
  @collision_list = []
  self.collide_internal = collide_internal
end

Instance Attribute Details

#collide_internalObject

Returns the value of attribute collide_internal.



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

def collide_internal
  @collide_internal
end

#collision_listObject

Returns the value of attribute collision_list.



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

def collision_list
  @collision_list
end

#compositesObject

Returns the value of attribute composites.



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

def composites
  @composites
end

Instance Method Details

#<<(item) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pongo/group.rb', line 23

def <<(item)
  case item
  when Composite
    add_composite(item)
  when Group
    add_collidable(item)
  when Array
    add_collidable_list(item)
  else
    super
  end
end

#add_collidable(g) ⇒ Object



54
55
56
# File 'lib/pongo/group.rb', line 54

def add_collidable(g)
  @collision_list << g
end

#add_collidable_list(*list) ⇒ Object



62
63
64
65
# File 'lib/pongo/group.rb', line 62

def add_collidable_list(*list)
  list = list.first if list.first.is_a?(Array)
  list.each {|g| @collision_list << g}
end

#add_composite(c) ⇒ Object



36
37
38
39
40
# File 'lib/pongo/group.rb', line 36

def add_composite(c)
  @composites << c
  c.is_parented = true
  c.init if @is_parented
end

#brace(p1, p2, p3, min_ang, max_ang, options = {}) ⇒ Object



160
161
162
163
164
# File 'lib/pongo/group.rb', line 160

def brace(p1, p2, p3, min_ang, max_ang, options={})
  ang = AngularConstraint.new(p1, p2, p3, min_ang, max_ang, options)
  self << ang
  ang
end

#check_collision_group_internalObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pongo/group.rb', line 92

def check_collision_group_internal
  # check collisions not in composites
  check_internal_collisions

  # for every composite in this Group..
  valid_composites.each do |ca|
    # .. vs non composite particles and constraints in this group
    ca.check_collisions_vs_collection(self)

    # ...vs every other composite in this Group
    valid_composites.each do |cb|
      ca.check_collisions_vs_collection(cb)
    end
  end
end

#check_collision_vs_group(g) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pongo/group.rb', line 108

def check_collision_vs_group(g)
  # check particles and constraints not in composites of either group
  check_collisions_vs_collection(g)

  # for every composite in this group..
  valid_composites.each do |c|
    # check vs the particles and constraints of g
    c.check_collisions_vs_collection(g)

    # check vs composites of g
    g.valid_composites.each do |g|
      c.check_collections_vs_collection(g)
    end
  end

  # check particles and constraints of this group vs the composites of g
  g.valid_composites.each do |gc|
    check_collisions_vs_collection(gc)
  end
end

#check_collisionsObject



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

def check_collisions
  check_collision_group_internal if @collide_internal
  @collision_list.each {|g| check_collision_vs_group(g) if g}
end

#circle(x, y, radius, options = {}) ⇒ Object



133
134
135
136
137
138
# File 'lib/pongo/group.rb', line 133

def circle(x, y, radius, options={})
  cir = CircleParticle.new(x, y, radius, options)
  cir.always_redraw! if options[:always_redraw]
  self << cir
  cir
end

#clieanupObject



72
73
74
75
# File 'lib/pongo/group.rb', line 72

def clieanup
  super
  @composites.each {|c| c.cleanup}
end

#collide_internal!Object



19
20
21
# File 'lib/pongo/group.rb', line 19

def collide_internal!
  @collide_internal = true
end

#connect(p1, p2, options = {}) ⇒ Object



154
155
156
157
158
# File 'lib/pongo/group.rb', line 154

def connect(p1, p2, options={})
  con = SpringConstraint.new(p1, p2, options)
  self << con
  con
end

#drawObject



49
50
51
52
# File 'lib/pongo/group.rb', line 49

def draw
  super
  @composites.each {|c| c.draw}
end

#get_allObject Also known as: all



67
68
69
# File 'lib/pongo/group.rb', line 67

def get_all
  @particles + @constraints + @composites
end

#initObject



14
15
16
17
# File 'lib/pongo/group.rb', line 14

def init
  super
  @composites.each {|c| c.init}
end

#integrate(dt2) ⇒ Object



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

def integrate(dt2)
  super
  @composites.each {|cmp| cmp.integrate(dt2)}
end

#rectangle(x, y, width, height, options = {}) ⇒ Object



140
141
142
143
144
145
# File 'lib/pongo/group.rb', line 140

def rectangle(x, y, width, height, options={})
  rect = RectangleParticle.new(x, y, width, height, options)
  rect.always_redraw! if options[:always_redraw]
  self << rect
  rect
end

#remove_collidable(g) ⇒ Object



58
59
60
# File 'lib/pongo/group.rb', line 58

def remove_collidable(g)
  @collision_list.delete(g)
end

#remove_composite(c) ⇒ Object



42
43
44
45
46
47
# File 'lib/pongo/group.rb', line 42

def remove_composite(c)
  if @composites.delete(c)
    c.is_parented = false
    c.cleanup
  end
end

#satisfy_constraintsObject



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

def satisfy_constraints
  super
  @composites.each {|cmp| cmp.satisfy_constraints}
end

#valid_compositesObject



129
130
131
# File 'lib/pongo/group.rb', line 129

def valid_composites
  @composites.select{|c| not c.nil?}
end

#wheel(x, y, radius, options = {}) ⇒ Object



147
148
149
150
151
152
# File 'lib/pongo/group.rb', line 147

def wheel(x, y, radius, options={})
  wh = WheelParticle.new(x, y, radius, options)
  wh.always_redraw! if options[:always_redraw]
  self << wh
  wh
end