Skip to content

Commit 24651f0

Browse files
committed
fix: update player intro facing logic to use spawn room center instead of world center
1 parent ed19c99 commit 24651f0

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

src/GameScene.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,25 @@ export class GameScene extends Phaser.Scene {
648648
this.accumulator = 0;
649649
this.freezeTimer = 0;
650650
this.syncCurrentRoomToPoint(this.spawnX, this.spawnY);
651+
const spawnRoom = findRoomAtPoint(this.rooms, this.spawnX, this.spawnY) ?? this.currentRoom;
652+
const facingCenterX = spawnRoom.bounds.x + spawnRoom.bounds.w * 0.5;
651653
if (introType === "respawn") {
652-
const respawnRoom = findRoomAtPoint(this.rooms, this.spawnX, this.spawnY) ?? this.currentRoom;
653654
const clampedSource = clampRespawnSource(sourceX, sourceY, {
654-
left: respawnRoom.bounds.x,
655-
right: respawnRoom.bounds.x + respawnRoom.bounds.w,
656-
top: respawnRoom.bounds.y,
657-
bottom: respawnRoom.bounds.y + respawnRoom.bounds.h,
655+
left: spawnRoom.bounds.x,
656+
right: spawnRoom.bounds.x + spawnRoom.bounds.w,
657+
top: spawnRoom.bounds.y,
658+
bottom: spawnRoom.bounds.y + spawnRoom.bounds.h,
658659
});
659660
this.player.reviveAt(this.spawnX, this.spawnY, {
660661
type: "respawn",
661662
sourceX: clampedSource.x,
662663
sourceY: clampedSource.y,
664+
facingCenterX,
665+
});
666+
} else if (introType === "start") {
667+
this.player.reviveAt(this.spawnX, this.spawnY, {
668+
type: "start",
669+
facingCenterX,
663670
});
664671
} else {
665672
this.player.reviveAt(this.spawnX, this.spawnY, introType);

src/player/Player.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ export class Player extends Actor {
524524
const introSpec: PlayerIntroSpec = typeof intro === "string" ? { type: intro } : intro;
525525
this.resetStateAt(x, y);
526526
this.syncStateAfterExternalMove();
527-
this.alignFacingForIntro(x, introSpec.type);
527+
this.alignFacingForIntro(x, introSpec);
528528

529529
if (!isActivePlayerIntroSpec(introSpec)) {
530530
return;
@@ -655,13 +655,14 @@ export class Player extends Actor {
655655
this.introSourceY = null;
656656
}
657657

658-
private alignFacingForIntro(x: number, introType: PlayerIntroType): void {
659-
if (introType === "none") {
658+
private alignFacingForIntro(x: number, introSpec: PlayerIntroSpec): void {
659+
if (introSpec.type === "none") {
660660
return;
661661
}
662662

663-
const worldCenterX = (this.world.cols * WORLD.tile) * 0.5;
664-
this.facing = x > worldCenterX ? -1 : 1;
663+
const defaultCenterX = (this.world.cols * WORLD.tile) * 0.5;
664+
const compareCenterX = introSpec.facingCenterX ?? defaultCenterX;
665+
this.facing = x > compareCenterX ? -1 : 1;
665666
this.lastAim = { x: this.facing, y: 0 };
666667
}
667668

src/player/intro.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface PlayerIntroSpec {
1212
type: PlayerIntroType;
1313
sourceX?: number;
1414
sourceY?: number;
15+
facingCenterX?: number;
1516
duration?: number;
1617
}
1718

tests/model/player-intro.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ describe("Player intro lifecycle", () => {
5858
expect(snapshot.facing).toBe(1);
5959
});
6060

61+
test("intro facing uses the spawn room center instead of the global world center", () => {
62+
const specs: Parameters<typeof buildWorld>[0] = [];
63+
withFloor(specs);
64+
const world = buildWorld(specs);
65+
const player = createPlayer(world, 300, 20 * WORLD.tile);
66+
67+
player.reviveAt(300, 20 * WORLD.tile, {
68+
type: "respawn",
69+
sourceX: 260,
70+
sourceY: 120,
71+
facingCenterX: 340,
72+
});
73+
74+
const snapshot = player.getSnapshot();
75+
expect(snapshot.state).toBe("intro_respawn");
76+
expect(snapshot.facing).toBe(1);
77+
});
78+
6179
test("intro type none revives directly into normal control without intro state", () => {
6280
const specs: Parameters<typeof buildWorld>[0] = [];
6381
withFloor(specs);

0 commit comments

Comments
 (0)