Compare commits

..

5 Commits

Author SHA1 Message Date
hypercross 113d240f71 refactor: simplify item effect handling in grid inventory
Update `getItemEffects` to directly use `startEffects` from item meta
instead of performing a manual lookup against the effect table. This
is made possible by updating `GameItemMeta` to store the full effect
object (data and stacks) instead of just the effect ID and stack count.
2026-04-21 23:17:19 +08:00
hypercross 0547180074 feat(slay-the-spire-like): add getItemEffects to grid inventory 2026-04-21 23:15:02 +08:00
hypercross d1605a9ee3 feat(samples/slay-the-spire-like): export data module 2026-04-21 23:02:27 +08:00
hypercross 093738cd42 test: expand shape-utils coverage and refactor style 2026-04-21 23:01:26 +08:00
hypercross 97ff61985a refactor(slay-the-spire-like): clean up barrel exports and formatting
- Convert explicit barrel exports to `export *` patterns in several
  systems
- Create a new `utils/index.ts` barrel for the system utils
- Rename `validatePlacement` to `validateShapePlacement` for clarity
- Reformat `shape-collision.ts` to use 2-space indentation and
  consistent
  styling
- Fix import paths in `index.ts`
2026-04-21 23:01:19 +08:00
9 changed files with 501 additions and 502 deletions

View File

@ -3,5 +3,6 @@ export * from "./system/deck";
export * from "./system/encounter"; export * from "./system/encounter";
export * from "./system/grid-inventory"; export * from "./system/grid-inventory";
export * from "./system/map"; export * from "./system/map";
export * from "./system/utils/parse-shape"; export * from "./system/utils";
export * from "./system/types"; export * from "./system/types";
export { default as data } from "./data";

View File

@ -1,7 +1,2 @@
export type { GameCard, GameCardMeta, PlayerDeck, DeckRegions } from './types'; export * from "./factory";
export { export * from "./types";
generateDeckFromInventory,
createCard,
createPlayerDeck,
generateCardId,
} from './factory';

View File

@ -1,24 +1,3 @@
export type { export * from "./types";
CellCoordinate, export * from "./transform";
CellKey,
GridInventory,
InventoryItem,
MutationResult,
PlacementResult,
} from "./types";
export type { GameItemMeta, GameItem } from "./types";
export {
createGridInventory,
flipItem,
getAdjacentItems,
getItemAtCell,
getOccupiedCellSet,
moveItem,
placeItem,
removeItem,
rotateItem,
validatePlacement,
} from "./transform";
export * from "./factory"; export * from "./factory";

View File

