Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion frontend/src/gameplay/lava.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Phaser from "phaser";
import type { LavaZone } from "@shared";
import { movingWallSegments } from "@shared";
import type { LavaZone, MovingLavaWall, Rect } from "@shared";

/**
* copied from color.ts a bit...
Expand All @@ -25,3 +26,53 @@ export function findLavaZone(
return (cursor.x - cx) ** 2 + (cursor.y - cy) ** 2 < radius ** 2;
});
}

export interface MovingWallRuntime {
def: MovingLavaWall;
rects: [Phaser.GameObjects.Rectangle, Phaser.GameObjects.Rectangle];
}

export function buildMovingLavaWalls(
scene: Phaser.Scene,
walls: MovingLavaWall[],
): MovingWallRuntime[] {
return walls.map((def) => ({
def,
rects: [
scene.add.rectangle(0, 0, 0, 0, 0xff0000, 0.85).setOrigin(0, 0).setDepth(1),
scene.add.rectangle(0, 0, 0, 0, 0xff0000, 0.85).setOrigin(0, 0).setDepth(1),
],
}));
}

export function updateMovingLavaWalls(
runtimes: MovingWallRuntime[],
nowMs: number,
roomLeft: number,
roomRight: number,
) {
for (const runtime of runtimes) {
const segments = movingWallSegments(runtime.def, nowMs, roomLeft, roomRight);
segments.forEach((segment: Rect, i) => {
runtime.rects[i].setPosition(segment.x, segment.y).setSize(segment.width, segment.height);
});
}
}

export function findMovingLavaHit(
cursor: { x: number; y: number },
radius: number,
walls: MovingLavaWall[],
nowMs: number,
roomLeft: number,
roomRight: number,
): MovingLavaWall | undefined {
return walls.find((wall) => {
const segments = movingWallSegments(wall, nowMs, roomLeft, roomRight);
return segments.some((segment) => {
const cx = Phaser.Math.Clamp(cursor.x, segment.x, segment.x + segment.width);
const cy = Phaser.Math.Clamp(cursor.y, segment.y, segment.y + segment.height);
return (cursor.x - cx) ** 2 + (cursor.y - cy) ** 2 < radius ** 2;
});
});
}
36 changes: 35 additions & 1 deletion frontend/src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
BUTTON,
COLOR_STATIONS,
LAVA_ZONES,
MOVING_LAVA_WALLS,
MOVING_WALL_ROOM_LEFT,
MOVING_WALL_ROOM_RIGHT,
OBSTACLES,
PLAYER_RADIUS,
SPAWN_POINT,
Expand All @@ -25,7 +28,14 @@ import {
updateCollectorProgress,
} from "../gameplay/levelView";
import type { ButtonRuntime, DoorRuntime, PlateRuntime } from "../gameplay/levelView";
import { buildLavaZones, findLavaZone } from "../gameplay/lava";
import {
buildLavaZones,
buildMovingLavaWalls,
findLavaZone,
findMovingLavaHit,
updateMovingLavaWalls,
} from "../gameplay/lava";
import type { MovingWallRuntime } from "../gameplay/lava";
import { SLIDE_DEFAULT_SPEED, SLIDE_MAX_MS, buildSlides, findSlide } from "../gameplay/slide";
import { buildWorldTexts } from "../gameplay/worldText";
import { FONT_HAND, INK_SOFT_CSS } from "../gameplay/palette";
Expand All @@ -49,6 +59,7 @@ export class GameScene extends Phaser.Scene {
private status!: Phaser.GameObjects.Text;
private doors: DoorRuntime[] = [];
private plates: PlateRuntime[] = [];
private movingWalls: MovingWallRuntime[] = [];
private button!: ButtonRuntime;
private multiplayer?: MultiplayerClient;
private renderPlayers!: RenderPlayers;
Expand Down Expand Up @@ -90,6 +101,7 @@ export class GameScene extends Phaser.Scene {
this.doors = doors;
this.plates = plates;
buildLavaZones(this, LAVA_ZONES);
this.movingWalls = buildMovingLavaWalls(this, MOVING_LAVA_WALLS);
buildSlides(this, 1);
buildWorldTexts(this, WORLD_TEXTS);
this.button = buildButton(this);
Expand Down Expand Up @@ -220,6 +232,13 @@ export class GameScene extends Phaser.Scene {

const dt = Math.min(deltaMs / 1000, 0.05);

updateMovingLavaWalls(
this.movingWalls,
Date.now(),
MOVING_WALL_ROOM_LEFT,
MOVING_WALL_ROOM_RIGHT,
);

const solids = this.collectSolids();
const cursorFromX = this.cursor.x;
const cursorFromY = this.cursor.y;
Expand Down Expand Up @@ -255,6 +274,7 @@ export class GameScene extends Phaser.Scene {
moveAndCollide(this.cursor, this.velocity, this.radius, dx, dy, solids);
}
this.updateLava();
this.updateMovingLava();
}

this.renderPlayers.update(dt);
Expand Down Expand Up @@ -446,6 +466,20 @@ export class GameScene extends Phaser.Scene {
this.velocity.set(0, 0);
}

private updateMovingLava() {
const wall = findMovingLavaHit(
this.cursor,
this.radius,
MOVING_LAVA_WALLS,
Date.now(),
MOVING_WALL_ROOM_LEFT,
MOVING_WALL_ROOM_RIGHT,
);
if (!wall) return;
this.cursor.setPosition(wall.teleportTo.x, wall.teleportTo.y);
this.velocity.set(0, 0);
}

private updateColorStation() {
const station = findColorStation(this.cursor, this.radius, COLOR_STATIONS);
if (station?.color === this.activeColorStation) return;
Expand Down
Loading
Loading