Skip to content

Commit

Permalink
fix: Imporve TileMap performance (thanks kristen.maeyvn!)
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 18, 2024
1 parent cf5265c commit b5a1b41
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 72 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Performance improvement in `ex.TileMap` finding onscreen tiles is now BLAZINGLY FAST thanks to a suggestion from Kristen Maeyvn in the Discord.
- TileMaps no longer need a quad tree, we can calculate the onscreen tiles with math by converting the screen into tilemap space 😎
- Fixed bug where `ex.TileMap.getTileByPoint()` did not take into account the rotation/scale of the tilemap.
- Fixes issue where mis-matched coordinate planes on parent/children caused bizarre issues. Now children are forced to inherit their parent's coordinate plane, it will always be the coordinate plane of the top most parent.
- Fixed issue with Log ScreenAppender utility where it was not positioned correctly, you can now deeply configure it!
```typescript
Expand Down
41 changes: 29 additions & 12 deletions sandbox/tests/tilemap/tilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var game = new ex.Engine({
width: 600,
height: 600
height: 600,
pixelArt: true
});

game.showDebug(true);
Expand All @@ -28,23 +29,39 @@ var tm = new ex.TileMap({
columns: 40,
rows: 40
});
tm.transform.scale = ex.vec(2, 2);
// tm.transform.rotation = Math.PI / 4;

var tilesprite = ss.sprites[0];
var tileSprite = ss.sprites[0];

for (var i = 0; i < tm.columns * tm.rows; i++) {
tm.getTileByIndex(i).addGraphic(tilesprite);
tm.getTileByIndex(i).addGraphic(tileSprite);
}

game.add(tm);

game.start(loader).then(() => {
game.currentScene.camera.move(ex.Vector.Zero.clone(), 2000, ex.EasingFunctions.EaseInOutCubic).then(() => {
game.currentScene.camera.move(new ex.Vector(600, 600), 2000, ex.EasingFunctions.EaseInOutCubic).then(() => {
game.currentScene.camera.zoomOverTime(2, 1000).then(() => {
game.currentScene.camera.zoomOverTime(1, 1000);
});
});
});
game.input.pointers.primary.on('down', (evt: ex.PointerEvent) => {
var tile = tm.getTileByPoint(evt.worldPos);
if (tile) {
if (tile.getGraphics().length) {
tile.clearGraphics();
} else {
tile.addGraphic(tileSprite);
}
}
});

console.log('started');
game.start(loader).then(async () => {
await game.currentScene.camera.move(ex.Vector.Zero.clone(), 2000, ex.EasingFunctions.EaseInOutCubic);
console.log(tm.getOnScreenTiles());
await game.currentScene.camera.move(new ex.Vector(200, 600), 2000, ex.EasingFunctions.EaseInOutCubic);
console.log(tm.getOnScreenTiles());
await game.currentScene.camera.zoomOverTime(2, 1000);
console.log(tm.getOnScreenTiles());
await game.currentScene.camera.zoomOverTime(1, 1000);
console.log(tm.getOnScreenTiles());
await game.currentScene.camera.move(tm.pos, 2000, ex.EasingFunctions.EaseInOutCubic);
console.log(tm.getOnScreenTiles());
await game.currentScene.camera.zoomOverTime(2, 1000);
console.log(tm.getOnScreenTiles());
});
16 changes: 16 additions & 0 deletions src/engine/Collision/BoundingBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ export class BoundingBox {
return new Vector((this.left + this.right) / 2, (this.top + this.bottom) / 2);
}

public get topLeft(): Vector {
return new Vector(this.left, this.top);
}

public get bottomRight(): Vector {
return new Vector(this.right, this.bottom);
}

public get topRight(): Vector {
return new Vector(this.right, this.top);
}

public get bottomLeft(): Vector {
return new Vector(this.left, this.bottom);
}

public translate(pos: Vector): BoundingBox {
return new BoundingBox(this.left + pos.x, this.top + pos.y, this.right + pos.x, this.bottom + pos.y);
}
Expand Down
3 changes: 1 addition & 2 deletions src/engine/Debug/DebugConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@ export class DebugConfig {
gridWidth: .5,
showSolidBounds: false,
solidBoundsColor: Color.fromHex('#8080807F'), // grayish
showColliderGeometry: true,
showQuadTree: false
showColliderGeometry: true
};

public isometric = {
Expand Down
Loading

0 comments on commit b5a1b41

Please sign in to comment.