one-way platforms

This commit is contained in:
2026-05-15 19:27:18 +02:00
parent 5d869b12f1
commit 1722842e0a
2 changed files with 14 additions and 1 deletions

View File

@@ -10,4 +10,17 @@ export class Platform extends Phaser.GameObjects.Rectangle {
scene.add.existing(this); scene.add.existing(this);
scene.physics.add.existing(this, true); scene.physics.add.existing(this, true);
} }
// Phaser collider processCallback: only collide when the mover is descending onto the top.
static canLand(
mover: Phaser.Types.Physics.Arcade.GameObjectWithBody,
platform: Phaser.Types.Physics.Arcade.GameObjectWithBody,
): boolean {
const moverBody = mover.body as Phaser.Physics.Arcade.Body;
const platBody = platform.body as Phaser.Physics.Arcade.StaticBody;
return (
moverBody.velocity.y >= 0 &&
moverBody.bottom - moverBody.deltaY() <= platBody.top
);
}
} }

View File

@@ -41,7 +41,7 @@ export class GameScene extends Phaser.Scene {
this.exit = loaded.exit; this.exit = loaded.exit;
this.platforms = loaded.platforms; this.platforms = loaded.platforms;
this.physics.add.collider(this.player, this.platforms); this.physics.add.collider(this.player, this.platforms, undefined, Platform.canLand);
this.physics.add.overlap(this.player, this.exit, () => this.completeLevel()); this.physics.add.overlap(this.player, this.exit, () => this.completeLevel());
this.hudText = this.add.text( this.hudText = this.add.text(