Skip to content

Instantly share code, notes, and snippets.

@krisavi
Created September 29, 2012 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisavi/3803039 to your computer and use it in GitHub Desktop.
Save krisavi/3803039 to your computer and use it in GitHub Desktop.
Diff of melonJS changes
--- /0.9.3_clean.js Sat Sep 29 06:12:29 2012
+++ /melonJS-0.9.3-nonmin.js Sat Sep 29 06:12:29 2012
@@ -1028,6 +1028,13 @@
* @type me.Viewport
* @name me.game#viewport
*/
+ api.paused = false;
+ /**
+ * a reference to the game viewport.
+ * @public
+ * @type me.Viewport
+ * @name me.game#viewport
+ */
api.viewport = null;
/**
* a reference to the game HUD (if defined).
@@ -1261,6 +1268,18 @@
return objList;
};
+ // TODO: need to cache this
+ api.getObjects = function() {
+ var objList = [];
+ for (var i = objCount, obj; i--, obj = gameObjects[i];) {
+ // TODO: check this, to support only right types of objects
+ if (!(obj instanceof me.TileMap) && !(obj instanceof me.TiledLayer) && obj.isEntity) {
+ objList.push(obj);
+ }
+ }
+ return objList;
+ };
+
/**
* return the entity corresponding to the specified GUID<br>
* note : avoid calling this function every frame since
@@ -1282,6 +1301,26 @@
};
/**
+ * return the entity corresponding to the specified ID(settings.id)<br>
+ * note : avoid calling this function every frame since
+ * it parses the whole object list each time
+ * @name me.game#getEntityByGUID
+ * @public
+ * @function
+ * @param {String} ID entity ID
+ * @return {me.ObjectEntity} Object Entity (or null if not found)
+ */
+ api.getEntityByID = function(id)
+ {
+ for (var i = objCount, obj; i--, obj = gameObjects[i];) {
+ if(obj.isEntity && obj.settings.id == id) {
+ return obj;
+ }
+ }
+ return null;
+ }
+
+ /**
* add a HUD obj to the game manager
* @name me.game#addHUD
* @public
@@ -1328,6 +1367,7 @@
api.update = function() {
// update the Frame counter
me.timer.update();
+ if(!api.paused) {
// previous rect (if any)
var oldRect = null;
// loop through our objects
@@ -1350,6 +1390,22 @@
if (api.viewport.update(drawManager.isDirty)) {
drawManager.makeAllDirty();
}
+ }
+ };
+
+ api.pause = function() {
+ api.paused = true;
+ me.state.pause();
+ };
+
+ api.unpause = function() {
+ api.paused = false;
+ me.state.resume();
+ };
+
+ api.togglePause = function() {
+ if(api.paused) me.game.unpause();
+ else me.game.pause();
};
/**
@@ -1409,6 +1465,21 @@
drawManager.flush();
};
+ api.moveToTop = function(shape) {
+ var biggest = -9999;
+ for(var i = objCount, obj; i--, obj = gameObjects[i];) {
+ if(obj.z > biggest) biggest = obj.z;
+ }
+ shape.z = biggest + 1;
+
+ api.sort();
+
+ for(var i = objCount, obj; i--, obj = gameObjects[i];) {
+ obj.z = objCount - i;
+ }
+
+ };
+
/**
* <p>Sort all objects (using object z property value).</p>
* <p>Normally all objects loaded through the LevelDirector are automatically sorted.
@@ -7315,7 +7386,23 @@
for(var t=0, l=obj.touches.length; t<l; t++) {
for (var i = handlers.length, handler; i--, handler = handlers[i];) {
// call the defined handler
- if ((handler.rect === null) || handler.rect.containsPoint({x:obj.touches[t].x,y:obj.touches[t].y})) {
+ if (handler.rect !== null) {
+ var inputX = (obj.touches[t].x + me.game.viewport.pos.x);
+ var inputY = (obj.touches[t].y + me.game.viewport.pos.y);
+ var rectX = handler.rect.pos.x;
+ var rectY = handler.rect.pos.y;
+ var rectWidth = handler.rect.width;
+ var rectHeight = handler.rect.height;
+ if(( rectX <= inputX ) && (( rectX + rectWidth) > inputX )) { //X
+ if(( rectY <= inputY ) && (( rectY + rectHeight) > inputY )) { //Y
+ if (handler.cb(e) === false) {
+ // stop propagating the event if return false
+ break;
+ }
+ }
+ }
+ }
+ if (handler.rect === null) {
if (handler.cb(e) === false) {
// stop propagating the event if return false
break;
@@ -7350,8 +7436,8 @@
else {
for(var i=0, l=e.changedTouches.length; i<l; i++) {
var t = e.changedTouches[i];
- var x = t.clientX - obj.mouse.offset.x;
- var y = t.clientY - obj.mouse.offset.y;
+ var x = t.pageX - obj.mouse.offset.x;
+ var y = t.pageY - obj.mouse.offset.y;
if (me.sys.scale != 1.0) {
x/=me.sys.scale;
y/=me.sys.scale;
@@ -7395,7 +7481,7 @@
function onMouseEvent(e) {
// in case of touch event button is undefined
var keycode = obj.mouse.bind[e.button || 0];
-
+ updateCoordFromEvent(e);
// dispatch event to registered objects
dispatchMouseEvent(e);
// check if mapped to a key
@@ -7417,7 +7503,7 @@
*/
function onTouchEvent(e) {
// update the new touch position
- updateCoordFromEvent(e);
+ //updateCoordFromEvent(e);
// reuse the mouse event function
onMouseEvent(e);
};
@@ -7655,9 +7741,9 @@
/**
* Associate a mouse (button) action to a keycode
- * Left button – 0
- * Middle button – 1
- * Right button – 2
+ * Left button ā€“ 0
+ * Middle button ā€“ 1
+ * Right button ā€“ 2
* @name me.input#bindMouse
* @public
* @function
@@ -10249,8 +10335,8 @@
this.update = function(/* time */) {
var property, elapsed, value;
-
var time = me.timer.getTime();
+ if(!me.game.paused) {
if (time < _startTime) {
@@ -10298,7 +10384,7 @@
return false;
}
-
+ }
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment