Skip to content

Instantly share code, notes, and snippets.

@bendavis78
Created April 21, 2016 00:20
Show Gist options
  • Save bendavis78/d91bb79e99e94a2f6915ed43290b3d5f to your computer and use it in GitHub Desktop.
Save bendavis78/d91bb79e99e94a2f6915ed43290b3d5f to your computer and use it in GitHub Desktop.
//---- Game utility functions -------------------
function getBounds(id) {
var x = getXPosition(id);
var y = getYPosition(id);
var w = parseInt(getAttribute(id, "width"));
var h = parseInt(getAttribute(id, "height"));
return {
left: x,
top: y,
right: x + w,
bottom: y + h,
width: w,
height: h
};
}
function isTouching(id1, id2) {
var box1 = getBounds(id1);
var box2 = getBounds(id2);
return (box1.left < box2.right &&
box1.right > box2.left &&
box1.top < box2.bottom &&
box1.bottom > box2.top);
}
function isContained(id1, id2, debug) {
var box1 = getBounds(id1);
var box2 = getBounds(id2);
if (debug) {
console.log(box1, box2, "");
}
return (box1.right < box2.right &&
box1.bottom < box2.bottom &&
box1.top > box2.top &&
box1.left > box2.left);
}
function animate(func) {
var ts = new Date().getTime();
setInterval(function() {
var elapsed = new Date().getTime() - ts;
func(elapsed / 1000);
ts = new Date().getTime();
}, (1000 / fps));
}
//-----------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment