UNPKG

@slippi/slippi-js

Version:
58 lines (55 loc) 2.62 kB
import { GameMode, Frames } from '../types.esm.js'; import { exists } from '../utils/exists.esm.js'; // The Target item's in-game ID const TARGET_ITEM_TYPE_ID = 209; class TargetBreakComputer { constructor() { this.targetBreaks = []; this.isTargetTestGame = false; } setup(settings) { // Reset the state this.targetBreaks = []; this.isTargetTestGame = settings.gameMode === GameMode.TARGET_TEST; } processFrame(frame, allFrames) { if (!this.isTargetTestGame) { return; } handleTargetBreak(allFrames, frame, this.targetBreaks); } fetch() { return this.targetBreaks; } } function handleTargetBreak(frames, frame, targetBreaks) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const currentFrameNumber = frame.frame; const prevFrameNumber = currentFrameNumber - 1; // Add all targets on the first frame if (currentFrameNumber === Frames.FIRST) { const targets = (_c = (_b = (_a = frames[Frames.FIRST]) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.filter((item) => item.typeId === TARGET_ITEM_TYPE_ID)) !== null && _c !== void 0 ? _c : []; targets.forEach((target) => { targetBreaks.push({ spawnId: target.spawnId, frameDestroyed: undefined, positionX: target.positionX, positionY: target.positionY, }); }); } const currentTargets = (_f = (_e = (_d = frames[currentFrameNumber]) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.filter((item) => item.typeId === TARGET_ITEM_TYPE_ID)) !== null && _f !== void 0 ? _f : []; const previousTargets = (_j = (_h = (_g = frames[prevFrameNumber]) === null || _g === void 0 ? void 0 : _g.items) === null || _h === void 0 ? void 0 : _h.filter((item) => item.typeId === TARGET_ITEM_TYPE_ID)) !== null && _j !== void 0 ? _j : []; const currentTargetIds = currentTargets.map((item) => item.spawnId).filter(exists); const previousTargetIds = previousTargets.map((item) => item.spawnId).filter(exists); // Check if any targets were destroyed const brokenTargetIds = previousTargetIds.filter((id) => !currentTargetIds.includes(id)); brokenTargetIds.forEach((id) => { // Update the target break const targetBreak = targetBreaks.find((targetBreak) => targetBreak.spawnId === id); if (targetBreak) { targetBreak.frameDestroyed = currentFrameNumber; } }); } export { TargetBreakComputer };