Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
352 lines (298 loc) · 10.3 KB
/
Copy pathgame.js
File metadata and controls
352 lines (298 loc) · 10.3 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* MelonJS Game Engine
* (C) 2011 - 2014 Olivier Biot, Jason Oster, Aaron McLeod
* http://www.melonjs.org
*/
(function () {
/**
* me.game represents your current game, it contains all the objects,
* tilemap layers, current viewport, collision map, etc...<br>
* me.game is also responsible for updating (each frame) the object status
* and draw them<br>
* @namespace me.game
* @memberOf me
*/
me.game = (function () {
// hold public stuff in our singleton
var api = {};
/*
* PRIVATE STUFF
*/
// flag to redraw the sprites
var initialized = false;
// to know when we have to refresh the display
var isDirty = true;
// frame counter for frameSkipping
// reset the frame counter
var frameCounter = 0;
var frameRate = 1;
// reference to the renderer object
var renderer = null;
/*
* PUBLIC STUFF
*/
/**
* a reference to the game viewport.
* @public
* @type {me.Viewport}
* @name viewport
* @memberOf me.game
*/
api.viewport = null;
/**
* a reference to the game collision Map
* @public
* @type {me.TMXLayer}
* @name collisionMap
* @memberOf me.game
*/
api.collisionMap = null;
/**
* a reference to the game current level
* @public
* @type {me.TMXTileMap}
* @name currentLevel
* @memberOf me.game
*/
api.currentLevel = null;
/**
* a reference to the game world <br>
* a world is a virtual environment containing all the game objects
* @public
* @type {me.Container}
* @name world
* @memberOf me.game
*/
api.world = null;
/**
* when true, all objects will be added under the root world container<br>
* when false, a `me.Container` object will be created for each
* corresponding `TMXObjectGroup`
* default value : true
* @public
* @type {boolean}
* @name mergeGroup
* @memberOf me.game
*/
api.mergeGroup = true;
/**
* The property of should be used when sorting entities <br>
* value : "x", "y", "z" (default: "z")
* @public
* @type {string}
* @name sortOn
* @memberOf me.game
*/
api.sortOn = "z";
/**
* default layer tmxRenderer
* @private
* @ignore
* @type {me.TMXRenderer}
* @name tmxRenderer
* @memberOf me.game
*/
api.tmxRenderer = null;
// FIX ME : put this somewhere else
api.NO_OBJECT = 0;
/**
* Default object type constant.<br>
* See type property of the returned collision vector.
* @const
* @name ENEMY_OBJECT
* @memberOf me.game
*/
api.ENEMY_OBJECT = 1;
/**
* Default object type constant.<br>
* See type property of the returned collision vector.
* @const
* @name COLLECTABLE_OBJECT
* @memberOf me.game
*/
api.COLLECTABLE_OBJECT = 2;
/**
* Default object type constant.<br>
* See type property of the returned collision vector.
* @const
* @name ACTION_OBJECT
* @memberOf me.game
*/
api.ACTION_OBJECT = 3; // door, etc...
/**
* Fired when a level is fully loaded and <br>
* and all entities instantiated. <br>
* Additionnaly the level id will also be passed
* to the called function.
* @public
* @callback
* @name onLevelLoaded
* @memberOf me.game
* @example
* // call myFunction () everytime a level is loaded
* me.game.onLevelLoaded = this.myFunction.bind(this);
*/
api.onLevelLoaded = null;
/**
* Initialize the game manager
* @name init
* @memberOf me.game
* @private
* @ignore
* @function
* @param {Number} [width] width of the canvas
* @param {Number} [height] width of the canvas
* init function.
*/
api.init = function (width, height) {
if (!initialized) {
// if no parameter specified use the system size
width = width || me.video.renderer.getWidth();
height = height || me.video.renderer.getHeight();
// create a defaut viewport of the same size
api.viewport = new me.Viewport(0, 0, width, height);
//the root object of our world is an entity container
api.world = new me.Container(0, 0, width, height);
// give it a name
api.world.name = "rootContainer";
// initialize the collision system (the quadTree mostly)
me.collision.init();
renderer = me.video.renderer;
// publish init notification
me.event.publish(me.event.GAME_INIT);
// translate global pointer events
me.input._translatePointerEvents();
// make display dirty by default
isDirty = true;
// dummy current level
api.currentLevel = {
pos : {
x : 0,
y : 0
}
};
api.defaultCollisionMap = new me.CollisionTiledLayer(0, 0);
// set as initialized
initialized = true;
}
};
/**
* reset the game Object manager<p>
* destroy all current objects
* @name reset
* @memberOf me.game
* @public
* @function
*/
api.reset = function () {
// clear the quadtree
me.collision.quadTree.clear();
// remove all objects
api.world.destroy();
// reset the viewport to zero ?
if (api.viewport) {
api.viewport.reset();
}
// dummy current level
api.currentLevel = {
pos : {
x : 0,
y : 0
}
};
api.collisionMap = api.defaultCollisionMap;
// reset the transform matrix to the normal one
renderer.resetTransform();
// reset the frame counter
frameCounter = 0;
frameRate = Math.round(60 / me.sys.fps);
};
/**
* Returns the parent container of the specified Child in the game world
* @name getParentContainer
* @memberOf me.game
* @function
* @param {me.Renderable} child
* @return {me.Container}
*/
api.getParentContainer = function (child) {
return child.ancestor;
};
/**
* force the redraw (not update) of all objects
* @name repaint
* @memberOf me.game
* @public
* @function
*/
api.repaint = function () {
isDirty = true;
};
/**
* update all objects of the game manager
* @name update
* @memberOf me.game
* @private
* @ignore
* @function
* @param {Number} time current timestamp as provided by the RAF callback
*/
api.update = function (time) {
// handle frame skipping if required
if ((++frameCounter % frameRate) === 0) {
// reset the frame counter
frameCounter = 0;
// update the timer
me.timer.update(time);
// only enable the quadTree when the quadtree debug mode is enabled
if (me.debug && me.debug.renderQuadTree) {
// clear and populate the quadTree
me.collision.quadTree.clear();
for (var i = api.world.children.length, item; i--, (item = api.world.children[i]);) {
// only insert object with a "physic body"
if (typeof (item.body) !== "undefined") {
me.collision.quadTree.insert(item);
}
}
}
// update all objects (and pass the elapsed time since last frame)
isDirty = api.world.update(me.timer.getDelta()) || isDirty;
// update the camera/viewport
isDirty = api.viewport.update(me.timer.getDelta()) || isDirty;
}
};
/**
* draw all existing objects
* @name draw
* @memberOf me.game
* @private
* @ignore
* @function
*/
api.draw = function () {
if (isDirty) {
// cache the viewport rendering position, so that other object
// can access it later (e,g. entityContainer when drawing floating objects)
var translateX = api.viewport.pos.x + ~~api.viewport.offset.x;
var translateY = api.viewport.pos.y + ~~api.viewport.offset.y;
// translate the world coordinates by default to screen coordinates
api.world.transform.translate(-translateX, -translateY);
// substract the map offset to current the current pos
api.viewport.screenX = translateX - api.currentLevel.pos.x;
api.viewport.screenY = translateY - api.currentLevel.pos.y;
// update all objects,
// specifying the viewport as the rectangle area to redraw
api.world.draw(renderer, api.viewport);
// translate back
api.world.transform.translate(translateX, translateY);
// draw our camera/viewport
api.viewport.draw(renderer);
}
isDirty = false;
// blit our frame
me.video.renderer.blitSurface();
};
// return our object
return api;
})();
})();