Skip to content

Commit 0f481ed

Browse files
committed
fix: [#3389] Adjust tilemap parallax calculations for offscreen culling
1 parent 991032d commit 0f481ed

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

sandbox/tests/tilemap/tilemap.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
var game = new ex.Engine({
44
width: 600,
55
height: 600,
6-
pixelArt: true
6+
pixelArt: true,
7+
displayMode: ex.DisplayMode.FitScreenAndFill
78
});
89

910
game.showDebug(true);
@@ -24,15 +25,19 @@ var ss = ex.SpriteSheet.fromImageSource({
2425

2526
var tm = new ex.TileMap({
2627
pos: ex.vec(-100, -100),
27-
tileWidth: 16,
28-
tileHeight: 16,
28+
tileWidth: 16 * 20,
29+
tileHeight: 16 * 20,
2930
columns: 40,
3031
rows: 40
3132
});
32-
tm.transform.scale = ex.vec(2, 2);
33+
34+
//tm.addComponent(new ex.ParallaxComponent(ex.vec(.4, .4)));
35+
//tm.transform.scale = ex.vec(2, 2);
3336
// tm.transform.rotation = Math.PI / 4;
3437

3538
var tileSprite = ss.sprites[0];
39+
tileSprite.destSize.width = 320;
40+
tileSprite.destSize.height = 320;
3641

3742
for (var i = 0; i < tm.columns * tm.rows; i++) {
3843
tm.getTileByIndex(i).addGraphic(tileSprite);
@@ -54,8 +59,23 @@ game.input.pointers.primary.on('down', (evt: ex.PointerEvent) => {
5459
game.start(loader).then(async () => {
5560
await game.currentScene.camera.move(ex.Vector.Zero.clone(), 2000, ex.EasingFunctions.EaseInOutCubic);
5661
console.log(tm.getOnScreenTiles());
57-
await game.currentScene.camera.move(new ex.Vector(200, 600), 2000, ex.EasingFunctions.EaseInOutCubic);
62+
await game.currentScene.camera.move(new ex.Vector(800, 600), 4000, ex.EasingFunctions.EaseInOutCubic);
5863
console.log(tm.getOnScreenTiles());
64+
65+
await ex.coroutine(
66+
game,
67+
function* () {
68+
let duration = 2000;
69+
while (duration >= 0) {
70+
const elapsed = yield;
71+
72+
game.currentScene.camera.rotation = ex.lerpAngle(0, Math.PI, ex.RotationType.ShortestPath, ex.clamp(1 - duration / 2000, 0, 1));
73+
74+
duration -= elapsed;
75+
}
76+
}.bind(this)
77+
);
78+
5979
await game.currentScene.camera.zoomOverTime(2, 1000);
6080
console.log(tm.getOnScreenTiles());
6181
await game.currentScene.camera.zoomOverTime(1, 1000);

src/engine/TileMap/TileMap.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,16 @@ export class TileMap extends Entity implements HasNestedPointerEvents {
528528
* Useful if you need to perform specific logic on onscreen tiles
529529
*/
530530
public getOnScreenTiles(): readonly Tile[] {
531-
let worldBounds = this._engine.screen.getWorldBounds();
531+
const worldBounds = this._engine.screen.getWorldBounds();
532+
let parallaxOffset = vec(0, 0);
533+
let bounds = this.transform.coordPlane === CoordPlane.Screen ? this._engine.screen.getScreenBounds() : worldBounds;
532534
const maybeParallax = this.get(ParallaxComponent);
533535
if (maybeParallax && this.isInitialized) {
534-
let pos = this.pos;
535536
const oneMinusFactor = Vector.One.sub(maybeParallax.parallaxFactor);
536-
const parallaxOffset = this._engine.currentScene.camera.pos.scale(oneMinusFactor);
537-
pos = pos.sub(parallaxOffset);
538-
// adjust world bounds by parallax factor
539-
worldBounds = worldBounds.translate(pos);
537+
parallaxOffset = this._engine.currentScene.camera.pos.scale(oneMinusFactor);
538+
bounds = bounds.translate(parallaxOffset.negate());
540539
}
541540

542-
const bounds = this.transform.coordPlane === CoordPlane.Screen ? this._engine.screen.getScreenBounds() : worldBounds;
543541
const topLeft = this._getTileCoordinates(bounds.topLeft);
544542
const topRight = this._getTileCoordinates(bounds.topRight);
545543
const bottomRight = this._getTileCoordinates(bounds.bottomRight);

0 commit comments

Comments
 (0)