@ -1,3 +1,4 @@
import { EffectData } from "../types";
import type { ParsedShape } from "../utils/parse-shape"; import type { ParsedShape } from "../utils/parse-shape";
import type { Transform2D } from "../utils/shape-collision"; import type { Transform2D } from "../utils/shape-collision";
import { import {
@ -10,6 +11,7 @@ import {
} from "../utils/shape-collision"; } from "../utils/shape-collision";
import type { import type {
CellKey, CellKey,
GameItemMeta,
GridInventory, GridInventory,
InventoryItem, InventoryItem,
MutationResult, MutationResult,
@ -255,3 +257,21 @@ export function getAdjacentItems<TMeta>(
return adjacent; return adjacent;
} }
// export type EffectTable = Record<string, { data: EffectData; stacks: number }>;
export function getItemEffects(inv: GridInventory<GameItemMeta>) {
const effects = {} as Record<
string,
Record<string, { data: EffectData; stacks: number }>
>;
for (const item of inv.items.values()) {
if (!item.meta) continue;
const { startEffects } = item.meta;
if (!startEffects) continue;
effects[item.id] = startEffects;
}
return effects;
}

View File

@ -1,4 +1,4 @@
import { ItemData } from "../types"; import { EffectData, ItemData } from "../types";
import type { ParsedShape } from "../utils/parse-shape"; import type { ParsedShape } from "../utils/parse-shape";
import type { Transform2D } from "../utils/shape-collision"; import type { Transform2D } from "../utils/shape-collision";
@ -64,7 +64,7 @@ export interface GameItemMeta {
itemData: ItemData; itemData: ItemData;
shape: ParsedShape; shape: ParsedShape;
consumedUses?: number; consumedUses?: number;
startEffects?: Record<string, number>; startEffects?: Record<string, { data: EffectData; stacks: number }>;
tradePrice?: number; tradePrice?: number;
} }
export type GameItem = InventoryItem<GameItemMeta>; export type GameItem = InventoryItem<GameItemMeta>;

View File

@ -1,24 +1,3 @@
export { MapNodeType, MapLayerType } from "./types"; export * from "./generator";
export type { export * from "./navigation";
MapNode, export * from "./types";
MapLayer,
PointCrawlMap,
MapGenerationConfig,
} from "./types";
export { generatePointCrawlMap } from "./generator";
export {
getNode,
getChildren,
getParents,
hasPath,
findAllPaths,
} from "./generator";
export {
canMoveTo,
moveToNode,
getReachableChildren,
isAtEndNode,
isAtStartNode,
} from "./navigation";

View File

@ -0,0 +1,2 @@
export * from "./parse-shape";
export * from "./shape-collision";

View File

@ -1,4 +1,4 @@
import type { ParsedShape } from './parse-shape'; import type { ParsedShape } from "./parse-shape";
/** /**
* Represents a 2D point in grid coordinates. * Represents a 2D point in grid coordinates.
@ -54,7 +54,7 @@ export function transformPoint(
point: Point2D, point: Point2D,
transform: Transform2D, transform: Transform2D,
shapeWidth: number, shapeWidth: number,
shapeHeight: number shapeHeight: number,
): Point2D { ): Point2D {
let { x, y } = point; let { x, y } = point;
@ -96,10 +96,13 @@ export function transformPoint(
/** /**
* Transforms a shape and returnss its occupied cells in world coordinates. * Transforms a shape and returnss its occupied cells in world coordinates.
*/ */
export function transformShape(shape: ParsedShape, transform: Transform2D): Point2D[] { export function transformShape(
shape: ParsedShape,
transform: Transform2D,
): Point2D[] {
const cells = getOccupiedCells(shape); const cells = getOccupiedCells(shape);
return cells.map(cell => return cells.map((cell) =>
transformPoint(cell, transform, shape.width, shape.height) transformPoint(cell, transform, shape.width, shape.height),
); );
} }
@ -110,12 +113,12 @@ export function checkCollision(
shapeA: ParsedShape, shapeA: ParsedShape,
transformA: Transform2D, transformA: Transform2D,
shapeB: ParsedShape, shapeB: ParsedShape,
transformB: Transform2D transformB: Transform2D,
): boolean { ): boolean {
const cellsA = transformShape(shapeA, transformA); const cellsA = transformShape(shapeA, transformA);
const cellsB = transformShape(shapeB, transformB); const cellsB = transformShape(shapeB, transformB);
const setA = new Set(cellsA.map(c => `${c.x},${c.y}`)); const setA = new Set(cellsA.map((c) => `${c.x},${c.y}`));
for (const cell of cellsB) { for (const cell of cellsB) {
if (setA.has(`${cell.x},${cell.y}`)) { if (setA.has(`${cell.x},${cell.y}`)) {
@ -135,7 +138,7 @@ export function checkCollision(
export function checkBoardCollision( export function checkBoardCollision(
shape: ParsedShape, shape: ParsedShape,
transform: Transform2D, transform: Transform2D,
occupiedCells: Set<string> occupiedCells: Set<string>,
): boolean { ): boolean {
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
@ -159,12 +162,17 @@ export function checkBounds(
shape: ParsedShape, shape: ParsedShape,
transform: Transform2D, transform: Transform2D,
boardWidth: number, boardWidth: number,
boardHeight: number boardHeight: number,
): boolean { ): boolean {
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
for (const cell of cells) { for (const cell of cells) {
if (cell.x < 0 || cell.x >= boardWidth || cell.y < 0 || cell.y >= boardHeight) { if (
cell.x < 0 ||
cell.x >= boardWidth ||
cell.y < 0 ||
cell.y >= boardHeight
) {
return false; return false;
} }
} }
@ -176,19 +184,19 @@ export function checkBounds(
* Validates that a placement is both in bounds and collision-free. * Validates that a placement is both in bounds and collision-free.
* @returns Object with `valid` flag and optional `reason` string * @returns Object with `valid` flag and optional `reason` string
*/ */
export function validatePlacement( export function validateShapePlacement(
shape: ParsedShape, shape: ParsedShape,
transform: Transform2D, transform: Transform2D,
boardWidth: number, boardWidth: number,
boardHeight: number, boardHeight: number,
occupiedCells: Set<string> occupiedCells: Set<string>,
): { valid: true } | { valid: false; reason: string } { ): { valid: true } | { valid: false; reason: string } {
if (!checkBounds(shape, transform, boardWidth, boardHeight)) { if (!checkBounds(shape, transform, boardWidth, boardHeight)) {
return { valid: false, reason: '超出边界' }; return { valid: false, reason: "超出边界" };
} }
if (checkBoardCollision(shape, transform, occupiedCells)) { if (checkBoardCollision(shape, transform, occupiedCells)) {
return { valid: false, reason: '与已有形状重叠' }; return { valid: false, reason: "与已有形状重叠" };
} }
return { valid: true }; return { valid: true };
@ -199,10 +207,13 @@ export function validatePlacement(
* @param current The current transform * @param current The current transform
* @param degrees Degrees to rotate (typically 90, 180, or 270) * @param degrees Degrees to rotate (typically 90, 180, or 270)
*/ */
export function rotateTransform(current: Transform2D, degrees: number): Transform2D { export function rotateTransform(
current: Transform2D,
degrees: number,
): Transform2D {
return { return {
...current, ...current,
rotation: ((current.rotation + degrees) % 360 + 360) % 360, rotation: (((current.rotation + degrees) % 360) + 360) % 360,
}; };
} }

View File

@ -1,18 +1,18 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from "vitest";
import { parseShapeString } from '@/samples/slay-the-spire-like/system/utils/parse-shape'; import { parseShapeString } from "@/samples/slay-the-spire-like/system/utils/parse-shape";
import { import {
checkCollision, checkCollision,
checkBoardCollision, checkBoardCollision,
checkBounds, checkBounds,
validatePlacement, validateShapePlacement,
transformShape, transformShape,
getOccupiedCells, getOccupiedCells,
IDENTITY_TRANSFORM, IDENTITY_TRANSFORM,
} from '@/samples/slay-the-spire-like/system/utils/shape-collision'; } from "@/samples/slay-the-spire-like/system/utils/shape-collision";
describe('parseShapeString', () => { describe("parseShapeString", () => {
it('should parse a single cell with o', () => { it("should parse a single cell with o", () => {
const result = parseShapeString('o'); const result = parseShapeString("o");
expect(result.grid).toEqual([[true]]); expect(result.grid).toEqual([[true]]);
expect(result.width).toBe(1); expect(result.width).toBe(1);
expect(result.height).toBe(1); expect(result.height).toBe(1);
@ -21,8 +21,8 @@ describe('parseShapeString', () => {
expect(result.originY).toBe(0); expect(result.originY).toBe(0);
}); });
it('should parse a horizontal line', () => { it("should parse a horizontal line", () => {
const result = parseShapeString('oee'); const result = parseShapeString("oee");
expect(result.width).toBe(3); expect(result.width).toBe(3);
expect(result.height).toBe(1); expect(result.height).toBe(1);
expect(result.count).toBe(3); expect(result.count).toBe(3);
@ -31,8 +31,8 @@ describe('parseShapeString', () => {
expect(result.originY).toBe(0); expect(result.originY).toBe(0);
}); });
it('should parse a vertical line', () => { it("should parse a vertical line", () => {
const result = parseShapeString('oss'); const result = parseShapeString("oss");
expect(result.width).toBe(1); expect(result.width).toBe(1);
expect(result.height).toBe(3); expect(result.height).toBe(3);
expect(result.count).toBe(3); expect(result.count).toBe(3);
@ -41,8 +41,8 @@ describe('parseShapeString', () => {
expect(result.originY).toBe(0); expect(result.originY).toBe(0);
}); });
it('should parse an L shape', () => { it("should parse an L shape", () => {
const result = parseShapeString('oes'); const result = parseShapeString("oes");
expect(result.width).toBe(2); expect(result.width).toBe(2);
expect(result.height).toBe(2); expect(result.height).toBe(2);
expect(result.count).toBe(3); expect(result.count).toBe(3);
@ -52,47 +52,47 @@ describe('parseShapeString', () => {
]); ]);
}); });
it('should handle return command', () => { it("should handle return command", () => {
const result = parseShapeString('oeerww'); const result = parseShapeString("oeerww");
expect(result.width).toBe(4); expect(result.width).toBe(4);
expect(result.height).toBe(1); expect(result.height).toBe(1);
expect(result.count).toBe(4); expect(result.count).toBe(4);
expect(result.grid).toEqual([[true, true, true, true]]); expect(result.grid).toEqual([[true, true, true, true]]);
}); });
it('should handle case insensitivity', () => { it("should handle case insensitivity", () => {
const resultLower = parseShapeString('oes'); const resultLower = parseShapeString("oes");
const resultUpper = parseShapeString('OES'); const resultUpper = parseShapeString("OES");
expect(resultLower.grid).toEqual(resultUpper.grid); expect(resultLower.grid).toEqual(resultUpper.grid);
expect(resultLower.count).toBe(resultUpper.count); expect(resultLower.count).toBe(resultUpper.count);
}); });
it('should return empty grid for empty input', () => { it("should return empty grid for empty input", () => {
const result = parseShapeString(''); const result = parseShapeString("");
expect(result.grid).toEqual([[]]); expect(result.grid).toEqual([[]]);
expect(result.width).toBe(0); expect(result.width).toBe(0);
expect(result.height).toBe(1); expect(result.height).toBe(1);
expect(result.count).toBe(0); expect(result.count).toBe(0);
}); });
it('should track origin correctly', () => { it("should track origin correctly", () => {
// eeso: e(1,0), e(2,0), s(2,1), o sets origin at (2,1) // eeso: e(1,0), e(2,0), s(2,1), o sets origin at (2,1)
// After normalization: minX=1, minY=0, so originX = 2-1 = 1, originY = 1-0 = 1 // After normalization: minX=1, minY=0, so originX = 2-1 = 1, originY = 1-0 = 1
const result = parseShapeString('eeso'); const result = parseShapeString("eeso");
expect(result.originX).toBe(1); expect(result.originX).toBe(1);
expect(result.originY).toBe(1); expect(result.originY).toBe(1);
}); });
it('should track origin at first o only', () => { it("should track origin at first o only", () => {
const result = parseShapeString('oes'); const result = parseShapeString("oes");
expect(result.originX).toBe(0); expect(result.originX).toBe(0);
expect(result.originY).toBe(0); expect(result.originY).toBe(0);
}); });
it('should handle complex T shape', () => { it("should handle complex T shape", () => {
// oewers: o(0,0), e(1,0), w(0,0), e(1,0), r->(0,0), s(0,1) // oewers: o(0,0), e(1,0), w(0,0), e(1,0), r->(0,0), s(0,1)
// Filled: (0,0), (1,0), (0,1) - 3 cells // Filled: (0,0), (1,0), (0,1) - 3 cells
const result = parseShapeString('oewers'); const result = parseShapeString("oewers");
expect(result.width).toBe(2); expect(result.width).toBe(2);
expect(result.height).toBe(2); expect(result.height).toBe(2);
expect(result.count).toBe(3); expect(result.count).toBe(3);
@ -103,16 +103,16 @@ describe('parseShapeString', () => {
}); });
}); });
describe('shape-collision', () => { describe("shape-collision", () => {
describe('getOccupiedCells', () => { describe("getOccupiedCells", () => {
it('should return cells for a single cell shape', () => { it("should return cells for a single cell shape", () => {
const shape = parseShapeString('o'); const shape = parseShapeString("o");
const cells = getOccupiedCells(shape); const cells = getOccupiedCells(shape);
expect(cells).toEqual([{ x: 0, y: 0 }]); expect(cells).toEqual([{ x: 0, y: 0 }]);
}); });
it('should return cells for a horizontal line', () => { it("should return cells for a horizontal line", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const cells = getOccupiedCells(shape); const cells = getOccupiedCells(shape);
expect(cells).toEqual([ expect(cells).toEqual([
{ x: 0, y: 0 }, { x: 0, y: 0 },
@ -120,8 +120,8 @@ describe('shape-collision', () => {
]); ]);
}); });
it('should return cells for an L shape', () => { it("should return cells for an L shape", () => {
const shape = parseShapeString('oes'); const shape = parseShapeString("oes");
const cells = getOccupiedCells(shape); const cells = getOccupiedCells(shape);
expect(cells).toEqual([ expect(cells).toEqual([
{ x: 0, y: 0 }, { x: 0, y: 0 },
@ -131,49 +131,49 @@ describe('shape-collision', () => {
}); });
}); });
describe('checkCollision', () => { describe("checkCollision", () => {
it('should detect collision between overlapping shapes', () => { it("should detect collision between overlapping shapes", () => {
const shapeA = parseShapeString('o'); const shapeA = parseShapeString("o");
const shapeB = parseShapeString('o'); const shapeB = parseShapeString("o");
const result = checkCollision( const result = checkCollision(
shapeA, shapeA,
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
shapeB, shapeB,
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } } { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
); );
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should not detect collision between non-overlapping shapes', () => { it("should not detect collision between non-overlapping shapes", () => {
const shapeA = parseShapeString('o'); const shapeA = parseShapeString("o");
const shapeB = parseShapeString('o'); const shapeB = parseShapeString("o");
const result = checkCollision( const result = checkCollision(
shapeA, shapeA,
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
shapeB, shapeB,
{ ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } } { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } },
); );
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should detect collision with adjacent shapes', () => { it("should detect collision with adjacent shapes", () => {
const shapeA = parseShapeString('o'); const shapeA = parseShapeString("o");
const shapeB = parseShapeString('o'); const shapeB = parseShapeString("o");
const result = checkCollision( const result = checkCollision(
shapeA, shapeA,
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
shapeB, shapeB,
{ ...IDENTITY_TRANSFORM, offset: { x: 1, y: 0 } } { ...IDENTITY_TRANSFORM, offset: { x: 1, y: 0 } },
); );
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should detect collision with rotation', () => { it("should detect collision with rotation", () => {
const shapeA = parseShapeString('oe'); const shapeA = parseShapeString("oe");
const shapeB = parseShapeString('os'); const shapeB = parseShapeString("os");
// shapeA is horizontal at (0,0)-(1,0) // shapeA is horizontal at (0,0)-(1,0)
// shapeB rotated 90° becomes vertical at (0,0)-(0,1) // shapeB rotated 90° becomes vertical at (0,0)-(0,1)
@ -182,130 +182,142 @@ describe('shape-collision', () => {
shapeA, shapeA,
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
shapeB, shapeB,
{ ...IDENTITY_TRANSFORM, rotation: 90, offset: { x: 0, y: 0 } } { ...IDENTITY_TRANSFORM, rotation: 90, offset: { x: 0, y: 0 } },
); );
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('checkBoardCollision', () => { describe("checkBoardCollision", () => {
it('should detect collision with occupied cells', () => { it("should detect collision with occupied cells", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set(['0,0', '1,0']); const occupied = new Set(["0,0", "1,0"]);
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied); const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should not detect collision with empty board', () => { it("should not detect collision with empty board", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set<string>(); const occupied = new Set<string>();
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied); const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should detect collision after translation', () => { it("should detect collision after translation", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set(['5,5', '6,5']); const occupied = new Set(["5,5", "6,5"]);
const result = checkBoardCollision( const result = checkBoardCollision(
shape, shape,
{ ...IDENTITY_TRANSFORM, offset: { x: 5, y: 5 } }, { ...IDENTITY_TRANSFORM, offset: { x: 5, y: 5 } },
occupied occupied,
); );
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('checkBounds', () => { describe("checkBounds", () => {
it('should return true for shape within bounds', () => { it("should return true for shape within bounds", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const result = checkBounds(shape, IDENTITY_TRANSFORM, 10, 10); const result = checkBounds(shape, IDENTITY_TRANSFORM, 10, 10);
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should return false for shape outside bounds', () => { it("should return false for shape outside bounds", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const result = checkBounds( const result = checkBounds(
shape, shape,
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
10, 10,
10 10,
); );
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should return false for negative coordinates', () => { it("should return false for negative coordinates", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const result = checkBounds( const result = checkBounds(
shape, shape,
{ ...IDENTITY_TRANSFORM, offset: { x: -1, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: -1, y: 0 } },
10, 10,
10 10,
); );
expect(result).toBe(false); expect(result).toBe(false);
}); });
it('should return true for shape at boundary edge', () => { it("should return true for shape at boundary edge", () => {
const shape = parseShapeString('o'); const shape = parseShapeString("o");
const result = checkBounds( const result = checkBounds(
shape, shape,
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 9 } }, { ...IDENTITY_TRANSFORM, offset: { x: 9, y: 9 } },
10, 10,
10 10,
); );
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('validatePlacement', () => { describe("validatePlacement", () => {
it('should return valid for good placement', () => { it("should return valid for good placement", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set<string>(); const occupied = new Set<string>();
const result = validatePlacement(shape, IDENTITY_TRANSFORM, 10, 10, occupied); const result = validateShapePlacement(
shape,
IDENTITY_TRANSFORM,
10,
10,
occupied,
);
expect(result).toEqual({ valid: true }); expect(result).toEqual({ valid: true });
}); });
it('should return invalid for out of bounds', () => { it("should return invalid for out of bounds", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set<string>(); const occupied = new Set<string>();
const result = validatePlacement( const result = validateShapePlacement(
shape, shape,
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } }, { ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
10, 10,
10, 10,
occupied occupied,
); );
expect(result).toEqual({ valid: false, reason: '超出边界' }); expect(result).toEqual({ valid: false, reason: "超出边界" });
}); });
it('should return invalid for collision', () => { it("should return invalid for collision", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const occupied = new Set(['0,0', '1,0']); const occupied = new Set(["0,0", "1,0"]);
const result = validatePlacement(shape, IDENTITY_TRANSFORM, 10, 10, occupied); const result = validateShapePlacement(
expect(result).toEqual({ valid: false, reason: '与已有形状重叠' }); shape,
IDENTITY_TRANSFORM,
10,
10,
occupied,
);
expect(result).toEqual({ valid: false, reason: "与已有形状重叠" });
}); });
}); });
describe('transformShape', () => { describe("transformShape", () => {
it('should apply translation correctly', () => { it("should apply translation correctly", () => {
const shape = parseShapeString('o'); const shape = parseShapeString("o");
const transform = { ...IDENTITY_TRANSFORM, offset: { x: 5, y: 3 } }; const transform = { ...IDENTITY_TRANSFORM, offset: { x: 5, y: 3 } };
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
expect(cells).toEqual([{ x: 5, y: 3 }]); expect(cells).toEqual([{ x: 5, y: 3 }]);
}); });
it('should apply 90° rotation correctly', () => { it("should apply 90° rotation correctly", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const transform = { ...IDENTITY_TRANSFORM, rotation: 90 }; const transform = { ...IDENTITY_TRANSFORM, rotation: 90 };
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
@ -315,8 +327,8 @@ describe('shape-collision', () => {
]); ]);
}); });
it('should apply horizontal flip correctly', () => { it("should apply horizontal flip correctly", () => {
const shape = parseShapeString('oe'); const shape = parseShapeString("oe");
const transform = { ...IDENTITY_TRANSFORM, flipX: true }; const transform = { ...IDENTITY_TRANSFORM, flipX: true };
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
@ -326,8 +338,8 @@ describe('shape-collision', () => {
]); ]);
}); });
it('should apply vertical flip correctly', () => { it("should apply vertical flip correctly", () => {
const shape = parseShapeString('os'); const shape = parseShapeString("os");
const transform = { ...IDENTITY_TRANSFORM, flipY: true }; const transform = { ...IDENTITY_TRANSFORM, flipY: true };
const cells = transformShape(shape, transform); const cells = transformShape(shape, transform);
@ -337,8 +349,8 @@ describe('shape-collision', () => {
]); ]);
}); });
it('should combine rotation and translation', () => { it("should combine rotation and translation", () => {
const shape = parseShapeString('os'); const shape = parseShapeString("os");
const transform = { const transform = {
...IDENTITY_TRANSFORM, ...IDENTITY_TRANSFORM,
rotation: 90, rotation: 90,