2026-04-02 16:39:08 +08:00
|
|
|
import {batch, computed, ReadonlySignal, SignalOptions} from "@preact/signals-core";
|
2026-04-02 15:59:27 +08:00
|
|
|
import {Entity} from "@/utils/entity";
|
2026-04-01 13:36:16 +08:00
|
|
|
import {Part} from "./part";
|
2026-04-02 15:59:27 +08:00
|
|
|
import {RNG} from "@/utils/rng";
|
2026-04-01 13:36:16 +08:00
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export type Region = {
|
|
|
|
|
id: string;
|
2026-04-01 13:36:16 +08:00
|
|
|
axes: RegionAxis[];
|
2026-04-02 13:52:15 +08:00
|
|
|
children: Entity<Part>[];
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RegionAxis = {
|
|
|
|
|
name: string;
|
|
|
|
|
min?: number;
|
|
|
|
|
max?: number;
|
|
|
|
|
align?: 'start' | 'end' | 'center';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 16:32:26 +08:00
|
|
|
export class RegionEntity extends Entity<Region> {
|
|
|
|
|
public readonly partsMap: ReadonlySignal<Record<string, Entity<Part>>>;
|
|
|
|
|
|
|
|
|
|
public constructor(id: string, t?: Region, options?: SignalOptions<Region>) {
|
|
|
|
|
super(id, t, options);
|
|
|
|
|
this.partsMap = computed(() => {
|
|
|
|
|
const result: Record<string, Entity<Part>> = {};
|
|
|
|
|
for (const child of this.value.children) {
|
|
|
|
|
const key = child.value.position.join(',');
|
|
|
|
|
result[key] = child;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function applyAlign(region: Entity<Region>) {
|
2026-04-02 16:39:08 +08:00
|
|
|
batch(() => {
|
|
|
|
|
region.produce(applyAlignCore);
|
|
|
|
|
});
|
2026-04-02 13:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyAlignCore(region: Region) {
|
2026-04-01 17:48:40 +08:00
|
|
|
if (region.children.length === 0) return;
|
|
|
|
|
|
|
|
|
|
for (let axisIndex = 0; axisIndex < region.axes.length; axisIndex++) {
|
|
|
|
|
const axis = region.axes[axisIndex];
|
|
|
|
|
if (!axis.align) continue;
|
2026-04-01 17:34:21 +08:00
|
|
|
|
2026-04-01 18:33:09 +08:00
|
|
|
const positionValues = new Set<number>();
|
2026-04-02 13:52:15 +08:00
|
|
|
for (const child of region.children) {
|
|
|
|
|
positionValues.add(child.value.position[axisIndex] ?? 0);
|
2026-04-01 18:33:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sortedPositions = Array.from(positionValues).sort((a, b) => a - b);
|
|
|
|
|
const positionMap = new Map<number, number>();
|
2026-04-01 17:34:21 +08:00
|
|
|
|
|
|
|
|
if (axis.align === 'start' && axis.min !== undefined) {
|
2026-04-01 18:33:09 +08:00
|
|
|
sortedPositions.forEach((pos, index) => {
|
|
|
|
|
positionMap.set(pos, axis.min! + index);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
} else if (axis.align === 'end' && axis.max !== undefined) {
|
2026-04-01 18:33:09 +08:00
|
|
|
const count = sortedPositions.length;
|
|
|
|
|
sortedPositions.forEach((pos, index) => {
|
|
|
|
|
positionMap.set(pos, axis.max! - (count - 1 - index));
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
} else if (axis.align === 'center') {
|
2026-04-01 18:33:09 +08:00
|
|
|
const count = sortedPositions.length;
|
2026-04-01 17:34:21 +08:00
|
|
|
const min = axis.min ?? 0;
|
|
|
|
|
const max = axis.max ?? count - 1;
|
|
|
|
|
const range = max - min;
|
|
|
|
|
const center = min + range / 2;
|
2026-04-01 17:48:40 +08:00
|
|
|
|
2026-04-01 18:33:09 +08:00
|
|
|
sortedPositions.forEach((pos, index) => {
|
2026-04-01 17:34:21 +08:00
|
|
|
const offset = index - (count - 1) / 2;
|
2026-04-01 18:33:09 +08:00
|
|
|
positionMap.set(pos, center + offset);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
}
|
2026-04-01 18:33:09 +08:00
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
for (const child of region.children) {
|
|
|
|
|
child.produce(draft => {
|
|
|
|
|
const currentPos = draft.position[axisIndex] ?? 0;
|
|
|
|
|
draft.position[axisIndex] = positionMap.get(currentPos) ?? currentPos;
|
|
|
|
|
});
|
2026-04-01 18:33:09 +08:00
|
|
|
}
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
2026-04-01 18:33:09 +08:00
|
|
|
|
|
|
|
|
region.children.sort((a, b) => {
|
|
|
|
|
for (let i = 0; i < region.axes.length; i++) {
|
|
|
|
|
const diff = (a.value.position[i] ?? 0) - (b.value.position[i] ?? 0);
|
|
|
|
|
if (diff !== 0) return diff;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function shuffle(region: Entity<Region>, rng: RNG) {
|
2026-04-02 16:39:08 +08:00
|
|
|
batch(() => {
|
|
|
|
|
region.produce(region => shuffleCore(region, rng));
|
|
|
|
|
});
|
2026-04-02 13:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shuffleCore(region: Region, rng: RNG){
|
2026-04-01 17:34:21 +08:00
|
|
|
if (region.children.length <= 1) return;
|
|
|
|
|
|
|
|
|
|
const children = [...region.children];
|
|
|
|
|
for (let i = children.length - 1; i > 0; i--) {
|
|
|
|
|
const j = rng.nextInt(i + 1);
|
2026-04-02 13:52:15 +08:00
|
|
|
const posI = [...children[i].value.position];
|
|
|
|
|
const posJ = [...children[j].value.position];
|
|
|
|
|
children[i].produce(draft => {
|
|
|
|
|
draft.position = posJ;
|
|
|
|
|
});
|
|
|
|
|
children[j].produce(draft => {
|
|
|
|
|
draft.position = posI;
|
|
|
|
|
});
|
2026-04-01 17:34:21 +08:00
|
|
|
}
|
2026-04-02 21:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-02 13:52:15 +08:00
|
|
|
}
|