feat: Add scene with track loop and mine cart
This commit is contained in:
62
src/engine/systems/MineCartSystem.ts
Normal file
62
src/engine/systems/MineCartSystem.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import Phaser from "phaser";
|
||||
import { GAME_CONFIG } from "../../core/config/GameConfig";
|
||||
import { TrackDirection } from "./TrackSystem";
|
||||
|
||||
export interface MineCartState {
|
||||
x: number;
|
||||
y: number;
|
||||
facing: { dx: number, dy: number };
|
||||
}
|
||||
|
||||
export class MineCartSystem {
|
||||
static updateOrientation(sprite: Phaser.GameObjects.Sprite, dx: number, dy: number, _connections: TrackDirection) {
|
||||
const { mineCarts } = GAME_CONFIG.rendering;
|
||||
|
||||
// Horizontal movement
|
||||
if (dx !== 0 && dy === 0) {
|
||||
sprite.setFrame(mineCarts.horizontal);
|
||||
sprite.setFlipX(dx < 0);
|
||||
sprite.setAngle(0);
|
||||
}
|
||||
// Vertical movement
|
||||
else if (dy !== 0 && dx === 0) {
|
||||
sprite.setFrame(mineCarts.vertical);
|
||||
sprite.setFlipY(false);
|
||||
sprite.setAngle(0);
|
||||
}
|
||||
// Turning (Corner case)
|
||||
else {
|
||||
sprite.setFrame(mineCarts.turning);
|
||||
// Logic for 56 (turned from right to down by default)
|
||||
// We need to rotate/flip to match the actual turn.
|
||||
// This is a bit complex without seeing the sprite, but we'll approximate:
|
||||
if (dx > 0 && dy > 0) sprite.setAngle(0); // Right to Down
|
||||
if (dx < 0 && dy < 0) sprite.setAngle(180); // Left to Up
|
||||
if (dx > 0 && dy < 0) sprite.setAngle(-90); // Right to Up
|
||||
if (dx < 0 && dy > 0) sprite.setAngle(90); // Left to Down
|
||||
}
|
||||
}
|
||||
|
||||
static getNextPosition(current: { x: number, y: number }, dx: number, dy: number, isTrack: (x: number, y: number) => boolean): { x: number, y: number, dx: number, dy: number } | null {
|
||||
const nextX = current.x + dx;
|
||||
const nextY = current.y + dy;
|
||||
|
||||
if (isTrack(nextX, nextY)) {
|
||||
return { x: nextX, y: nextY, dx, dy };
|
||||
}
|
||||
|
||||
// Try turning if blocked
|
||||
const possibleTurns = [
|
||||
{ tdx: dy, tdy: -dx }, // Left turn
|
||||
{ tdx: -dy, tdy: dx } // Right turn
|
||||
];
|
||||
|
||||
for (const turn of possibleTurns) {
|
||||
if (isTrack(current.x + turn.tdx, current.y + turn.tdy)) {
|
||||
return { x: current.x + turn.tdx, y: current.y + turn.tdy, dx: turn.tdx, dy: turn.tdy };
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
45
src/engine/systems/TrackSystem.ts
Normal file
45
src/engine/systems/TrackSystem.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { GAME_CONFIG } from "../../core/config/GameConfig";
|
||||
|
||||
export const TrackDirection = {
|
||||
NONE: 0,
|
||||
NORTH: 1 << 0,
|
||||
SOUTH: 1 << 1,
|
||||
EAST: 1 << 2,
|
||||
WEST: 1 << 3
|
||||
} as const;
|
||||
|
||||
export type TrackDirection = number;
|
||||
|
||||
export class TrackSystem {
|
||||
static getTrackFrame(connections: TrackDirection): number {
|
||||
const { tracks } = GAME_CONFIG.rendering;
|
||||
|
||||
// Dead Ends
|
||||
if (connections === TrackDirection.SOUTH) return tracks.endTop;
|
||||
if (connections === TrackDirection.NORTH) return tracks.endBottom;
|
||||
if (connections === TrackDirection.EAST) return tracks.endLeft;
|
||||
if (connections === TrackDirection.WEST) return tracks.endRight;
|
||||
|
||||
// Straights
|
||||
if (connections === (TrackDirection.NORTH | TrackDirection.SOUTH)) return tracks.vertical;
|
||||
if (connections === (TrackDirection.EAST | TrackDirection.WEST)) return tracks.horizontal;
|
||||
|
||||
// Corners
|
||||
if (connections === (TrackDirection.NORTH | TrackDirection.EAST)) return tracks.cornerNE;
|
||||
if (connections === (TrackDirection.SOUTH | TrackDirection.EAST)) return tracks.cornerSE;
|
||||
if (connections === (TrackDirection.SOUTH | TrackDirection.WEST)) return tracks.cornerSW;
|
||||
if (connections === (TrackDirection.NORTH | TrackDirection.WEST)) return tracks.cornerNW;
|
||||
|
||||
// Fallback to horizontal
|
||||
return tracks.horizontal;
|
||||
}
|
||||
|
||||
static getConnectionsFromNeighbors(x: number, y: number, isTrack: (x: number, y: number) => boolean): TrackDirection {
|
||||
let connections = TrackDirection.NONE;
|
||||
if (isTrack(x, y - 1)) connections |= TrackDirection.NORTH;
|
||||
if (isTrack(x, y + 1)) connections |= TrackDirection.SOUTH;
|
||||
if (isTrack(x + 1, y)) connections |= TrackDirection.EAST;
|
||||
if (isTrack(x - 1, y)) connections |= TrackDirection.WEST;
|
||||
return connections;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user