     return # Void
 
   ###
-   * player hit asteroid
-  ###
-  BulletHitAsteroid: (bullet, asteroid) =>
-    if (bullet.get(Physics)?) # already been killed?
-      @creator.destroyEntity bullet
-      PhysicsSystem.deadPool.push(bullet.get(Physics).body)
-
-    if (asteroid.get(Physics)?) # already been killed?
-      radius = asteroid.get(Collision).radius
-      position = asteroid.get(Position).position
-      if (radius > 10)
-        @creator.createAsteroid(radius - 10, position.x + Math.random() * 10 - 5, position.y + Math.random() * 10 - 5)
-        @creator.createAsteroid(radius - 10, position.x + Math.random() * 10 - 5, position.y + Math.random() * 10 - 5)
-      body = asteroid.get(Physics).body
-      asteroid.get(Asteroid).fsm.changeState('destroyed')
-      asteroid.get(DeathThroes).body = body
-      #asteroid.get(Audio).play(ExplodeAsteroid)
-      if (@games.head)
-        @games.head.state.hits++
-    return
-
-  ###
-   * asteroid hit spaceship
-  ###
-  AsteroidHitShip: (asteroid, spaceship) =>
-    if (spaceship.get(Physics)?) # already been killed?
-      body = spaceship.get(Physics).body
-      spaceship.get(Spaceship).fsm.changeState('destroyed')
-      spaceship.get(DeathThroes).body = body
-      #asteroid.get(Audio).play(ExplodeShip)
-      if (@games.head)
-        @games.head.state.lives--
-
-    return
-
-  ###
    * b2ContactListener Interface
+   *
+   * filter the event and collisions up valid combinations
   ###
   BeginContact: (contact) =>
 

    switch (a.type)
      when 'asteroid'
        switch(b.type)
          when 'asteroid'   then return
          when 'bullet'     then return @que.push(type: 1, a: b.entity, b: a.entity)
          when 'spaceship'  then return @que.push(type: 2, a: a.entity, b: b.entity)
      when 'bullet'
        switch(b.type)
          when 'asteroid'   then return @que.push(type: 1, a: a.entity, b: b.entity)
          when 'bullet'     then return
          when 'spaceship'  then return

      when 'spaceship'
        switch(b.type)
          when 'asteroid'   then return @que.push(type: 2, a: b.entity, b: a.entity)
          when 'bullet'     then return
          when 'spaceship'  then return
    return
    ###
     * type:


class asteroids.systems.CollisionSystem extends System #implements b2ContactListener

  BulletHitAsteroid = 1
  AsteroidHitShip = 2


  world           : null #  b2World
  creator         : null #  EntityCreator
  games           : null #  NodeList
  collisions      : null #  collision que

  constructor: (@world, @creator) ->
    @collisions = []
    @world.SetContactListener(this)

  update:(time) =>
    while @collisions.length
      contact = @collisions.pop()
      switch (contact.type)
        when BulletHitAsteroid
          do (bullet=contact.a, asteroid=contact.b) =>
            if (bullet.get(Physics)?) # already been killed?
              @creator.destroyEntity bullet
              PhysicsSystem.deadPool.push(bullet.get(Physics).body)

            if (asteroid.get(Physics)?) # already been killed?
              radius = asteroid.get(Collision).radius
              position = asteroid.get(Position).position
              if (radius > 10)
                @creator.createAsteroid(radius - 10, position.x + Math.random() * 10 - 5, position.y + Math.random() * 10 - 5)
                @creator.createAsteroid(radius - 10, position.x + Math.random() * 10 - 5, position.y + Math.random() * 10 - 5)
              body = asteroid.get(Physics).body
              asteroid.get(Asteroid).fsm.changeState('destroyed')
              asteroid.get(DeathThroes).body = body
              #asteroid.get(Audio).play(ExplodeAsteroid)
              if (@games.head)
                @games.head.state.hits++
            return

        when AsteroidHitShip
          do (asteroid=contact.a, spaceship=contact.b) =>
            if (spaceship.get(Physics)?) # already been killed?
              body = spaceship.get(Physics).body
              spaceship.get(Spaceship).fsm.changeState('destroyed')
              spaceship.get(DeathThroes).body = body
              #asteroid.get(Audio).play(ExplodeShip)
              if (@games.head)
                @games.head.state.lives--

            return
    return

  addToEngine: (engine) ->
