diff --git a/src/entities/Platform.ts b/src/entities/Platform.ts index 160b8b6..7a6f942 100644 --- a/src/entities/Platform.ts +++ b/src/entities/Platform.ts @@ -10,4 +10,17 @@ export class Platform extends Phaser.GameObjects.Rectangle { scene.add.existing(this); 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 + ); + } } diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index 503a4d0..017d979 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -41,7 +41,7 @@ export class GameScene extends Phaser.Scene { this.exit = loaded.exit; 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.hudText = this.add.text(