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
45 /**
46 Initialises this object
47 @return A reference to the initialised object
48 */
49 this.startupGameObjectManager = function()
50 {
51 // set the global pointer to reference this object
52 g_GameObjectManager = this;
53
54 // get references to the canvas elements and their 2D contexts
55 this.canvas = document.getElementById('canvas');
56 this.context2D = this.canvas.getContext('2d');
57 this.backBuffer = document.createElement('canvas');
58 this.backBuffer.width = this.canvas.width;
59 this.backBuffer.height = this.canvas.height;
60 this.backBufferContext2D = this.backBuffer.getContext('2d');
61
62 // create a new ApplicationManager
63 this.applicationManager = new ApplicationManager().startupApplicationManager();
64
65 // use setInterval to call the draw function
66 setInterval(function(){g_GameObjectManager.draw();}, SECONDS_BETWEEN_FRAMES);
67
68 return this;
69 }
70
71 /**
72 The render loop
73 */
74 this.draw = function ()
75 {
76 // calculate the time since the last frame
77 var thisFrame = new Date().getTime();
78 var dt = (thisFrame - this.lastFrame)/1000;
79 this.lastFrame = thisFrame;
80
81 // clear the drawing contexts
82 this.backBufferContext2D.clearRect(0, 0, this.backBuffer.width, this.backBuffer.height);
83 this.context2D.clearRect(0, 0, this.canvas.width, this.canvas.height);
84
85 // first update all the game objects
86 for (x in this.gameObjects)
87 {
88 if (this.gameObjects[x].update)
89 {
90 this.gameObjects[x].update(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
91 }
92 }
93
94 // then draw the game objects
95 for (x in this.gameObjects)
96 {
97 if (this.gameObjects[x].draw)
98 {
99 this.gameObjects[x].draw(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
100 }
101 }
102
103 // copy the back buffer to the displayed canvas
104 this.context2D.drawImage(this.backBuffer, 0, 0);
105 };
106
107 /**
108 Adds a new GameObject to the gameObjects collection
109 @param gameObject The object to add
110 */
111 this.addGameObject = function(gameObject)
112 {
113 this.gameObjects.push(gameObject);
114 this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
115 };
116
117 /**
118 Removes a GameObject from the gameObjects collection
119 @param gameObject The object to remove
120 */
121 this.removeGameObject = function(gameObject)
122 {
123 this.gameObjects.removeObject(gameObject);
124 }
125 }
Top