1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/**
A manager for all the objects in the game
@author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a>
@class
*/
function GameObjectManager()
{
/** An array of game objects
@type Arary
*/
this.gameObjects = new Array();
/** The time that the last frame was rendered
@type Date
*/
this.lastFrame = new Date().getTime();
/** The global scrolling value of the x axis
@type Number
*/
this.xScroll = 0;
/** The global scrolling value of the y axis
@type Number
*/
this.yScroll = 0;
/** A reference to the ApplicationManager instance
@type ApplicationManager
*/
this.applicationManager = null;
/** A reference to the canvas element
@type HTMLCanvasElement
*/
this.canvas = null;
/** A reference to the 2D context of the canvas element
@type CanvasRenderingContext2D
*/
this.context2D = null;
/** A reference to the in-memory canvas used as a back buffer
@type HTMLCanvasElement
*/
this.backBuffer = null;
/** A reference to the backbuffer 2D context
@type CanvasRenderingContext2D
*/
this.backBufferContext2D = null;
/**
Initialises this object
@return A reference to the initialised object
*/
this.startupGameObjectManager = function()
{
// set the global pointer to reference this object
g_GameObjectManager = this;
// get references to the canvas elements and their 2D contexts
this.canvas = document.getElementById('canvas');
this.context2D = this.canvas.getContext('2d');
this.backBuffer = document.createElement('canvas');
this.backBuffer.width = this.canvas.width;
this.backBuffer.height = this.canvas.height;
this.backBufferContext2D = this.backBuffer.getContext('2d');
// create a new ApplicationManager
this.applicationManager = new ApplicationManager().startupApplicationManager();
// use setInterval to call the draw function
setInterval(function(){g_GameObjectManager.draw();}, SECONDS_BETWEEN_FRAMES);
return this;
}
/**
The render loop
*/
this.draw = function ()
{
// calculate the time since the last frame
var thisFrame = new Date().getTime();
var dt = (thisFrame - this.lastFrame)/1000;
this.lastFrame = thisFrame;
// clear the drawing contexts
this.backBufferContext2D.clearRect(0, 0, this.backBuffer.width, this.backBuffer.height);
this.context2D.clearRect(0, 0, this.canvas.width, this.canvas.height);
// first update all the game objects
for (x in this.gameObjects)
{
if (this.gameObjects[x].update)
{
this.gameObjects[x].update(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
}
}
// then draw the game objects
for (x in this.gameObjects)
{
if (this.gameObjects[x].draw)
{
this.gameObjects[x].draw(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
}
}
// copy the back buffer to the displayed canvas
this.context2D.drawImage(this.backBuffer, 0, 0);
};
/**
Adds a new GameObject to the gameObjects collection
@param gameObject The object to add
*/
this.addGameObject = function(gameObject)
{
this.gameObjects.push(gameObject);
this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
};
/**
Removes a GameObject from the gameObjects collection
@param gameObject The object to remove
*/
this.removeGameObject = function(gameObject)
{
this.gameObjects.removeObject(gameObject);
}
}
|