     @ctx              = @canvas.getContext '2d'
 
     @projectiles      = []
+    @enemies          = [new Enemy @]
 
     $('body').append @canvas
 
     @controller = new Controller @
     @player     = new Player @
-    @enemy      = new Enemy @
 
   timestamp: ->
     window.performance.now()
 
   update: ->
     @player.update()
-    @enemy.update()
+    @updateEnemies()
     @updateProjectiles()
+    @handleCollisions()
 
   render: ->
     @renderBackground()
     @player.render()
-    @enemy.render()
+    @renderEnemies()
     @renderProjectiles()
 
   frame: ->

class Enemy extends Entity

  constructor: (@game)->
    super 0, 0, 100, 100, 5, @game
    @y =  @height
    @x = -@width
    @move @x, @y

  render: ->
    @game.ctx.drawImage @game.assets.enemyShip, @x, @y, @width, @height

  update: ->
    @move(@x + @speed)
    @move(-@width) if @x > @game.width+@width

  move: (x)->
    super x, @y

    for projectile of @projectiles
      @projectiles[projectile].render()

  updateEnemies: ->
    for enemy of @enemies
      @enemies[enemy].update()

  renderEnemies: ->
    for enemy of @enemies
      @enemies[enemy].render()

  handleCollisions: ->
    for projectile of @projectiles
      for enemy of @enemies
        if @isCollision @projectiles[projectile], @enemies[enemy]
          @killEnemy @enemies[enemy]
          @addEnemy()

  killEnemy: (enemy)->
    @enemies.splice @enemies.indexOf(enemy), 1

  addEnemy: ->
    @enemies = [new Enemy @]

  isCollision: (entity1, entity2)->
    points = []
    points.push x: entity1.x,                y: entity1.y
    points.push x: entity1.x+entity1.width,  y: entity1.y
    points.push x: entity1.x,                y: entity1.y+entity1.height
    points.push x: entity1.x+entity1.width,  y: entity1.y+entity1.height

    for point of points
      return true if @isPointInEntity points[point], entity2
    false

  isPointInEntity: (point, entity)->
    (
      point.x > entity.x && point.x < (entity.x+entity.width) &&
      point.y > entity.y && point.y < (entity.y+entity.height)
    )

  addProjectile: (projectile)->
    @projectiles.push projectile

