-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditConditionRolled.ts
58 lines (50 loc) · 1.23 KB
/
EditConditionRolled.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
DataSet,
Condition,
ConditionRolled,
} from "@systemic-games/pixels-core-animation";
import { safeAssign } from "@systemic-games/pixels-core-utils";
import EditCondition from "./EditCondition";
import EditDataSet from "./EditDataSet";
import { observable } from "./decorators";
export default class EditConditionRolled extends EditCondition {
readonly type = "rolled";
// @widget("faces")
// @range(1, 20)
// @name("Faces")
@observable
faces: number[];
constructor(opt?: { faces?: number[] }) {
super();
this.faces = opt?.faces ?? [];
}
toCondition(_editSet: EditDataSet, _set: DataSet): Condition {
return safeAssign(new ConditionRolled(), {
faceMask: EditConditionRolled.toFaceMask(this.faces),
});
}
duplicate(): EditCondition {
return new EditConditionRolled({
faces: [...this.faces],
});
}
static toFaceMask(faces: number[]): number {
let mask = 0;
for (const face of faces) {
mask |= 1 << (face - 1);
}
return mask;
}
static fromFaceMask(mask: number): number[] {
const faces = [];
let face = 1;
while (mask) {
if (mask & 1) {
faces.push(face);
}
mask = mask >> 1;
++face;
}
return faces;
}
}