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 canvas element  
25            @type HTMLCanvasElement
26        */
27        this.canvas = null;
28        /** A reference to the 2D context of the canvas element
29            @type CanvasRenderingContext2D
30        */
31        this.context2D = null;
32        /** A reference to the in-memory canvas used as a back buffer 
33            @type HTMLCanvasElement
34        */
35        this.backBuffer = null;
36        /** A reference to the backbuffer 2D context 
37            @type CanvasRenderingContext2D
38        */
39        this.backBufferContext2D = null;
40        /** True if the canvas element is supported, false otherwise
41            @type Boolean
42        */
43        this.canvasSupported = false;
44       /** True if the resources supplied to the ResourceManager are all loaded, false otherwise
45            @type Boolean
46        */
47        this.resourcesLoaded = false;
48       /** The current colour of the loading screen
49            @type Number
50        */
51       this.loadingScreenCol = 0;
52       /** The direction of the changes to the loading screen colour.
53          1 = colour moving towards white
54          -1 = colour moving topwards balck
55            @type Number
56        */
57       this.loadingScreenColDirection = 1;
58       /** How quickly to change the loading screen colour per second
59            @type Number
60        */
61       this.loadingScreenColSpeed = 255;
62
63        /**
64            Initialises this object
65            @return A reference to the initialised object
66        */
67        this.startupGameObjectManager = function()
68        {
69            // set the global pointer to reference this object
70            g_GameObjectManager = this;
71
72            // watch for keyboard events
73            document.onkeydown = function(event){g_GameObjectManager.keyDown(event);}
74            document.onkeyup = function(event){g_GameObjectManager.keyUp(event);}
75
76            // get references to the canvas elements and their 2D contexts
77            this.canvas = document.getElementById('canvas');
78
79            // if the this.canvas.getContext function does not exist it is a safe bet that
80            // the current browser does not support the canvas element.
81            // in this case we don't go any further, which will save some debuggers (like
82            // the IE8 debugger) from throwing up a lot of errors.
83            if (this.canvas.getContext)
84            {
85                this.canvasSupported = true;
86                this.context2D = this.canvas.getContext('2d');
87                this.backBuffer = document.createElement('canvas');
88                this.backBuffer.width = this.canvas.width;
89                this.backBuffer.height = this.canvas.height;
90                this.backBufferContext2D = this.backBuffer.getContext('2d');
91            }
92
93            // create a new ResourceManager
94            new ResourceManager().startupResourceManager(
95                [{name: 'runLeft', src: 'run_left.png'},
96                {name: 'runRight', src: 'run_right.png'},
97                {name: 'idleLeft', src: 'idle_left.png'},
98                {name: 'idleRight', src: 'idle_right.png'},
99                {name: 'background0', src: 'jsplatformer4_b0.png'},
100               {name: 'background1', src: 'jsplatformer4_b1.png'},
101               {name: 'background2', src: 'jsplatformer4_b2.png'},
102               {name: 'block', src: 'BlockA0.png'},
103               {name: 'gem', src: 'Gem.png'}]);
104
105           // use setInterval to call the draw function
106           setInterval(function(){g_GameObjectManager.draw();}, SECONDS_BETWEEN_FRAMES);
107
108           return this;
109       }
110
111       /**
112           The render loop
113       */
114       this.draw = function ()
115       {
116           // calculate the time since the last frame
117           var thisFrame = new Date().getTime();
118           var dt = (thisFrame - this.lastFrame)/1000;
119           this.lastFrame = thisFrame;
120
121           if (!this.resourcesLoaded)
122           {
123               var numLoaded = 0;
124               for (i = 0; i < g_ResourceManager.imageProperties.length; ++i)
125               {
126                   if (g_ResourceManager[g_ResourceManager.imageProperties[i]].complete)
127                       ++numLoaded;
128               }
129
130               if ( numLoaded == g_ResourceManager.imageProperties.length )
131               {
132                   // create a new ApplicationManager
133                   new ApplicationManager().startupApplicationManager(this.canvas.width, this.canvas.height);
134                   this.resourcesLoaded = true;
135               }
136            else
137            {
138               this.loadingScreenCol += this.loadingScreenColDirection * this.loadingScreenColSpeed * dt;
139               if (this.loadingScreenCol > 255)
140               {
141                  this.loadingScreenCol = 255;
142                  this.loadingScreenColDirection = -1;
143               }
144               else if (this.loadingScreenCol < 0)
145               {
146                  this.loadingScreenCol = 0;
147                  this.loadingScreenColDirection = 1;
148               }
149               this.context2D.fillStyle = "rgb(" + parseInt(this.loadingScreenCol) + "," + parseInt(this.loadingScreenCol) + "," + parseInt(this.loadingScreenCol) + ")";
150               this.context2D.fillRect (0, 0, this.canvas.width, this.canvas.height);
151            }
152           }
153
154           // clear the drawing contexts
155           if (this.canvasSupported && this.resourcesLoaded)
156           {
157               this.backBufferContext2D.clearRect(0, 0, this.backBuffer.width, this.backBuffer.height);
158               this.context2D.clearRect(0, 0, this.canvas.width, this.canvas.height);
159
160               // first update all the game objects
161               for (x in this.gameObjects)
162               {
163                   if (this.gameObjects[x].update)
164                   {
165                       this.gameObjects[x].update(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
166                   }
167               }
168
169               // then draw the game objects
170               for (x in this.gameObjects)
171               {
172                   if (this.gameObjects[x].draw)
173                   {
174                       this.gameObjects[x].draw(dt, this.backBufferContext2D, this.xScroll, this.yScroll);
175                   }
176               }
177
178               // copy the back buffer to the displayed canvas
179               this.context2D.drawImage(this.backBuffer, 0, 0);
180           }
181       };
182
183       /**
184           Adds a new GameObject to the gameObjects collection
185           @param gameObject The object to add
186       */
187       this.addGameObject = function(gameObject)
188       {
189           this.gameObjects.push(gameObject);
190           this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
191       };
192
193       /**
194           Removes a GameObject from the gameObjects collection
195           @param gameObject The object to remove
196       */
197       this.removeGameObject = function(gameObject)
198       {
199           this.gameObjects.removeObject(gameObject);
200       }
201
202       this.keyDown = function(event)
203       {
204           for (x in this.gameObjects)
205           {
206               if (this.gameObjects[x].keyDown)
207               {
208                   this.gameObjects[x].keyDown(event);
209               }
210           }
211       }
212
213       this.keyUp = function(event)
214       {
215           for (x in this.gameObjects)
216           {
217               if (this.gameObjects[x].keyUp)
218               {
219                   this.gameObjects[x].keyUp(event);
220               }
221           }
222       }
223   }

Top