2025-12-28 00:54:03 +00:00
|
|
|
import { expect } from "@esm-bundle/chai";
|
|
|
|
|
import { GameLoop } from "../../../src/core/GameLoop.js";
|
|
|
|
|
import {
|
|
|
|
|
createGameLoopSetup,
|
|
|
|
|
cleanupGameLoop,
|
|
|
|
|
createRunData,
|
|
|
|
|
createMockGameStateManagerForCombat,
|
|
|
|
|
setupCombatUnits,
|
|
|
|
|
cleanupTurnSystem,
|
|
|
|
|
} from "./helpers.js";
|
|
|
|
|
|
|
|
|
|
describe("Core: GameLoop - Combat Movement Execution", function () {
|
|
|
|
|
this.timeout(30000);
|
|
|
|
|
|
|
|
|
|
let gameLoop;
|
|
|
|
|
let container;
|
|
|
|
|
let playerUnit;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
const setup = createGameLoopSetup();
|
|
|
|
|
gameLoop = setup.gameLoop;
|
|
|
|
|
container = setup.container;
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
if (gameLoop.turnSystemAbortController) {
|
|
|
|
|
gameLoop.turnSystemAbortController.abort();
|
|
|
|
|
}
|
2025-12-28 00:54:03 +00:00
|
|
|
gameLoop.stop();
|
|
|
|
|
if (
|
|
|
|
|
gameLoop.turnSystem &&
|
|
|
|
|
typeof gameLoop.turnSystem.reset === "function"
|
|
|
|
|
) {
|
|
|
|
|
gameLoop.turnSystem.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gameLoop.init(container);
|
2026-01-02 01:57:06 +00:00
|
|
|
const mockGameStateManager = createMockGameStateManagerForCombat();
|
2025-12-28 00:54:03 +00:00
|
|
|
gameLoop.gameStateManager = mockGameStateManager;
|
|
|
|
|
|
|
|
|
|
const runData = createRunData({
|
|
|
|
|
squad: [{ id: "u1", classId: "CLASS_VANGUARD" }],
|
|
|
|
|
});
|
|
|
|
|
await gameLoop.startLevel(runData, { startAnimation: false });
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
gameLoop.updateCombatState = async () => Promise.resolve();
|
|
|
|
|
|
2025-12-28 00:54:03 +00:00
|
|
|
const units = setupCombatUnits(gameLoop);
|
|
|
|
|
playerUnit = units.playerUnit;
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
|
// Ensure combat is ended before cleanup
|
|
|
|
|
if (
|
|
|
|
|
gameLoop.turnSystem &&
|
|
|
|
|
gameLoop.turnSystem.phase !== "INIT" &&
|
|
|
|
|
gameLoop.turnSystem.phase !== "COMBAT_END"
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
gameLoop.turnSystem.endCombat();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Ignore errors during cleanup
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 00:54:03 +00:00
|
|
|
gameLoop.clearMovementHighlights();
|
|
|
|
|
gameLoop.clearSpawnZoneHighlights();
|
|
|
|
|
cleanupTurnSystem(gameLoop);
|
|
|
|
|
cleanupGameLoop(gameLoop, container);
|
2026-01-02 01:57:06 +00:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
2025-12-28 00:54:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("CoA 9: should move player unit in combat when clicking valid position", async () => {
|
|
|
|
|
playerUnit.chargeMeter = 100;
|
2026-01-02 01:57:06 +00:00
|
|
|
playerUnit.baseStats.speed = 20;
|
2025-12-28 00:54:03 +00:00
|
|
|
|
|
|
|
|
const allUnits = [playerUnit];
|
|
|
|
|
gameLoop.turnSystem.startCombat(allUnits);
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
const activeUnit = gameLoop.turnSystem.getActiveUnit();
|
|
|
|
|
expect(activeUnit).to.equal(playerUnit);
|
2025-12-28 00:54:03 +00:00
|
|
|
|
|
|
|
|
const initialPos = { ...playerUnit.position };
|
|
|
|
|
const targetPos = {
|
|
|
|
|
x: initialPos.x + 1,
|
|
|
|
|
y: initialPos.y,
|
|
|
|
|
z: initialPos.z,
|
|
|
|
|
};
|
|
|
|
|
const initialAP = playerUnit.currentAP;
|
|
|
|
|
|
|
|
|
|
await gameLoop.handleCombatMovement(targetPos);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
playerUnit.position.x !== initialPos.x ||
|
|
|
|
|
playerUnit.position.z !== initialPos.z
|
|
|
|
|
) {
|
|
|
|
|
expect(playerUnit.position.x).to.equal(targetPos.x);
|
|
|
|
|
expect(playerUnit.position.z).to.equal(targetPos.z);
|
|
|
|
|
expect(playerUnit.currentAP).to.be.lessThan(initialAP);
|
|
|
|
|
} else {
|
|
|
|
|
expect(playerUnit.currentAP).to.be.at.most(initialAP);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
it("CoA 10: should not move unit if target is not reachable", async () => {
|
|
|
|
|
playerUnit.chargeMeter = 100;
|
|
|
|
|
playerUnit.baseStats.speed = 20;
|
|
|
|
|
|
|
|
|
|
const allUnits = [playerUnit];
|
|
|
|
|
gameLoop.turnSystem.startCombat(allUnits);
|
|
|
|
|
|
|
|
|
|
expect(gameLoop.turnSystem.getActiveUnit()).to.equal(playerUnit);
|
2025-12-28 00:54:03 +00:00
|
|
|
|
|
|
|
|
const initialPos = { ...playerUnit.position };
|
|
|
|
|
const targetPos = { x: 20, y: 1, z: 20 };
|
|
|
|
|
|
|
|
|
|
gameLoop.isRunning = false;
|
|
|
|
|
gameLoop.inputManager = {
|
|
|
|
|
getCursorPosition: () => targetPos,
|
|
|
|
|
update: () => {},
|
|
|
|
|
isKeyPressed: () => false,
|
|
|
|
|
setCursor: () => {},
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
await gameLoop.handleCombatMovement(targetPos);
|
2025-12-28 00:54:03 +00:00
|
|
|
|
|
|
|
|
expect(playerUnit.position.x).to.equal(initialPos.x);
|
|
|
|
|
expect(playerUnit.position.z).to.equal(initialPos.z);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
it("CoA 11: should not move unit if not enough AP", async () => {
|
|
|
|
|
playerUnit.chargeMeter = 100;
|
|
|
|
|
playerUnit.baseStats.speed = 20;
|
|
|
|
|
|
|
|
|
|
const allUnits = [playerUnit];
|
|
|
|
|
gameLoop.turnSystem.startCombat(allUnits);
|
|
|
|
|
|
|
|
|
|
expect(gameLoop.turnSystem.getActiveUnit()).to.equal(playerUnit);
|
2025-12-28 00:54:03 +00:00
|
|
|
|
|
|
|
|
playerUnit.currentAP = 0;
|
|
|
|
|
const initialPos = { ...playerUnit.position };
|
|
|
|
|
const targetPos = { x: 6, y: 1, z: 5 };
|
|
|
|
|
|
|
|
|
|
gameLoop.isRunning = false;
|
|
|
|
|
gameLoop.inputManager = {
|
|
|
|
|
getCursorPosition: () => targetPos,
|
|
|
|
|
update: () => {},
|
|
|
|
|
isKeyPressed: () => false,
|
|
|
|
|
setCursor: () => {},
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-02 01:57:06 +00:00
|
|
|
await gameLoop.handleCombatMovement(targetPos);
|
2025-12-28 00:54:03 +00:00
|
|
|
expect(playerUnit.position.x).to.equal(initialPos.x);
|
|
|
|
|
});
|
|
|
|
|
});
|