This commit is contained in:
2026-05-15 20:11:19 +02:00
parent 1722842e0a
commit c9f0b5d704
6 changed files with 195 additions and 40 deletions

View File

@@ -2,6 +2,9 @@ import Phaser from "phaser";
import { Player } from "../entities/Player";
import { Exit } from "../entities/Exit";
import { Platform } from "../entities/Platform";
import { ObstaclePlatformDisappearing } from "../entities/ObstaclePlatformDisappearing";
import { ObstacleSpikeNormal } from "../entities/ObstacleSpikeNormal";
import { ObstacleSpikeTricky } from "../entities/ObstacleSpikeTricky";
import { LEVELS } from "../levels/levels";
import { LevelLoader } from "../levels/LevelLoader";
@@ -17,7 +20,11 @@ export class GameScene extends Phaser.Scene {
private player!: Player;
private exit!: Exit;
private platforms: Platform[] = [];
private disappearing: ObstaclePlatformDisappearing[] = [];
private spikes: ObstacleSpikeNormal[] = [];
private trickySpikes: ObstacleSpikeTricky[] = [];
private levelCompleted = false;
private dying = false;
private hudText!: Phaser.GameObjects.Text;
private centerText!: Phaser.GameObjects.Text;
@@ -28,7 +35,11 @@ export class GameScene extends Phaser.Scene {
init(data: SceneData): void {
this.levelIndex = data.levelIndex ?? 0;
this.levelCompleted = false;
this.dying = false;
this.platforms = [];
this.disappearing = [];
this.spikes = [];
this.trickySpikes = [];
}
create(): void {
@@ -40,9 +51,15 @@ export class GameScene extends Phaser.Scene {
this.player = loaded.player;
this.exit = loaded.exit;
this.platforms = loaded.platforms;
this.disappearing = loaded.disappearing;
this.spikes = loaded.spikes;
this.trickySpikes = loaded.trickySpikes;
this.physics.add.collider(this.player, this.platforms, undefined, Platform.canLand);
this.physics.add.collider(this.player, this.disappearing, undefined, Platform.canLand);
this.physics.add.overlap(this.player, this.exit, () => this.completeLevel());
this.physics.add.overlap(this.player, this.spikes, () => this.die());
this.physics.add.overlap(this.player, this.trickySpikes, () => this.die());
this.hudText = this.add.text(
12,
@@ -63,16 +80,23 @@ export class GameScene extends Phaser.Scene {
}
override update(): void {
if (this.levelCompleted) return;
if (this.levelCompleted || this.dying) return;
this.player.update();
for (const platform of this.disappearing) {
platform.react(this.player.x, this.player.y);
}
for (const spike of this.trickySpikes) {
spike.react(this.player.x);
}
if (this.player.y > this.scale.height + GameScene.FALL_LIMIT_PADDING) {
this.restartLevel();
this.die();
}
}
private completeLevel(): void {
if (this.levelCompleted) return;
if (this.levelCompleted || this.dying) return;
this.levelCompleted = true;
this.player.freeze();
@@ -88,7 +112,10 @@ export class GameScene extends Phaser.Scene {
});
}
private restartLevel(): void {
private die(): void {
if (this.levelCompleted || this.dying) return;
this.dying = true;
this.player.freeze();
this.scene.restart({ levelIndex: this.levelIndex });
}
}