 ( ($) ->
   defaults =
     FPS: 33.3333
+    age: 0
     ambientLight: 1
     backgroundColor: "#FFFFFF"
     cameraTransform: Matrix.IDENTITY
+    excludedModules: []
+    includedModules: []
+    objects: []
+    paused: false
 
-  shadowCanvas = $("<canvas width=640 height=480 />").powerCanvas()
-  hudCanvas = $("<canvas width=640 height=480 />").powerCanvas()
-
-  hudCanvas.font("bold 9pt consolas, 'Courier New', 'andale mono', 'lucida console', monospace")
-
-  ###*
-  The engine holds a bunch of game objects and fires off cool events.
-  
-  @name Engine
-  @constructor
-  ###
   window.Engine = (I) ->
     I ||= {}
 
     $.reverseMerge I, defaults
 
     intervalId = null
-    savedState = null
-    age = 0
-    paused = false
 
     queuedObjects = []
-    objects = []
   
     update = ->
-      objects = objects.select (object) ->
+      I.objects = I.objects.select (object) ->
         object.update()
         
-      objects = objects.concat(queuedObjects)
+      I.objects = I.objects.concat(queuedObjects)
       queuedObjects = []
       
       self.trigger "update"
-      
-    drawDeveloperOverlay = (canvas) ->
-      #TODO: Move this into the debug draw method of the objects themselves
-      canvas.withTransform I.cameraTransform, (canvas) ->
-        objects.each (object) ->
-          canvas.fillColor 'rgba(255, 0, 0, 0.5)'
-          canvas.fillRect(object.bounds().x, object.bounds().y, object.bounds().width, object.bounds().height)
-          
-      canvas.fillColor 'rgba(0, 0, 0, 0.5)'
-      canvas.fillRect(430, 10, 200, 60)
-      canvas.fillColor '#fff'
-      canvas.fillText("Developer Mode. Press Esc to resume", 440, 25)
-      canvas.fillText("Left click to add boxes", 440, 43)
-      canvas.fillText("Right click red boxes to edit properties", 440, 60)
 
     draw = ->
-      if I.ambientLight < 1
-        shadowContext = shadowCanvas.context()
-        shadowContext.globalCompositeOperation = "source-over"
-        shadowCanvas.clear()
-        # Fill with shadows
-        shadowCanvas.fill("rgba(0, 0, 0, #{1 - I.ambientLight})")
-  
-        # Etch out the light
-        shadowContext.globalCompositeOperation = "destination-out"
-        shadowCanvas.withTransform I.cameraTransform, (shadowCanvas) ->
-          objects.each (object, i) ->
-            if object.illuminate
-              shadowContext.globalAlpha = 1
-              object.illuminate(shadowCanvas)
-
       canvas.withTransform I.cameraTransform, (canvas) ->
         if I.backgroundColor
           canvas.fill(I.backgroundColor)
 
-        objects.invoke("draw", canvas, hudCanvas)
-           
-      if I.ambientLight < 1
-        shadows = shadowCanvas.element()
-        canvas.drawImage(shadows, 0, 0, shadows.width, shadows.height, 0, 0, shadows.width, shadows.height)
+        I.objects.invoke("draw", canvas)
 
-      hud = hudCanvas.element()
-      canvas.drawImage hud, 0, 0, hud.width, hud.height, 0, 0, hud.width, hud.height
-
-      drawDeveloperOverlay(canvas) if paused
+      self.trigger "draw", canvas
 
     step = ->
-      unless paused
+      unless I.paused
         update()
-        age += 1
+        I.age += 1
 
       draw()
    
     canvas = I.canvas || $("<canvas />").powerCanvas()
-    
-    construct = (entityData) ->
-      if entityData.class
-        entityData.class.constantize()(entityData)
-      else
-        GameObject(entityData)
   
     self = Core(I).extend
       add: (entityData) ->
-        obj = construct entityData
+        obj = GameObject.construct entityData
         
-        if intervalId && !paused
+        if intervalId && !I.paused
           queuedObjects.push obj
         else
-          objects.push obj
-          
-      construct: construct
-  
-      #TODO: This is only used in testing and should be removed when possible
-      age: ->
-        age
-  
+          I.objects.push obj
+
       #TODO: This is a bad idea in case access is attempted during update
       objects: ->
-        objects
+        I.objects
         
       objectAt: (x, y) ->
         targetObject = null

    self.attrAccessor "backgroundColor"
    self.attrAccessor "cameraTransform"
    self.include Bindable
    
    return self
)(jQuery)

        return targetObject
        
      eachObject: (iterator) ->
        I.objects.each iterator

      find: (selector) ->
        results = []

        matcher = EngineSelector.generate(selector)

        I.objects.each (object) ->
          results.push object if matcher.match object

        $.extend results, EngineSelector.instanceMethods

      collides: (bounds, sourceObject) ->
        I.objects.inject false, (collided, object) ->
          collided || (object.solid() && (object != sourceObject) && object.collides(bounds))
          
      rayCollides: (source, direction, sourceObject) ->
        hits = I.objects.map (object) ->
          hit = object.solid() && (object != sourceObject) && Collision.rayRectangle(source, direction, object.centeredBounds())
          hit.object = object if hit
          
