63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
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;
|
|
}
|
|
}
|