Skip to content

Commit

Permalink
Merge branch 'develop' into testing
Browse files Browse the repository at this point in the history
  • Loading branch information
starwed committed Jan 21, 2018
2 parents 9d833a8 + 23bc903 commit 6835b6f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions playgrounds/parallax.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
Crafty.e("Background, Bar, Collision, WiredHitBox").color("blue").attr({x: 100*i, y: 10, z: 30});
Crafty.e("Midground, Bar, Collision, WiredHitBox").color("purple").attr({x: 100*i, y: 20, z: 30});
Crafty.e("Foreground, Bar, Collision, WiredHitBox").color("yellow").attr({x: 100*i, y: 30, z: 10});

// A hitbox not attached to a renderable entity should track the default layer
Crafty.e("2D, Collision, WiredHitBox").debugStroke("green").attr({x: 100*i, y: 40, z: 100, h: 100, w: 31});
Crafty.e("ActionLayer, Bar").color("brown").attr({x: 100*i, y: 40, z: 100});

}

// Some text that shows the viewport's current position, and sits in the UI layer
Expand Down
13 changes: 9 additions & 4 deletions src/debug/debug-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,17 @@ Crafty.DebugCanvas = {
current = q[i];

// If necessary, update the view transform to match the current entities layer
if (lastLayer !== current._drawlayer){
view = current._drawLayer._viewportRect();
ctx.setTransform(view._scale, 0, 0, view._scale, Math.round(-view._x*view._scale), Math.round(-view._y*view._scale));
// If the current entity has no layer, switch back to the viewport's transform
if (lastLayer !== current._drawLayer){
if (current._drawLayer) {
view = current._drawLayer._viewportRect();
ctx.setTransform(view._scale, 0, 0, view._scale, Math.round(-view._x*view._scale), Math.round(-view._y*view._scale));
} else {
view = Crafty.viewport;
ctx.setTransform(view._scale, 0, 0, view._scale, Math.round(view._x*view._scale), Math.round(view._y*view._scale));
}
lastLayer = current._drawLayer;
}

current.debugDraw(ctx);
}

Expand Down
2 changes: 1 addition & 1 deletion src/spatial/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ Crafty.c("Collision", {
SAT.obj = obj;
SAT.type = "SAT";
}
} else if (Crafty.rectManager.overlap(area, this._cbr || this._mbr || this)){
} else if (Crafty.rectManager.overlap(area, obj._cbr || obj._mbr || obj)){
results.push({
obj: obj,
type: "MBR"
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/spatial/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@
_.ok('overlap' in results[0], "expected overlap value");
});

test("hit -- collision vs. non collision tests", function(_){
var e1 = Crafty.e("2D, Collision")
.attr({x: 0, y: 0, w: 2, h: 2});
var e2 = Crafty.e("2D")
.attr({x: 0, y: 0, w: 2, h: 2});
var results = e1.hit('2D');
_.strictEqual(results.length, 1, "exactly one collision");
_.strictEqual(results[0].type, "MBR", "expected MBR collision type");
_.strictEqual(results[0].obj[0], e2[0], "expected collision with e2");

// Move e2 such that it should be returned by the broadphase search
// (i.e. it's in the same cell of the spatial hashmap)
// but doesn't actually overlap e1's MBR
e2.x=3;
var newResults = e1.hit('2D');
_.ok(!newResults, 0, "No collisions");
});

test("onHit", function(_) {
var e = Crafty.e("2D, Collision")
.attr({x: 0, y: 0, w: 25, h: 25});
Expand Down

0 comments on commit 6835b6f

Please sign in to comment.