Skip to content

Commit

Permalink
Merge pull request #49 from saebyn/develop
Browse files Browse the repository at this point in the history
Add inflate methods for rect, fix gh-47
  • Loading branch information
oberhamsi committed Apr 16, 2012
2 parents ca4033e + 969ea5e commit decf507
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
38 changes: 34 additions & 4 deletions lib/gamejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand Down Expand Up @@ -402,6 +402,36 @@ Rect.prototype.union = function(rect) {
return new Rect(x, y, width, height);
};

/**
* 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.
*
Expand Down
4 changes: 4 additions & 0 deletions lib/gamejs/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 31 additions & 6 deletions tests/rectsurface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
Expand All @@ -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)
]);
});

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit decf507

Please sign in to comment.