samples/04_physics_and_collisions/03_entities/app/main.rb
# Determines if a bullet hits an enemy
Determines if a bullet hits an enemy
Determines if a bullet hits an enemy
def calculate_collisions args
  args.state.bullets.each do |bullet| 
    args.state.enemies.each do |enemy|
      if !bullet.exploded && bullet.rect.intersect_rect?(enemy.rect)
        bullet.exploded = true 
        enemy.dead = true 
      end
    end
  end
  args.state.bullets = args.state.bullets.reject(&:exploded)
  args.state.enemies = args.state.enemies.reject(&:dead)
end
def calculate_collisions args args . state . bullets . each do | bullet | args . state . enemies . each do | enemy | if ! bullet . exploded && bullet . rect . intersect_rect? ( enemy . rect ) bullet . exploded = true enemy . dead = true end end end args . state . bullets = args . state . bullets . reject ( & :exploded ) args . state . enemies = args . state . enemies . reject ( & :dead ) end
Determines if a bullet hits an enemy
Determines if a bullet hits an enemy
# perform action on every bullet and enemy in collections # if bullet has not exploded yet and the bullet hits an enemy # bullet explodes # enemy is killed # All exploded bullets are rejected or removed from the bullets collection # and any dead enemy is rejected from the enemies collection.
args