Skip to content

Instantly share code, notes, and snippets.

@wassname
Last active December 26, 2015 15:03
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 wassname/4a9fa5a8746682b51aff to your computer and use it in GitHub Desktop.
Save wassname/4a9fa5a8746682b51aff to your computer and use it in GitHub Desktop.
For Phaser.Plugin.IsoArcarde, this helps displayObject placement by moving it above others. This is done by moving them up until they no longer overlap with anything.
/*
* For Phaser.Plugin.IsoArcard, this helps displayObject placement by moving it above others. This is
* done by moving them up until they no longer overlap with anything.
* @method Phaser.Physics.IsoArcade#placeAboveOverlaps
* @param {any} displayObject - The display object to move.
* @param {any} groupCollision - The the group of displayObjects to check for overlaps with.
* @param {number} [startZ=displayObject.isoZ] - The speed it will move, in pixels per second (default is 60 pixels/sec)
*/
function placeAboveOverlaps(displayObject,groupCollision,startZ){
var overlap
startZ = startZ?startZ:displayObject.isoZ;
var deltaZ = displayObject.body.height+game.physics.isoArcade.OVERLAP_BIAS;
var maxZ = game.physics.isoArcade.bounds.height;
var isoBounds = displayObject.isoBounds.copyTo(new Phaser.Plugin.Isometric.Cube());
/* Check if two sprites overlap */
function spritesIntersect(displayObject,sprites){
var overlap
for (var i=0; i<sprites.length; i++){
if (sprites[i].isoBounds && sprites[i]!==displayObject && displayObject.id!==sprites[i].id){
overlap = displayObject.isoBounds.intersects(sprites[i].isoBounds)
if (overlap){
return true
}
}
}
return false
}
for (var i=startZ; i<maxZ-deltaZ; i+=deltaZ){
// move up and check again
isoBounds.z=i;
overlap = spritesIntersect({isoBounds:isoBounds},groupCollision.children);
if (!overlap){
displayObject.isoZ=i;
displayObject.body.prev.z=i;
return i;
}
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment