Compare commits
No commits in common. "113d240f719e33c9d2918eb7b55f06b231c65888" and "03d367c7b031aecd51d0c9e4a8b13be39f370a6a" have entirely different histories.
113d240f71
...
03d367c7b0
|
|
@ -3,6 +3,5 @@ export * from "./system/deck";
|
|||
export * from "./system/encounter";
|
||||
export * from "./system/grid-inventory";
|
||||
export * from "./system/map";
|
||||
export * from "./system/utils";
|
||||
export * from "./system/utils/parse-shape";
|
||||
export * from "./system/types";
|
||||
export { default as data } from "./data";
|
||||
|
|
|
|||
|
|
@ -1,2 +1,7 @@
|
|||
export * from "./factory";
|
||||
export * from "./types";
|
||||
export type { GameCard, GameCardMeta, PlayerDeck, DeckRegions } from './types';
|
||||
export {
|
||||
generateDeckFromInventory,
|
||||
createCard,
|
||||
createPlayerDeck,
|
||||
generateCardId,
|
||||
} from './factory';
|
||||
|
|
|
|||
|
|
@ -1,3 +1,24 @@
|
|||
export * from "./types";
|
||||
export * from "./transform";
|
||||
export type {
|
||||
CellCoordinate,
|
||||
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";
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { EffectData } from "../types";
|
||||
import type { ParsedShape } from "../utils/parse-shape";
|
||||
import type { Transform2D } from "../utils/shape-collision";
|
||||
import {
|
||||
|
|
@ -11,7 +10,6 @@ import {
|
|||
} from "../utils/shape-collision";
|
||||
import type {
|
||||
CellKey,
|
||||
GameItemMeta,
|
||||
GridInventory,
|
||||
InventoryItem,
|
||||
MutationResult,
|
||||
|
|
@ -257,21 +255,3 @@ export function getAdjacentItems<TMeta>(
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { EffectData, ItemData } from "../types";
|
||||
import { ItemData } from "../types";
|
||||
import type { ParsedShape } from "../utils/parse-shape";
|
||||
import type { Transform2D } from "../utils/shape-collision";
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ export interface GameItemMeta {
|
|||
itemData: ItemData;
|
||||
shape: ParsedShape;
|
||||
consumedUses?: number;
|
||||
startEffects?: Record<string, { data: EffectData; stacks: number }>;
|
||||
startEffects?: Record<string, number>;
|
||||
tradePrice?: number;
|
||||
}
|
||||
export type GameItem = InventoryItem<GameItemMeta>;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,24 @@
|
|||
export * from "./generator";
|
||||
export * from "./navigation";
|
||||
export * from "./types";
|
||||
export { MapNodeType, MapLayerType } from "./types";
|
||||
export type {
|
||||
MapNode,
|
||||
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";
|
||||
|
|
|
|||
|
|
@ -1,2 +0,0 @@
|
|||
export * from "./parse-shape";
|
||||
export * from "./shape-collision";
|
||||
|
|
@ -1,132 +1,129 @@
|
|||
import type { ParsedShape } from "./parse-shape";
|
||||
import type { ParsedShape } from './parse-shape';
|
||||
|
||||
/**
|
||||
* Represents a 2D point in grid coordinates.
|
||||
*/
|
||||
export interface Point2D {
|
||||
x: number;
|
||||
y: number;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2D transformation to apply to a shape.
|
||||
*/
|
||||
export interface Transform2D {
|
||||
/** Translation offset in grid units */
|
||||
offset: Point2D;
|
||||
/** Rotation in degrees (0, 90, 180, 270) */
|
||||
rotation: number;
|
||||
/** Whether to flip horizontally */
|
||||
flipX: boolean;
|
||||
/** Whether to flip vertically */
|
||||
flipY: boolean;
|
||||
/** Translation offset in grid units */
|
||||
offset: Point2D;
|
||||
/** Rotation in degrees (0, 90, 180, 270) */
|
||||
rotation: number;
|
||||
/** Whether to flip horizontally */
|
||||
flipX: boolean;
|
||||
/** Whether to flip vertically */
|
||||
flipY: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default transform (identity).
|
||||
*/
|
||||
export const IDENTITY_TRANSFORM: Transform2D = {
|
||||
offset: { x: 0, y: 0 },
|
||||
rotation: 0,
|
||||
flipX: false,
|
||||
flipY: false,
|
||||
offset: { x: 0, y: 0 },
|
||||
rotation: 0,
|
||||
flipX: false,
|
||||
flipY: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets all occupied cell coordinates from a shape.
|
||||
*/
|
||||
export function getOccupiedCells(shape: ParsedShape): Point2D[] {
|
||||
const cells: Point2D[] = [];
|
||||
for (let y = 0; y < shape.height; y++) {
|
||||
for (let x = 0; x < shape.width; x++) {
|
||||
if (shape.grid[y]?.[x]) {
|
||||
cells.push({ x, y });
|
||||
}
|
||||
const cells: Point2D[] = [];
|
||||
for (let y = 0; y < shape.height; y++) {
|
||||
for (let x = 0; x < shape.width; x++) {
|
||||
if (shape.grid[y]?.[x]) {
|
||||
cells.push({ x, y });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return cells;
|
||||
return cells;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a 2D transformation to a point.
|
||||
*/
|
||||
export function transformPoint(
|
||||
point: Point2D,
|
||||
transform: Transform2D,
|
||||
shapeWidth: number,
|
||||
shapeHeight: number,
|
||||
point: Point2D,
|
||||
transform: Transform2D,
|
||||
shapeWidth: number,
|
||||
shapeHeight: number
|
||||
): Point2D {
|
||||
let { x, y } = point;
|
||||
let { x, y } = point;
|
||||
|
||||
// Apply flips
|
||||
if (transform.flipX) {
|
||||
x = shapeWidth - 1 - x;
|
||||
}
|
||||
if (transform.flipY) {
|
||||
y = shapeHeight - 1 - y;
|
||||
}
|
||||
// Apply flips
|
||||
if (transform.flipX) {
|
||||
x = shapeWidth - 1 - x;
|
||||
}
|
||||
if (transform.flipY) {
|
||||
y = shapeHeight - 1 - y;
|
||||
}
|
||||
|
||||
// Apply rotation (around origin 0,0)
|
||||
const rotation = ((transform.rotation % 360) + 360) % 360;
|
||||
let rotatedX = x;
|
||||
let rotatedY = y;
|
||||
// Apply rotation (around origin 0,0)
|
||||
const rotation = ((transform.rotation % 360) + 360) % 360;
|
||||
let rotatedX = x;
|
||||
let rotatedY = y;
|
||||
|
||||
switch (rotation) {
|
||||
case 90:
|
||||
rotatedX = y;
|
||||
rotatedY = -x;
|
||||
break;
|
||||
case 180:
|
||||
rotatedX = -x;
|
||||
rotatedY = -y;
|
||||
break;
|
||||
case 270:
|
||||
rotatedX = -y;
|
||||
rotatedY = x;
|
||||
break;
|
||||
}
|
||||
switch (rotation) {
|
||||
case 90:
|
||||
rotatedX = y;
|
||||
rotatedY = -x;
|
||||
break;
|
||||
case 180:
|
||||
rotatedX = -x;
|
||||
rotatedY = -y;
|
||||
break;
|
||||
case 270:
|
||||
rotatedX = -y;
|
||||
rotatedY = x;
|
||||
break;
|
||||
}
|
||||
|
||||
// Apply offset
|
||||
return {
|
||||
x: rotatedX + transform.offset.x,
|
||||
y: rotatedY + transform.offset.y,
|
||||
};
|
||||
// Apply offset
|
||||
return {
|
||||
x: rotatedX + transform.offset.x,
|
||||
y: rotatedY + transform.offset.y,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a shape and returnss its occupied cells in world coordinates.
|
||||
*/
|
||||
export function transformShape(
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
): Point2D[] {
|
||||
const cells = getOccupiedCells(shape);
|
||||
return cells.map((cell) =>
|
||||
transformPoint(cell, transform, shape.width, shape.height),
|
||||
);
|
||||
export function transformShape(shape: ParsedShape, transform: Transform2D): Point2D[] {
|
||||
const cells = getOccupiedCells(shape);
|
||||
return cells.map(cell =>
|
||||
transformPoint(cell, transform, shape.width, shape.height)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if two transformed shapes collide (share any occupied cell).
|
||||
*/
|
||||
export function checkCollision(
|
||||
shapeA: ParsedShape,
|
||||
transformA: Transform2D,
|
||||
shapeB: ParsedShape,
|
||||
transformB: Transform2D,
|
||||
shapeA: ParsedShape,
|
||||
transformA: Transform2D,
|
||||
shapeB: ParsedShape,
|
||||
transformB: Transform2D
|
||||
): boolean {
|
||||
const cellsA = transformShape(shapeA, transformA);
|
||||
const cellsB = transformShape(shapeB, transformB);
|
||||
const cellsA = transformShape(shapeA, transformA);
|
||||
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) {
|
||||
if (setA.has(`${cell.x},${cell.y}`)) {
|
||||
return true;
|
||||
for (const cell of cellsB) {
|
||||
if (setA.has(`${cell.x},${cell.y}`)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -136,19 +133,19 @@ export function checkCollision(
|
|||
* @param occupiedCells Set of occupied board cells in "x,y" format
|
||||
*/
|
||||
export function checkBoardCollision(
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
occupiedCells: Set<string>,
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
occupiedCells: Set<string>
|
||||
): boolean {
|
||||
const cells = transformShape(shape, transform);
|
||||
const cells = transformShape(shape, transform);
|
||||
|
||||
for (const cell of cells) {
|
||||
if (occupiedCells.has(`${cell.x},${cell.y}`)) {
|
||||
return true;
|
||||
for (const cell of cells) {
|
||||
if (occupiedCells.has(`${cell.x},${cell.y}`)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -159,47 +156,42 @@ export function checkBoardCollision(
|
|||
* @param boardHeight Board height
|
||||
*/
|
||||
export function checkBounds(
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
boardWidth: number,
|
||||
boardHeight: number,
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
boardWidth: number,
|
||||
boardHeight: number
|
||||
): boolean {
|
||||
const cells = transformShape(shape, transform);
|
||||
const cells = transformShape(shape, transform);
|
||||
|
||||
for (const cell of cells) {
|
||||
if (
|
||||
cell.x < 0 ||
|
||||
cell.x >= boardWidth ||
|
||||
cell.y < 0 ||
|
||||
cell.y >= boardHeight
|
||||
) {
|
||||
return false;
|
||||
for (const cell of cells) {
|
||||
if (cell.x < 0 || cell.x >= boardWidth || cell.y < 0 || cell.y >= boardHeight) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that a placement is both in bounds and collision-free.
|
||||
* @returns Object with `valid` flag and optional `reason` string
|
||||
*/
|
||||
export function validateShapePlacement(
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
boardWidth: number,
|
||||
boardHeight: number,
|
||||
occupiedCells: Set<string>,
|
||||
export function validatePlacement(
|
||||
shape: ParsedShape,
|
||||
transform: Transform2D,
|
||||
boardWidth: number,
|
||||
boardHeight: number,
|
||||
occupiedCells: Set<string>
|
||||
): { valid: true } | { valid: false; reason: string } {
|
||||
if (!checkBounds(shape, transform, boardWidth, boardHeight)) {
|
||||
return { valid: false, reason: "超出边界" };
|
||||
}
|
||||
if (!checkBounds(shape, transform, boardWidth, boardHeight)) {
|
||||
return { valid: false, reason: '超出边界' };
|
||||
}
|
||||
|
||||
if (checkBoardCollision(shape, transform, occupiedCells)) {
|
||||
return { valid: false, reason: "与已有形状重叠" };
|
||||
}
|
||||
if (checkBoardCollision(shape, transform, occupiedCells)) {
|
||||
return { valid: false, reason: '与已有形状重叠' };
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -207,32 +199,29 @@ export function validateShapePlacement(
|
|||
* @param current The current transform
|
||||
* @param degrees Degrees to rotate (typically 90, 180, or 270)
|
||||
*/
|
||||
export function rotateTransform(
|
||||
current: Transform2D,
|
||||
degrees: number,
|
||||
): Transform2D {
|
||||
return {
|
||||
...current,
|
||||
rotation: (((current.rotation + degrees) % 360) + 360) % 360,
|
||||
};
|
||||
export function rotateTransform(current: Transform2D, degrees: number): Transform2D {
|
||||
return {
|
||||
...current,
|
||||
rotation: ((current.rotation + degrees) % 360 + 360) % 360,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flips a transform horizontally.
|
||||
*/
|
||||
export function flipXTransform(current: Transform2D): Transform2D {
|
||||
return {
|
||||
...current,
|
||||
flipX: !current.flipX,
|
||||
};
|
||||
return {
|
||||
...current,
|
||||
flipX: !current.flipX,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flips a transform vertically.
|
||||
*/
|
||||
export function flipYTransform(current: Transform2D): Transform2D {
|
||||
return {
|
||||
...current,
|
||||
flipY: !current.flipY,
|
||||
};
|
||||
return {
|
||||
...current,
|
||||
flipY: !current.flipY,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,367 +1,355 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { parseShapeString } from "@/samples/slay-the-spire-like/system/utils/parse-shape";
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseShapeString } from '@/samples/slay-the-spire-like/system/utils/parse-shape';
|
||||
import {
|
||||
checkCollision,
|
||||
checkBoardCollision,
|
||||
checkBounds,
|
||||
validateShapePlacement,
|
||||
transformShape,
|
||||
getOccupiedCells,
|
||||
IDENTITY_TRANSFORM,
|
||||
} from "@/samples/slay-the-spire-like/system/utils/shape-collision";
|
||||
checkCollision,
|
||||
checkBoardCollision,
|
||||
checkBounds,
|
||||
validatePlacement,
|
||||
transformShape,
|
||||
getOccupiedCells,
|
||||
IDENTITY_TRANSFORM,
|
||||
} from '@/samples/slay-the-spire-like/system/utils/shape-collision';
|
||||
|
||||
describe("parseShapeString", () => {
|
||||
it("should parse a single cell with o", () => {
|
||||
const result = parseShapeString("o");
|
||||
expect(result.grid).toEqual([[true]]);
|
||||
expect(result.width).toBe(1);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(1);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
describe('parseShapeString', () => {
|
||||
it('should parse a single cell with o', () => {
|
||||
const result = parseShapeString('o');
|
||||
expect(result.grid).toEqual([[true]]);
|
||||
expect(result.width).toBe(1);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(1);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
|
||||
it("should parse a horizontal line", () => {
|
||||
const result = parseShapeString("oee");
|
||||
expect(result.width).toBe(3);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([[true, true, true]]);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
it('should parse a horizontal line', () => {
|
||||
const result = parseShapeString('oee');
|
||||
expect(result.width).toBe(3);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([[true, true, true]]);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
|
||||
it("should parse a vertical line", () => {
|
||||
const result = parseShapeString("oss");
|
||||
expect(result.width).toBe(1);
|
||||
expect(result.height).toBe(3);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([[true], [true], [true]]);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
it('should parse a vertical line', () => {
|
||||
const result = parseShapeString('oss');
|
||||
expect(result.width).toBe(1);
|
||||
expect(result.height).toBe(3);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([[true], [true], [true]]);
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
|
||||
it("should parse an L shape", () => {
|
||||
const result = parseShapeString("oes");
|
||||
expect(result.width).toBe(2);
|
||||
expect(result.height).toBe(2);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([
|
||||
[true, true],
|
||||
[false, true],
|
||||
]);
|
||||
});
|
||||
it('should parse an L shape', () => {
|
||||
const result = parseShapeString('oes');
|
||||
expect(result.width).toBe(2);
|
||||
expect(result.height).toBe(2);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([
|
||||
[true, true],
|
||||
[false, true],
|
||||
]);
|
||||
});
|
||||
|
||||
it("should handle return command", () => {
|
||||
const result = parseShapeString("oeerww");
|
||||
expect(result.width).toBe(4);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(4);
|
||||
expect(result.grid).toEqual([[true, true, true, true]]);
|
||||
});
|
||||
it('should handle return command', () => {
|
||||
const result = parseShapeString('oeerww');
|
||||
expect(result.width).toBe(4);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(4);
|
||||
expect(result.grid).toEqual([[true, true, true, true]]);
|
||||
});
|
||||
|
||||
it("should handle case insensitivity", () => {
|
||||
const resultLower = parseShapeString("oes");
|
||||
const resultUpper = parseShapeString("OES");
|
||||
expect(resultLower.grid).toEqual(resultUpper.grid);
|
||||
expect(resultLower.count).toBe(resultUpper.count);
|
||||
});
|
||||
it('should handle case insensitivity', () => {
|
||||
const resultLower = parseShapeString('oes');
|
||||
const resultUpper = parseShapeString('OES');
|
||||
expect(resultLower.grid).toEqual(resultUpper.grid);
|
||||
expect(resultLower.count).toBe(resultUpper.count);
|
||||
});
|
||||
|
||||
it("should return empty grid for empty input", () => {
|
||||
const result = parseShapeString("");
|
||||
expect(result.grid).toEqual([[]]);
|
||||
expect(result.width).toBe(0);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(0);
|
||||
});
|
||||
it('should return empty grid for empty input', () => {
|
||||
const result = parseShapeString('');
|
||||
expect(result.grid).toEqual([[]]);
|
||||
expect(result.width).toBe(0);
|
||||
expect(result.height).toBe(1);
|
||||
expect(result.count).toBe(0);
|
||||
});
|
||||
|
||||
it("should track origin correctly", () => {
|
||||
// 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
|
||||
const result = parseShapeString("eeso");
|
||||
expect(result.originX).toBe(1);
|
||||
expect(result.originY).toBe(1);
|
||||
});
|
||||
it('should track origin correctly', () => {
|
||||
// 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
|
||||
const result = parseShapeString('eeso');
|
||||
expect(result.originX).toBe(1);
|
||||
expect(result.originY).toBe(1);
|
||||
});
|
||||
|
||||
it("should track origin at first o only", () => {
|
||||
const result = parseShapeString("oes");
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
it('should track origin at first o only', () => {
|
||||
const result = parseShapeString('oes');
|
||||
expect(result.originX).toBe(0);
|
||||
expect(result.originY).toBe(0);
|
||||
});
|
||||
|
||||
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)
|
||||
// Filled: (0,0), (1,0), (0,1) - 3 cells
|
||||
const result = parseShapeString("oewers");
|
||||
expect(result.width).toBe(2);
|
||||
expect(result.height).toBe(2);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([
|
||||
[true, true],
|
||||
[true, false],
|
||||
]);
|
||||
});
|
||||
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)
|
||||
// Filled: (0,0), (1,0), (0,1) - 3 cells
|
||||
const result = parseShapeString('oewers');
|
||||
expect(result.width).toBe(2);
|
||||
expect(result.height).toBe(2);
|
||||
expect(result.count).toBe(3);
|
||||
expect(result.grid).toEqual([
|
||||
[true, true],
|
||||
[true, false],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shape-collision", () => {
|
||||
describe("getOccupiedCells", () => {
|
||||
it("should return cells for a single cell shape", () => {
|
||||
const shape = parseShapeString("o");
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([{ x: 0, y: 0 }]);
|
||||
describe('shape-collision', () => {
|
||||
describe('getOccupiedCells', () => {
|
||||
it('should return cells for a single cell shape', () => {
|
||||
const shape = parseShapeString('o');
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([{ x: 0, y: 0 }]);
|
||||
});
|
||||
|
||||
it('should return cells for a horizontal line', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return cells for an L shape', () => {
|
||||
const shape = parseShapeString('oes');
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it("should return cells for a horizontal line", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
]);
|
||||
describe('checkCollision', () => {
|
||||
it('should detect collision between overlapping shapes', () => {
|
||||
const shapeA = parseShapeString('o');
|
||||
const shapeB = parseShapeString('o');
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } }
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should not detect collision between non-overlapping shapes', () => {
|
||||
const shapeA = parseShapeString('o');
|
||||
const shapeB = parseShapeString('o');
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } }
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should detect collision with adjacent shapes', () => {
|
||||
const shapeA = parseShapeString('o');
|
||||
const shapeB = parseShapeString('o');
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 1, y: 0 } }
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should detect collision with rotation', () => {
|
||||
const shapeA = parseShapeString('oe');
|
||||
const shapeB = parseShapeString('os');
|
||||
|
||||
// shapeA is horizontal at (0,0)-(1,0)
|
||||
// shapeB rotated 90° becomes vertical at (0,0)-(0,1)
|
||||
// They should collide at (0,0)
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, rotation: 90, offset: { x: 0, y: 0 } }
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("should return cells for an L shape", () => {
|
||||
const shape = parseShapeString("oes");
|
||||
const cells = getOccupiedCells(shape);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
describe('checkBoardCollision', () => {
|
||||
it('should detect collision with occupied cells', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set(['0,0', '1,0']);
|
||||
|
||||
describe("checkCollision", () => {
|
||||
it("should detect collision between overlapping shapes", () => {
|
||||
const shapeA = parseShapeString("o");
|
||||
const shapeB = parseShapeString("o");
|
||||
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
it('should not detect collision with empty board', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should detect collision after translation', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set(['5,5', '6,5']);
|
||||
|
||||
const result = checkBoardCollision(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 5, y: 5 } },
|
||||
occupied
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("should not detect collision between non-overlapping shapes", () => {
|
||||
const shapeA = parseShapeString("o");
|
||||
const shapeB = parseShapeString("o");
|
||||
describe('checkBounds', () => {
|
||||
it('should return true for shape within bounds', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } },
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
const result = checkBounds(shape, IDENTITY_TRANSFORM, 10, 10);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for shape outside bounds', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
|
||||
10,
|
||||
10
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for negative coordinates', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: -1, y: 0 } },
|
||||
10,
|
||||
10
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true for shape at boundary edge', () => {
|
||||
const shape = parseShapeString('o');
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 9 } },
|
||||
10,
|
||||
10
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("should detect collision with adjacent shapes", () => {
|
||||
const shapeA = parseShapeString("o");
|
||||
const shapeB = parseShapeString("o");
|
||||
describe('validatePlacement', () => {
|
||||
it('should return valid for good placement', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 1, y: 0 } },
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
const result = validatePlacement(shape, IDENTITY_TRANSFORM, 10, 10, occupied);
|
||||
expect(result).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
it('should return invalid for out of bounds', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = validatePlacement(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
|
||||
10,
|
||||
10,
|
||||
occupied
|
||||
);
|
||||
expect(result).toEqual({ valid: false, reason: '超出边界' });
|
||||
});
|
||||
|
||||
it('should return invalid for collision', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const occupied = new Set(['0,0', '1,0']);
|
||||
|
||||
const result = validatePlacement(shape, IDENTITY_TRANSFORM, 10, 10, occupied);
|
||||
expect(result).toEqual({ valid: false, reason: '与已有形状重叠' });
|
||||
});
|
||||
});
|
||||
|
||||
it("should detect collision with rotation", () => {
|
||||
const shapeA = parseShapeString("oe");
|
||||
const shapeB = parseShapeString("os");
|
||||
describe('transformShape', () => {
|
||||
it('should apply translation correctly', () => {
|
||||
const shape = parseShapeString('o');
|
||||
const transform = { ...IDENTITY_TRANSFORM, offset: { x: 5, y: 3 } };
|
||||
|
||||
// shapeA is horizontal at (0,0)-(1,0)
|
||||
// shapeB rotated 90° becomes vertical at (0,0)-(0,1)
|
||||
// They should collide at (0,0)
|
||||
const result = checkCollision(
|
||||
shapeA,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 0, y: 0 } },
|
||||
shapeB,
|
||||
{ ...IDENTITY_TRANSFORM, rotation: 90, offset: { x: 0, y: 0 } },
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([{ x: 5, y: 3 }]);
|
||||
});
|
||||
|
||||
it('should apply 90° rotation correctly', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const transform = { ...IDENTITY_TRANSFORM, rotation: 90 };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 0, y: -1 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should apply horizontal flip correctly', () => {
|
||||
const shape = parseShapeString('oe');
|
||||
const transform = { ...IDENTITY_TRANSFORM, flipX: true };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 0, y: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should apply vertical flip correctly', () => {
|
||||
const shape = parseShapeString('os');
|
||||
const transform = { ...IDENTITY_TRANSFORM, flipY: true };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 1 },
|
||||
{ x: 0, y: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('should combine rotation and translation', () => {
|
||||
const shape = parseShapeString('os');
|
||||
const transform = {
|
||||
...IDENTITY_TRANSFORM,
|
||||
rotation: 90,
|
||||
offset: { x: 10, y: 10 },
|
||||
};
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 10, y: 10 },
|
||||
{ x: 11, y: 10 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkBoardCollision", () => {
|
||||
it("should detect collision with occupied cells", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set(["0,0", "1,0"]);
|
||||
|
||||
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should not detect collision with empty board", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = checkBoardCollision(shape, IDENTITY_TRANSFORM, occupied);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should detect collision after translation", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set(["5,5", "6,5"]);
|
||||
|
||||
const result = checkBoardCollision(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 5, y: 5 } },
|
||||
occupied,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkBounds", () => {
|
||||
it("should return true for shape within bounds", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
|
||||
const result = checkBounds(shape, IDENTITY_TRANSFORM, 10, 10);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for shape outside bounds", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
|
||||
10,
|
||||
10,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for negative coordinates", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: -1, y: 0 } },
|
||||
10,
|
||||
10,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true for shape at boundary edge", () => {
|
||||
const shape = parseShapeString("o");
|
||||
|
||||
const result = checkBounds(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 9 } },
|
||||
10,
|
||||
10,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validatePlacement", () => {
|
||||
it("should return valid for good placement", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = validateShapePlacement(
|
||||
shape,
|
||||
IDENTITY_TRANSFORM,
|
||||
10,
|
||||
10,
|
||||
occupied,
|
||||
);
|
||||
expect(result).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
it("should return invalid for out of bounds", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set<string>();
|
||||
|
||||
const result = validateShapePlacement(
|
||||
shape,
|
||||
{ ...IDENTITY_TRANSFORM, offset: { x: 9, y: 0 } },
|
||||
10,
|
||||
10,
|
||||
occupied,
|
||||
);
|
||||
expect(result).toEqual({ valid: false, reason: "超出边界" });
|
||||
});
|
||||
|
||||
it("should return invalid for collision", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const occupied = new Set(["0,0", "1,0"]);
|
||||
|
||||
const result = validateShapePlacement(
|
||||
shape,
|
||||
IDENTITY_TRANSFORM,
|
||||
10,
|
||||
10,
|
||||
occupied,
|
||||
);
|
||||
expect(result).toEqual({ valid: false, reason: "与已有形状重叠" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("transformShape", () => {
|
||||
it("should apply translation correctly", () => {
|
||||
const shape = parseShapeString("o");
|
||||
const transform = { ...IDENTITY_TRANSFORM, offset: { x: 5, y: 3 } };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([{ x: 5, y: 3 }]);
|
||||
});
|
||||
|
||||
it("should apply 90° rotation correctly", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const transform = { ...IDENTITY_TRANSFORM, rotation: 90 };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 0, y: -1 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should apply horizontal flip correctly", () => {
|
||||
const shape = parseShapeString("oe");
|
||||
const transform = { ...IDENTITY_TRANSFORM, flipX: true };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 0, y: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should apply vertical flip correctly", () => {
|
||||
const shape = parseShapeString("os");
|
||||
const transform = { ...IDENTITY_TRANSFORM, flipY: true };
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 0, y: 1 },
|
||||
{ x: 0, y: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should combine rotation and translation", () => {
|
||||
const shape = parseShapeString("os");
|
||||
const transform = {
|
||||
...IDENTITY_TRANSFORM,
|
||||
rotation: 90,
|
||||
offset: { x: 10, y: 10 },
|
||||
};
|
||||
|
||||
const cells = transformShape(shape, transform);
|
||||
expect(cells).toEqual([
|
||||
{ x: 10, y: 10 },
|
||||
{ x: 11, y: 10 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue