Files
trickster-tiles/src/entities/ObstaclePlatformDisappearing.ts
2026-05-15 20:11:19 +02:00

38 lines
1.1 KiB
TypeScript

import Phaser from "phaser";
export class ObstaclePlatformDisappearing extends Phaser.GameObjects.Rectangle {
static readonly COLOR = 0x9966cc;
static readonly TRIGGER_DISTANCE = 110;
static readonly FADE_MS = 200;
declare body: Phaser.Physics.Arcade.StaticBody;
private vanished = false;
constructor(scene: Phaser.Scene, x: number, y: number, width: number, height: number) {
super(scene, x, y, width, height, ObstaclePlatformDisappearing.COLOR);
scene.add.existing(this);
scene.physics.add.existing(this, true);
}
react(playerX: number, playerY: number): void {
if (this.vanished) return;
const dx = this.x - playerX;
const dy = this.y - playerY;
if (Math.hypot(dx, dy) < ObstaclePlatformDisappearing.TRIGGER_DISTANCE) {
this.vanish();
}
}
private vanish(): void {
this.vanished = true;
this.body.enable = false;
this.scene.tweens.add({
targets: this,
alpha: 0,
duration: ObstaclePlatformDisappearing.FADE_MS,
onComplete: () => this.setVisible(false),
});
}
}