1     /**
2         A manager for all the objects in the game
3         @author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a>
4         @class
5     */
6     function GameObjectManager()
7     {
8         /** An array of game objects 
9             @type Arary
10        */
11        this.gameObjects = new Array();
12        /** The time that the last frame was rendered  
13            @type Date
14        */
15        this.lastFrame = new Date().getTime();
16        /** The global scrolling value of the x axis  
17            @type Number
18        */
19        this.xScroll = 0;
20        /** The global scrolling value of the y axis  
21            @type Number
22        */
23        this.yScroll = 0;
24        /** A reference to the ApplicationManager instance  
25            @type ApplicationManager
26        */
27        this.applicationManager = null;
28        /** A reference to the canvas element  
29            @type HTMLCanvasElement
30        */
31        this.canvas = null;
32        /** A reference to the 2D context of the canvas element
33            @type CanvasRenderingContext2D
34        */
35        this.context2D = null;
36        /** A reference to the in-memory canvas used as a back buffer 
37            @type HTMLCanvasElement
38        */
39        this.backBuffer = null;
40        /** A reference to the backbuffer 2D context 
41            @type CanvasRenderingContext2D
42        */
43        this.backBufferContext2D = null;
44        /** True if the canvas element is supported, false otherwise
45            @type Boolean
46        */
47        this.canvasSupported = false;
48
49        /**
50            Initialises this object
51            @return A reference to the initialised object
52        */
53        this.startupGameObjectManager = function()
54        {
55            // set the global pointer to reference this object
56            g_GameObjectManager = this;
57
58            // watch for keyboard events
59            document.onkeydown = function(event){g_GameObjectManager.keyDown(event);}
60            document.onkeyup = function(event){g_GameObjectManager.keyUp(event);}
61
62            // get references to the canvas elements and their 2D contexts
63            this.canvas = document.getElementById('canvas');
64
65            // if the this.canvas.getContext function does not exist it is a safe bet that
66            // the current browser does not support the canvas element.
67            // in this case we don't go any further, which will save some debuggers (like
68            // the IE8 debugger) from throwing up a lot of errors.
69            if (this.canvas.getContext)
70            {
71                this.canvasSupported = true;
72                this.context2D = this.canvas.getContext('2d');
73                this.backBuffer = document.createElement('canvas');
74                this.backBuffer.width = this.canvas.width;
75                this.backBuffer.height = this.canvas.height;
76                this.backBufferContext2D = this.backBuffer.getContext('2d');
77            }
78
79            // create a new ApplicationManager
80            this.applicationManager = new ApplicationManager().startupApplicationManager();
81
82            // use setInterval to call the draw function 
83            setInterval(function(){g_GameObjectManager.draw();}, SECONDS_BETWEEN_FRAMES);
84
85            return this;
86        }
87
88        /**
89            The render loop
90        */
91        this.draw = function ()
92        {
93            // calculate the time since the last frame
94            var thisFrame = new Date().getTime();
95            var dt = (thisFrame - this.lastFrame)/1000;
96            this.lastFrame = thisFrame;
97
98            // clear the drawing contexts
99            if (this.canvasSupported)
100           {
101               this.backBufferContext2D.clearRect(0, 0, this.backBuffer.width, this.backBuffer.height);
102               this.context2D.clearRect(0, 0, this.canvas.width, this.canvas.height);
103
104               // first update all the game objects
105               for (x in this.gameObjects)
106               {
107                   if (this.gameObjects[x].update)
108                   {
109                       this.gameObjects[x].update(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
110                   }
111               }
112
113               // then draw the game objects
114               for (x in this.gameObjects)
115               {
116                   if (this.gameObjects[x].draw)
117                   {
118                       this.gameObjects[x].draw(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
119                   }
120               }
121
122               // copy the back buffer to the displayed canvas
123               this.context2D.drawImage(this.backBuffer, 0, 0);
124           }
125       };
126
127       /**
128           Adds a new GameObject to the gameObjects collection
129           @param gameObject The object to add
130       */
131       this.addGameObject = function(gameObject)
132       {
133           this.gameObjects.push(gameObject);
134           this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
135       };
136
137       /**
138           Removes a GameObject from the gameObjects collection
139           @param gameObject The object to remove
140       */
141       this.removeGameObject = function(gameObject)
142       {
143           this.gameObjects.removeObject(gameObject);
144       }
145
146       this.keyDown = function(event)
147       {
148           for (x in this.gameObjects)
149           {
150               if (this.gameObjects[x].keyDown)
151               {
152                   this.gameObjects[x].keyDown(event);
153               }
154           }
155       }
156
157       this.keyUp = function(event)
158       {
159           for (x in this.gameObjects)
160           {
161               if (this.gameObjects[x].keyUp)
162               {
163                   this.gameObjects[x].keyUp(event);
164               }
165           }
166       }
167   }

Top