obscales
This commit is contained in:
37
src/entities/ObstaclePlatformDisappearing.ts
Normal file
37
src/entities/ObstaclePlatformDisappearing.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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),
|
||||
});
|
||||
}
|
||||
}
|
||||
15
src/entities/ObstacleSpikeNormal.ts
Normal file
15
src/entities/ObstacleSpikeNormal.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import Phaser from "phaser";
|
||||
|
||||
export class ObstacleSpikeNormal extends Phaser.GameObjects.Rectangle {
|
||||
static readonly WIDTH = 40;
|
||||
static readonly HEIGHT = 20;
|
||||
static readonly COLOR = 0xff3333;
|
||||
|
||||
declare body: Phaser.Physics.Arcade.StaticBody;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, y: number) {
|
||||
super(scene, x, y, ObstacleSpikeNormal.WIDTH, ObstacleSpikeNormal.HEIGHT, ObstacleSpikeNormal.COLOR);
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this, true);
|
||||
}
|
||||
}
|
||||
30
src/entities/ObstacleSpikeTricky.ts
Normal file
30
src/entities/ObstacleSpikeTricky.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import Phaser from "phaser";
|
||||
|
||||
export class ObstacleSpikeTricky extends Phaser.GameObjects.Rectangle {
|
||||
static readonly WIDTH = 40;
|
||||
static readonly HEIGHT = 20;
|
||||
static readonly COLOR = 0xff8800;
|
||||
static readonly TRIGGER_DISTANCE = 140;
|
||||
static readonly DISPLACEMENT = 80;
|
||||
|
||||
declare body: Phaser.Physics.Arcade.StaticBody;
|
||||
|
||||
private moved = false;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, y: number) {
|
||||
super(scene, x, y, ObstacleSpikeTricky.WIDTH, ObstacleSpikeTricky.HEIGHT, ObstacleSpikeTricky.COLOR);
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this, true);
|
||||
}
|
||||
|
||||
react(playerX: number): void {
|
||||
if (this.moved) return;
|
||||
const dx = this.x - playerX;
|
||||
if (Math.abs(dx) < ObstacleSpikeTricky.TRIGGER_DISTANCE) {
|
||||
const direction = Math.sign(dx) || 1;
|
||||
this.x += direction * ObstacleSpikeTricky.DISPLACEMENT;
|
||||
this.body.updateFromGameObject();
|
||||
this.moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user