Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix freeze when actor width or height is 0 and tilemap is shown #1491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed Excalibur crashing when displaying both a tilemap and a zero-size actor ([#1418](https://github.com/excaliburjs/Excalibur/issues/1418))
- Fixed animation flipping behavior ([#1172](https://github.com/excaliburjs/Excalibur/issues/1172))
- Fixed actors being drawn when their opacity is 0 ([#875](https://github.com/excaliburjs/Excalibur/issues/875))
- Fixed iframe event handling, excalibur will respond to keyboard events from the top window ([#1294](https://github.com/excaliburjs/Excalibur/issues/1294))
Expand Down
7 changes: 5 additions & 2 deletions src/engine/TileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class TileMapImpl extends Class {
const height = actor.pos.y + actor.height;
const actorBounds = actor.body.collider.bounds;
const overlaps: Vector[] = [];
if (actor.width <= 0 || actor.height <= 0) {
return null;
}
// trace points for overlap
for (let x = actorBounds.left; x <= width; x += Math.min(actor.width / 2, this.cellWidth / 2)) {
for (let y = actorBounds.top; y <= height; y += Math.min(actor.height / 2, this.cellHeight / 2)) {
Expand Down Expand Up @@ -232,10 +235,10 @@ export class TileMapImpl extends Class {
const solid = Color.Red;
solid.a = 0.3;
this.data
.filter(function(cell) {
.filter(function (cell) {
return cell.solid;
})
.forEach(function(cell) {
.forEach(function (cell) {
ctx.fillStyle = solid.toString();
ctx.fillRect(cell.x, cell.y, cell.width, cell.height);
});
Expand Down
38 changes: 36 additions & 2 deletions src/spec/TileMapSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('A TileMap', () => {
});
const spriteTiles = new ex.SpriteSheet(texture, 1, 1, 64, 48);
tm.registerSpriteSheet('default', spriteTiles);
tm.data.forEach(function(cell: ex.Cell) {
tm.data.forEach(function (cell: ex.Cell) {
cell.solid = true;
cell.pushSprite(new ex.TileSprite('default', 0));
});
Expand All @@ -80,7 +80,7 @@ describe('A TileMap', () => {
});
const spriteTiles = new ex.SpriteSheet(texture, 1, 1, 64, 48);
tm.registerSpriteSheet('default', spriteTiles);
tm.data.forEach(function(cell: ex.Cell) {
tm.data.forEach(function (cell: ex.Cell) {
cell.solid = true;
cell.pushSprite(new ex.TileSprite('default', 0));
});
Expand All @@ -94,4 +94,38 @@ describe('A TileMap', () => {
});
});
});

describe('with an actor', () => {
let tm: ex.TileMap;
beforeEach(() => {
tm = new ex.TileMap({
x: 0,
y: 0,
cellWidth: 64,
cellHeight: 48,
rows: 10,
cols: 10
});
tm.data.forEach(function (cell: ex.Cell) {
cell.solid = true;
});
});

it('should collide when the actor is on a solid cell', () => {
const actor = new ex.Actor(0, 0, 20, 20);

const collision = tm.collides(actor);

expect(collision).not.toBeNull();
expect(collision).toBeTruthy();
});

it('should not collide when the actor has zero size dimensions', () => {
const actor = new ex.Actor(0, 0, 0, 0);

const collision = tm.collides(actor);

expect(collision).toBeNull();
});
});
});