feat: add new functions

This commit is contained in:
hyper
2026-04-02 21:58:11 +08:00
parent 11d6cbd030
commit b71ba12454
4 changed files with 315 additions and 102 deletions
+44
View File
@@ -113,4 +113,48 @@ function shuffleCore(region: Region, rng: RNG){
draft.position = posI;
});
}
}
export function moveToRegion(part: Entity<Part>, targetRegion: Entity<Region>, position?: number[]) {
const sourceRegion = part.value.region;
batch(() => {
sourceRegion.produce(draft => {
draft.children = draft.children.filter(c => c.id !== part.id);
});
targetRegion.produce(draft => {
draft.children.push(part);
});
part.produce(draft => {
draft.region = targetRegion;
if (position) draft.position = position;
});
});
}
export function moveToRegionAll(parts: Entity<Part>[], targetRegion: Entity<Region>, positions?: number[][]) {
batch(() => {
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const sourceRegion = part.value.region;
sourceRegion.produce(draft => {
draft.children = draft.children.filter(c => c.id !== part.id);
});
targetRegion.produce(draft => {
draft.children.push(part);
});
part.produce(draft => {
draft.region = targetRegion;
if (positions && positions[i]) draft.position = positions[i];
});
}
});
}
export function removeFromRegion(part: Entity<Part>) {
const region = part.value.region;
batch(() => {
region.produce(draft => {
draft.children = draft.children.filter(c => c.id !== part.id);
});
});
}
+1 -1
View File
@@ -11,7 +11,7 @@ export type { Part } from './core/part';
export { flip, flipTo, roll } from './core/part';
export type { Region, RegionAxis } from './core/region';
export { applyAlign, shuffle, RegionEntity } from './core/region';
export { applyAlign, shuffle, RegionEntity, moveToRegion, moveToRegionAll, removeFromRegion } from './core/region';
// Utils
export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';