From 97ff61985ad9766df4a6ccc30be0b7c2780d3bb6 Mon Sep 17 00:00:00 2001 From: hypercross Date: Tue, 21 Apr 2026 23:01:19 +0800 Subject: [PATCH] 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` --- src/samples/slay-the-spire-like/index.ts | 2 +- .../slay-the-spire-like/system/deck/index.ts | 9 +- .../system/grid-inventory/index.ts | 25 +- .../slay-the-spire-like/system/map/index.ts | 27 +- .../slay-the-spire-like/system/utils/index.ts | 2 + .../system/utils/shape-collision.ts | 251 +++++++++--------- 6 files changed, 141 insertions(+), 175 deletions(-) create mode 100644 src/samples/slay-the-spire-like/system/utils/index.ts diff --git a/src/samples/slay-the-spire-like/index.ts b/src/samples/slay-the-spire-like/index.ts index aa67064..bd1c78c 100644 --- a/src/samples/slay-the-spire-like/index.ts +++ b/src/samples/slay-the-spire-like/index.ts @@ -3,5 +3,5 @@ export * from "./system/deck"; export * from "./system/encounter"; export * from "./system/grid-inventory"; export * from "./system/map"; -export * from "./system/utils/parse-shape"; +export * from "./system/utils"; export * from "./system/types"; diff --git a/src/samples/slay-the-spire-like/system/deck/index.ts b/src/samples/slay-the-spire-like/system/deck/index.ts index 32c7ffa..4f90a60 100644 --- a/src/samples/slay-the-spire-like/system/deck/index.ts +++ b/src/samples/slay-the-spire-like/system/deck/index.ts @@ -1,7 +1,2 @@ -export type { GameCard, GameCardMeta, PlayerDeck, DeckRegions } from './types'; -export { - generateDeckFromInventory, - createCard, - createPlayerDeck, - generateCardId, -} from './factory'; +export * from "./factory"; +export * from "./types"; diff --git a/src/samples/slay-the-spire-like/system/grid-inventory/index.ts b/src/samples/slay-the-spire-like/system/grid-inventory/index.ts index 7804168..cbf93dc 100644 --- a/src/samples/slay-the-spire-like/system/grid-inventory/index.ts +++ b/src/samples/slay-the-spire-like/system/grid-inventory/index.ts @@ -1,24 +1,3 @@ -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 "./types"; +export * from "./transform"; export * from "./factory"; diff --git a/src/samples/slay-the-spire-like/system/map/index.ts b/src/samples/slay-the-spire-like/system/map/index.ts index 55e5470..cd75132 100644 --- a/src/samples/slay-the-spire-like/system/map/index.ts +++ b/src/samples/slay-the-spire-like/system/map/index.ts @@ -1,24 +1,3 @@ -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"; +export * from "./generator"; +export * from "./navigation"; +export * from "./types"; diff --git a/src/samples/slay-the-spire-like/system/utils/index.ts b/src/samples/slay-the-spire-like/system/utils/index.ts new file mode 100644 index 0000000..b7e1777 --- /dev/null +++ b/src/samples/slay-the-spire-like/system/utils/index.ts @@ -0,0 +1,2 @@ +export * from "./parse-shape"; +export * from "./shape-collision"; diff --git a/src/samples/slay-the-spire-like/system/utils/shape-collision.ts b/src/samples/slay-the-spire-like/system/utils/shape-collision.ts index fafdefb..5967605 100644 --- a/src/samples/slay-the-spire-like/system/utils/shape-collision.ts +++ b/src/samples/slay-the-spire-like/system/utils/shape-collision.ts @@ -1,129 +1,132 @@ -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; } /** @@ -133,19 +136,19 @@ export function checkCollision( * @param occupiedCells Set of occupied board cells in "x,y" format */ export function checkBoardCollision( - shape: ParsedShape, - transform: Transform2D, - occupiedCells: Set + shape: ParsedShape, + transform: Transform2D, + occupiedCells: Set, ): 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; } /** @@ -156,42 +159,47 @@ 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 validatePlacement( - shape: ParsedShape, - transform: Transform2D, - boardWidth: number, - boardHeight: number, - occupiedCells: Set +export function validateShapePlacement( + shape: ParsedShape, + transform: Transform2D, + boardWidth: number, + boardHeight: number, + occupiedCells: Set, ): { 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 }; } /** @@ -199,29 +207,32 @@ export function validatePlacement( * @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, + }; }