boardgame-core/src/core/part-factory.ts

23 lines
767 B
TypeScript
Raw Normal View History

import {Part} from "./part";
2026-04-05 10:22:27 +08:00
import {Immutable} from "mutative";
2026-04-06 15:47:33 +08:00
export function createPartsFromTable<T>(items: readonly T[], getId: (item: T, index: number) => string, getCount?: ((item: T) => number) | number){
2026-04-04 18:42:21 +08:00
const pool: Record<string, Part<T>> = {};
for (const entry of items) {
const count = getCount ? (typeof getCount === 'function' ? getCount(entry) : getCount) : 1;
for (let i = 0; i < count; i++) {
const id = getId(entry, i);
pool[id] = {
id,
regionId: '',
position: [],
2026-04-05 10:22:27 +08:00
...entry as Immutable<T>
2026-04-04 18:42:21 +08:00
};
}
}
return pool;
}
2026-04-05 10:22:27 +08:00
export function createParts<T>(item: T, getId: (index: number) => string, count?: number){
return createPartsFromTable([item], (_,index) => getId(index), count);
}