boardgame-core/src/core/part.ts

37 lines
764 B
TypeScript
Raw Normal View History

import {Entity} from "../utils/entity";
2026-04-01 13:36:16 +08:00
import {Region} from "./region";
import {RNG} from "../utils/rng";
export type Part = {
id: string;
2026-04-02 13:58:52 +08:00
sides?: number;
side?: number;
2026-04-01 13:36:16 +08:00
alignments?: string[];
alignment?: string;
region: Entity<Region>;
2026-04-01 13:36:16 +08:00
position: number[];
}
export function flip(part: Entity<Part>) {
part.produce(draft => {
2026-04-02 13:58:52 +08:00
if(!draft.sides)return;
draft.side = ((draft.side||0) + 1) % draft.sides;
});
2026-04-01 13:36:16 +08:00
}
export function flipTo(part: Entity<Part>, side: number) {
part.produce(draft => {
2026-04-02 13:58:52 +08:00
if(!draft.sides || side >= draft.sides)return;
draft.side = side;
});
2026-04-01 13:36:16 +08:00
}
export function roll(part: Entity<Part>, rng: RNG) {
part.produce(draft => {
2026-04-02 13:58:52 +08:00
if(!draft.sides)return;
draft.side = rng.nextInt(draft.sides);
});
2026-04-01 17:48:40 +08:00
}