Add a chain of carts and a start/stop button
This commit is contained in:
@@ -9,10 +9,10 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
|
||||
private tracks: Set<string> = new Set();
|
||||
private trackSprites: Phaser.GameObjects.Sprite[] = [];
|
||||
private cart?: Phaser.GameObjects.Sprite;
|
||||
private cartPos = { x: 0, y: 0 };
|
||||
private cartDir = { dx: 1, dy: 0 };
|
||||
private carts: Phaser.GameObjects.Sprite[] = [];
|
||||
private cartStates: { pos: { x: number, y: number }, dir: { dx: number, dy: number } }[] = [];
|
||||
private moveTimer?: Phaser.Time.TimerEvent;
|
||||
private isPaused = false;
|
||||
|
||||
create() {
|
||||
const { width, height } = this.scale;
|
||||
@@ -44,20 +44,26 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
|
||||
regenBtn.setAlpha(0.8);
|
||||
|
||||
const playPauseBtn = this.add.text(width / 2, height - 110, "STOP MOVEMENT", {
|
||||
fontSize: "24px",
|
||||
color: "#ff8800"
|
||||
}).setOrigin(0.5)
|
||||
.setInteractive({ useHandCursor: true })
|
||||
.on("pointerdown", () => {
|
||||
this.isPaused = !this.isPaused;
|
||||
playPauseBtn.setText(this.isPaused ? "START MOVEMENT" : "STOP MOVEMENT");
|
||||
playPauseBtn.setColor(this.isPaused ? "#00ff88" : "#ff8800");
|
||||
});
|
||||
|
||||
playPauseBtn.setAlpha(0.8);
|
||||
|
||||
// Initial generation
|
||||
this.generateRandomLoop();
|
||||
|
||||
// Add Mine Cart
|
||||
const { mineCarts } = GAME_CONFIG.rendering;
|
||||
this.cart = this.add.sprite(0, 0, "kennys_dungeon", mineCarts.horizontal);
|
||||
this.cart.setScale(2);
|
||||
this.cart.setDepth(10);
|
||||
this.resetCart();
|
||||
|
||||
// Movement Loop
|
||||
this.moveTimer = this.time.addEvent({
|
||||
delay: 500,
|
||||
callback: this.moveCart,
|
||||
callback: this.moveCarts,
|
||||
callbackScope: this,
|
||||
loop: true
|
||||
});
|
||||
@@ -73,7 +79,6 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
this.trackSprites = [];
|
||||
this.tracks.clear();
|
||||
|
||||
// Start with a 6x6 rectangle
|
||||
const startX = 6;
|
||||
const startY = 6;
|
||||
const initialSize = 15;
|
||||
@@ -93,7 +98,7 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
}
|
||||
|
||||
this.renderTracks();
|
||||
this.resetCart();
|
||||
this.resetCarts();
|
||||
}
|
||||
|
||||
private expandLoop() {
|
||||
@@ -120,7 +125,6 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
tempTracks.delete(`${x},${y}`);
|
||||
newTiles.forEach(k => tempTracks.add(k));
|
||||
|
||||
// Check rule: Every modified/new tile must have exactly 2 neighbors
|
||||
const affected = [...newTiles, `${x-1},${y}`, `${x+1},${y}`];
|
||||
const isValid = affected.every(posKey => {
|
||||
const [px, py] = posKey.split(',').map(Number);
|
||||
@@ -160,24 +164,50 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
}
|
||||
}
|
||||
|
||||
private resetCart() {
|
||||
if (!this.cart) return;
|
||||
const first = Array.from(this.tracks)[0];
|
||||
const [x, y] = first.split(',').map(Number);
|
||||
this.cartPos = { x, y };
|
||||
this.cart.setPosition(x * 32 + 16, y * 32 + 16);
|
||||
private resetCarts() {
|
||||
// Clear existing carts
|
||||
this.carts.forEach(c => c.destroy());
|
||||
this.carts = [];
|
||||
this.cartStates = [];
|
||||
|
||||
if (this.tracks.size === 0) return;
|
||||
|
||||
// Find a path sequence for 5 carts
|
||||
const path: {x: number, y: number, dx: number, dy: number}[] = [];
|
||||
let firstKey = Array.from(this.tracks)[0];
|
||||
let curr = firstKey.split(',').map(Number);
|
||||
let dx = 1, dy = 0;
|
||||
|
||||
// Find a valid initial direction
|
||||
const neighbors = [
|
||||
{ dx: 1, dy: 0 }, { dx: -1, dy: 0 },
|
||||
{ dx: 0, dy: 1 }, { dx: 0, dy: -1 }
|
||||
];
|
||||
for (const n of neighbors) {
|
||||
if (this.tracks.has(`${x + n.dx},${y + n.dy}`)) {
|
||||
this.cartDir = { dx: n.dx, dy: n.dy };
|
||||
const neighbors = [{dx: 1, dy: 0}, {dx: -1, dy: 0}, {dx: 0, dy: 1}, {dx: 0, dy: -1}];
|
||||
for(const n of neighbors) {
|
||||
if (this.tracks.has(`${curr[0] + n.dx},${curr[1] + n.dy}`)) {
|
||||
dx = n.dx; dy = n.dy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let tx = curr[0], ty = curr[1];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
path.push({ x: tx, y: ty, dx, dy });
|
||||
const next = MineCartSystem.getNextPosition({x: tx, y: ty}, dx, dy, (nx, ny) => this.tracks.has(`${nx},${ny}`));
|
||||
if (next) {
|
||||
tx = next.x; ty = next.y; dx = next.dx; dy = next.dy;
|
||||
}
|
||||
}
|
||||
|
||||
const { mineCarts } = GAME_CONFIG.rendering;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const p = path[i];
|
||||
const sprite = this.add.sprite(p.x * 32 + 16, p.y * 32 + 16, "kennys_dungeon", mineCarts.horizontal);
|
||||
sprite.setScale(2);
|
||||
sprite.setDepth(10);
|
||||
|
||||
const connections = TrackSystem.getConnectionsFromNeighbors(p.x, p.y, (nx, ny) => this.tracks.has(`${nx},${ny}`));
|
||||
MineCartSystem.updateOrientation(sprite, p.dx, p.dy, connections);
|
||||
|
||||
this.carts.push(sprite);
|
||||
this.cartStates.push({ pos: { x: p.x, y: p.y }, dir: { dx: p.dx, dy: p.dy } });
|
||||
}
|
||||
}
|
||||
|
||||
private renderTracks() {
|
||||
@@ -190,29 +220,34 @@ export class TrackExplorationScene extends Phaser.Scene {
|
||||
});
|
||||
}
|
||||
|
||||
private moveCart() {
|
||||
if (!this.cart || this.tracks.size === 0) return;
|
||||
private moveCarts() {
|
||||
if (this.isPaused || this.carts.length === 0 || this.tracks.size === 0) return;
|
||||
|
||||
const next = MineCartSystem.getNextPosition(this.cartPos, this.cartDir.dx, this.cartDir.dy, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
||||
for (let i = 0; i < this.carts.length; i++) {
|
||||
const cart = this.carts[i];
|
||||
const state = this.cartStates[i];
|
||||
|
||||
if (next) {
|
||||
this.cartPos = { x: next.x, y: next.y };
|
||||
this.cartDir = { dx: next.dx, dy: next.dy };
|
||||
const next = MineCartSystem.getNextPosition(state.pos, state.dir.dx, state.dir.dy, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
||||
|
||||
const tx = next.x * 32 + 16;
|
||||
const ty = next.y * 32 + 16;
|
||||
if (next) {
|
||||
state.pos = { x: next.x, y: next.y };
|
||||
state.dir = { dx: next.dx, dy: next.dy };
|
||||
|
||||
this.tweens.add({
|
||||
targets: this.cart,
|
||||
x: tx,
|
||||
y: ty,
|
||||
duration: 400,
|
||||
ease: 'Linear',
|
||||
onStart: () => {
|
||||
const connections = TrackSystem.getConnectionsFromNeighbors(next.x, next.y, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
||||
MineCartSystem.updateOrientation(this.cart!, next.dx, next.dy, connections);
|
||||
const tx = next.x * 32 + 16;
|
||||
const ty = next.y * 32 + 16;
|
||||
|
||||
this.tweens.add({
|
||||
targets: cart,
|
||||
x: tx,
|
||||
y: ty,
|
||||
duration: 400,
|
||||
ease: 'Linear',
|
||||
onStart: () => {
|
||||
const connections = TrackSystem.getConnectionsFromNeighbors(next.x, next.y, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
||||
MineCartSystem.updateOrientation(cart, next.dx, next.dy, connections);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user