Module: Chingu::Traits::CollisionDetection::ClassMethods

Defined in:
lib/chingu/traits/collision_detection.rb,
lib/chingu/traits/collision_detection.rb

Instance Method Summary collapse

Instance Method Details

#each_at(x, y) ⇒ Object

Yield all objects of this class that is colliding with point x,y

Example:

Enemy.each_at(sword.x, sword.y) { |enemy| enemy.die }


165
166
167
168
169
170
171
# File 'lib/chingu/traits/collision_detection.rb', line 165

def each_at(x,y)
  self.all.each do |object|
    next  unless object.collidable
    yield object if object.respond_to?(:bb) && object.bb.collide_point?(x,y)
    yield object if object.respond_to?(:radius) && Gosu.distance(object.x, object.y, x, y) < object.radius
  end
end

#each_bounding_box_collision(*klasses) ⇒ Object

Works like each_collsion but with explicit bounding_box collisions (inline-code for speedups)



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/chingu/traits/collision_detection.rb', line 194

def each_bounding_box_collision(*klasses)
  Array(klasses).each do |klass|
    object2_list = (klass.respond_to?(:all) ? klass.all : Array(klass))
    self.all.each do |object1|
      object2_list.each do |object2|
        next  if object1 == object2  # Don't collide objects with themselves
        next  unless object1.collidable && object2.collidable
        yield object1, object2  if object1.bounding_box.collide_rect?(object2.bounding_box)
      end
    end
  end
end

#each_bounding_circle_collision(*klasses) ⇒ Object

Works like each_collision but with inline-code for speedups



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chingu/traits/collision_detection.rb', line 176

def each_bounding_circle_collision(*klasses)
  Array(klasses).each do |klass|
    object2_list = (klass.respond_to?(:all) ? klass.all : Array(klass))
    #total_radius = object1.radius + object2.radius  # possible optimization?
    
    self.all.each do |object1|
      object2_list.each do |object2|
        next  if object1 == object2  # Don't collide objects with themselves
        next  unless object1.collidable && object2.collidable
        yield object1, object2  if Gosu.distance(object1.x, object1.y, object2.x, object2.y) < object1.radius + object2.radius
      end
    end
  end
end

#each_collision(*klasses) ⇒ Object

Class method that will check for collisions between all instances of two classes and yield the 2 colliding game object instances.

It will not collide objects with themselves.

example:

Enemy.each_collision(Bullet).each do |enemy, bullet| enemy.die!; end


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/chingu/traits/collision_detection.rb', line 218

def each_collision(*klasses)
  # Make sure klasses is always an array.
  Array(klasses).each do |klass|
    
    if self.respond_to?(:instance_methods) && klass.respond_to?(:instance_methods)
      if self.instance_methods.include?(:radius) && klass.instance_methods.include?(:radius)
        self.each_bounding_circle_collision(klass) do |o1, o2|
          yield o1, o2
        end
        next
      end
        
      if self.instance_methods.include?(:bounding_box) && klass.instance_methods.include?(:bounding_box)
        self.each_bounding_box_collision(klass) do |o1, o2|
          yield o1, o2
        end
        next
      end
    end
      
    #
    # Possible optimization, look into later.
    #
    # type1 = self.instance_methods.include?(:bounding_box) ? :bb : :bc
    # type2 = klass.instance_methods.include?(:bounding_box) ? :bb : :bc
    # Pointless optmization-attempts?
    #if type1 != type2
    #  self.all.each do |object1|
    #    object2_list.each do |object2|
    #      next  if object1 == object2  # Don't collide objects with themselves
    #      yield object1, object2  if object1.bounding_box_bounding_circle_collision?(object2)
    #    end
    #  end
    #end
    object2_list = (klass.respond_to?(:all) ? klass.all : Array(klass))
    self.all.each do |object1|
      object2_list.each do |object2|
        next  if object1 == object2  # Don't collide objects with themselves
        yield object1, object2  if object1.collides?(object2)
      end
    end
  end
end

#initialize_trait(options = {}) ⇒ Object



39
40
41
# File 'lib/chingu/traits/collision_detection.rb', line 39

def initialize_trait(options = {})
  trait_options[:collision_detection] = options
end