Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #49 from saebyn/develop

Add inflate methods for rect, fix gh-47
  • Loading branch information...
commit decf5077d0924fe8f313b929f5f972cd658f3f23 2 parents ca4033e + 969ea5e
@oberhamsi oberhamsi authored
Showing with 69 additions and 10 deletions.
  1. +34 −4 lib/gamejs.js
  2. +4 −0 lib/gamejs/time.js
  3. +31 −6 tests/rectsurface.js
View
38 lib/gamejs.js
@@ -209,14 +209,14 @@ objects.accessors(Rect.prototype, {
*/
'center': {
get: function() {
- return [this.left + (this.width / 2),
- this.top + (this.height / 2)
+ return [this.left + (this.width / 2) | 0,
+ this.top + (this.height / 2) | 0
];
},
set: function() {
var args = normalizeRectArguments.apply(this, arguments);
- this.left = args.left - (this.width / 2);
- this.top = args.top - (this.height / 2);
+ this.left = args.left - (this.width / 2) | 0;
+ this.top = args.top - (this.height / 2) | 0;
return;
}
},
@@ -403,6 +403,36 @@ Rect.prototype.union = function(rect) {
};
/**
+ * Grow or shrink the rectangle size
+ *
+ * @param {Number} amount to change in the width
+ * @param {Number} amount to change in the height
+ * @returns {gamejs.Rect} inflated rectangle centered on the original rectangle's center
+ */
+Rect.prototype.inflate = function(x, y) {
+ var copy = this.clone();
+
+ copy.inflateIp(x, y);
+
+ return copy;
+}
+
+/**
+ * Grow or shrink this Rect in place - not returning a new Rect like `inflate(x, y)` would.
+ *
+ * @param {Number} amount to change in the width
+ * @param {Number} amount to change in the height
+ */
+Rect.prototype.inflateIp = function(x, y) {
+ // Use Math.floor here to deal with rounding of negative numbers the
+ // way this relies on.
+ this.left -= Math.floor(x / 2);
+ this.top -= Math.floor(y / 2);
+ this.width += x;
+ this.height += y;
+}
+
+/**
* Check for collision with a point.
*
* `collidePoint(x,y)` or `collidePoint([x,y])` or `collidePoint(new Rect(x,y))`
View
4 lib/gamejs/time.js
@@ -47,6 +47,10 @@ exports.init = function() {
* @param {Number} fps specify the framerate by which you want the callback to be called. (e.g. 30 = 30 times per seconds). default: 30
*/
exports.fpsCallback = function(fn, thisObj, fps) {
+ if ( fps === undefined ) {
+ fps = 30;
+ }
+
fps = parseInt(1000/fps, 10);
CALLBACKS[fps] = CALLBACKS[fps] || [];
CALLBACKS_LASTCALL[fps] = CALLBACKS_LASTCALL[fps] || 0;
View
37 tests/rectsurface.js
@@ -31,8 +31,8 @@ test('RectConstructors', function() {
strictEqual(rect.bottom, top + height);
strictEqual(rect.right, left + width);
deepEqual(rect.center, [
- left + width / 2,
- top + height / 2
+ left + Math.floor(width / 2),
+ top + Math.floor(height / 2)
]);
var rect = new gamejs.Rect([left, top, width, height]);
@@ -44,8 +44,8 @@ test('RectConstructors', function() {
strictEqual(rect.bottom, top + height);
strictEqual(rect.right, left + width);
deepEqual(rect.center, [
- left + width / 2,
- top + height / 2
+ left + Math.floor(width / 2),
+ top + Math.floor(height / 2)
]);
var rect = new gamejs.Rect(left, top, width, height);
@@ -57,8 +57,8 @@ test('RectConstructors', function() {
strictEqual(rect.bottom, top + height);
strictEqual(rect.right, left + width);
deepEqual(rect.center, [
- left + width / 2,
- top + height / 2
+ left + Math.floor(width / 2),
+ top + Math.floor(height / 2)
]);
});
@@ -115,6 +115,31 @@ test('RectSetters', function() {
});
+test('RectInflate', function() {
+ var rect = new gamejs.Rect(0, 0, 10, 10);
+ var newRect = rect.inflate(1, 0);
+ strictEqual(newRect.left, 0);
+ strictEqual(newRect.top, 0);
+ strictEqual(newRect.width, 11);
+ strictEqual(newRect.height, 10);
+
+ rect = new gamejs.Rect(5, 5, 12, 14);
+ newRect = rect.inflate(12, 3);
+ deepEqual(newRect.center, rect.center);
+ newRect = rect.inflate(-3, 3);
+ deepEqual(newRect.center, rect.center);
+ strictEqual(newRect.left, 7);
+ strictEqual(newRect.top, 4);
+ strictEqual(newRect.width, 9);
+ strictEqual(newRect.height, 17);
+
+ rect.inflateIp(-2, -2);
+ strictEqual(rect.left, 6);
+ strictEqual(rect.top, 6);
+ strictEqual(rect.width, 10);
+ strictEqual(rect.height, 12);
+});
+
test('RectCollide', function() {
// overlapping
var rect = new gamejs.Rect(0, 0, 10, 10);
Please sign in to comment.
Something went wrong with that request. Please try again.