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 tracks: Set<string> = new Set();
|
||||||
private trackSprites: Phaser.GameObjects.Sprite[] = [];
|
private trackSprites: Phaser.GameObjects.Sprite[] = [];
|
||||||
private cart?: Phaser.GameObjects.Sprite;
|
private carts: Phaser.GameObjects.Sprite[] = [];
|
||||||
private cartPos = { x: 0, y: 0 };
|
private cartStates: { pos: { x: number, y: number }, dir: { dx: number, dy: number } }[] = [];
|
||||||
private cartDir = { dx: 1, dy: 0 };
|
|
||||||
private moveTimer?: Phaser.Time.TimerEvent;
|
private moveTimer?: Phaser.Time.TimerEvent;
|
||||||
|
private isPaused = false;
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
const { width, height } = this.scale;
|
const { width, height } = this.scale;
|
||||||
@@ -44,20 +44,26 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
|
|
||||||
regenBtn.setAlpha(0.8);
|
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
|
// Initial generation
|
||||||
this.generateRandomLoop();
|
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
|
// Movement Loop
|
||||||
this.moveTimer = this.time.addEvent({
|
this.moveTimer = this.time.addEvent({
|
||||||
delay: 500,
|
delay: 500,
|
||||||
callback: this.moveCart,
|
callback: this.moveCarts,
|
||||||
callbackScope: this,
|
callbackScope: this,
|
||||||
loop: true
|
loop: true
|
||||||
});
|
});
|
||||||
@@ -73,7 +79,6 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
this.trackSprites = [];
|
this.trackSprites = [];
|
||||||
this.tracks.clear();
|
this.tracks.clear();
|
||||||
|
|
||||||
// Start with a 6x6 rectangle
|
|
||||||
const startX = 6;
|
const startX = 6;
|
||||||
const startY = 6;
|
const startY = 6;
|
||||||
const initialSize = 15;
|
const initialSize = 15;
|
||||||
@@ -93,7 +98,7 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.renderTracks();
|
this.renderTracks();
|
||||||
this.resetCart();
|
this.resetCarts();
|
||||||
}
|
}
|
||||||
|
|
||||||
private expandLoop() {
|
private expandLoop() {
|
||||||
@@ -120,7 +125,6 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
tempTracks.delete(`${x},${y}`);
|
tempTracks.delete(`${x},${y}`);
|
||||||
newTiles.forEach(k => tempTracks.add(k));
|
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 affected = [...newTiles, `${x-1},${y}`, `${x+1},${y}`];
|
||||||
const isValid = affected.every(posKey => {
|
const isValid = affected.every(posKey => {
|
||||||
const [px, py] = posKey.split(',').map(Number);
|
const [px, py] = posKey.split(',').map(Number);
|
||||||
@@ -160,24 +164,50 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private resetCart() {
|
private resetCarts() {
|
||||||
if (!this.cart) return;
|
// Clear existing carts
|
||||||
const first = Array.from(this.tracks)[0];
|
this.carts.forEach(c => c.destroy());
|
||||||
const [x, y] = first.split(',').map(Number);
|
this.carts = [];
|
||||||
this.cartPos = { x, y };
|
this.cartStates = [];
|
||||||
this.cart.setPosition(x * 32 + 16, y * 32 + 16);
|
|
||||||
|
|
||||||
// Find a valid initial direction
|
if (this.tracks.size === 0) return;
|
||||||
const neighbors = [
|
|
||||||
{ dx: 1, dy: 0 }, { dx: -1, dy: 0 },
|
// Find a path sequence for 5 carts
|
||||||
{ dx: 0, dy: 1 }, { dx: 0, dy: -1 }
|
const path: {x: number, y: number, dx: number, dy: number}[] = [];
|
||||||
];
|
let firstKey = Array.from(this.tracks)[0];
|
||||||
for (const n of neighbors) {
|
let curr = firstKey.split(',').map(Number);
|
||||||
if (this.tracks.has(`${x + n.dx},${y + n.dy}`)) {
|
let dx = 1, dy = 0;
|
||||||
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;
|
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() {
|
private renderTracks() {
|
||||||
@@ -190,29 +220,34 @@ export class TrackExplorationScene extends Phaser.Scene {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private moveCart() {
|
private moveCarts() {
|
||||||
if (!this.cart || this.tracks.size === 0) return;
|
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];
|
||||||
|
|
||||||
|
const next = MineCartSystem.getNextPosition(state.pos, state.dir.dx, state.dir.dy, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
||||||
|
|
||||||
if (next) {
|
if (next) {
|
||||||
this.cartPos = { x: next.x, y: next.y };
|
state.pos = { x: next.x, y: next.y };
|
||||||
this.cartDir = { dx: next.dx, dy: next.dy };
|
state.dir = { dx: next.dx, dy: next.dy };
|
||||||
|
|
||||||
const tx = next.x * 32 + 16;
|
const tx = next.x * 32 + 16;
|
||||||
const ty = next.y * 32 + 16;
|
const ty = next.y * 32 + 16;
|
||||||
|
|
||||||
this.tweens.add({
|
this.tweens.add({
|
||||||
targets: this.cart,
|
targets: cart,
|
||||||
x: tx,
|
x: tx,
|
||||||
y: ty,
|
y: ty,
|
||||||
duration: 400,
|
duration: 400,
|
||||||
ease: 'Linear',
|
ease: 'Linear',
|
||||||
onStart: () => {
|
onStart: () => {
|
||||||
const connections = TrackSystem.getConnectionsFromNeighbors(next.x, next.y, (nx: number, ny: number) => this.tracks.has(`${nx},${ny}`));
|
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);
|
MineCartSystem.updateOrientation(cart, next.dx, next.dy, connections);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user