Repository navigation
Expand file tree
/
Copy pathshapes.js
More file actions
139 lines (126 loc) · 5.09 KB
/
Copy pathshapes.js
File metadata and controls
139 lines (126 loc) · 5.09 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function () {
const { hexToPixel, HEX_HEIGHT, getBoardMode } = window.GenericBlockGame;
// Each piece uses [q, r] hex cells and includes the [0, 0] anchor.
// Weight controls how often a shape appears: larger numbers appear more often.
// Add a new object to this array to add a new piece to the game.
const SHAPES = [
{ id: "single", weight: 5, cells: [[0,0]] },
{ id: "line2", weight: 5, cells: [[0,0],[1,0]] },
{ id: "line3", weight: 5, cells: [[0,0],[1,0],[2,0]] },
{ id: "line4", weight: 5, cells: [[0,0],[1,0],[2,0],[3,0]] },
{ id: "bend3", weight: 5, cells: [[0,0],[1,0],[1,1]] },
{ id: "triangle3", weight: 5, cells: [[0,0],[1,0],[0,1]] },
{ id: "diamond4", weight: 5, cells: [[0,0],[1,0],[0,1],[1,1]] },
{ id: "fork4", weight: 5, cells: [[0,0],[1,0],[-1,1],[0,-1]] },
{ id: "zigzag4", weight: 5, cells: [[0,0],[1,0],[1,1],[2,1]] },
{ id: "hook4", weight: 5, cells: [[0,0],[1,0],[2,0],[2,-1]] },
{ id: "arc4", weight: 5, cells: [[0,0],[1,0],[1,1],[0,2]] },
{ id: "triangleTail4", weight: 5, cells: [[0,0],[1,0],[0,1],[0,2]] },
{ id: "hookLeft4", weight: 5, cells: [[0,0],[-1,1],[-1,2],[-1,3]] },
{ id: "hookRight4", weight: 5, cells: [[0,0],[1,0],[1,1],[1,2]] },
];
const SQUARE_SHAPES = [
{ id: "single", cells: [[0,0]] },
{ id: "line2", cells: [[0,0],[1,0]] },
{ id: "diagonal2", cells: [[0,0],[1,1]] },
{ id: "line3", cells: [[0,0],[1,0],[2,0]] },
{ id: "diagonal3", cells: [[0,0],[1,1],[2,2]] },
{ id: "line4", cells: [[0,0],[1,0],[2,0],[3,0]] },
{ id: "square2", cells: [[0,0],[1,0],[0,1],[1,1]] },
{ id: "corner3", cells: [[0,0],[0,1],[1,1]] },
{ id: "corner5", cells: [[0,0],[0,1],[0,2],[1,2],[2,2]] },
{ id: "t4", cells: [[0,0],[1,0],[2,0],[1,1]] },
{ id: "s4", cells: [[1,0],[2,0],[0,1],[1,1]] },
{ id: "z4", cells: [[0,0],[1,0],[1,1],[2,1]] },
{ id: "l4", cells: [[0,0],[0,1],[0,2],[1,2]] },
{ id: "j4", cells: [[1,0],[1,1],[0,2],[1,2]] },
{ id: "t5", cells: [[0,0],[1,0],[2,0],[1,1],[1,2]] },
].map(shape => {
const [anchorQ, anchorR] = shape.cells[0];
return { ...shape, weight: 5, cells: shape.cells.map(([q, r]) => [q - anchorQ, r - anchorR]) };
});
function rotationCount(mode = getBoardMode()) {
return mode === "hex" ? 6 : 4;
}
function shapePoint(q, r, mode = getBoardMode()) {
return mode === "hex" ? hexToPixel(q, r) : { x: q, y: r };
}
function cellHeight(mode = getBoardMode()) {
return mode === "hex" ? HEX_HEIGHT : 1;
}
// Looks up a shape stored in the tray by its short ID.
function shapeById(id) {
// Fall back safely if a saved tray references a custom piece later deleted.
const shapes = getAllShapes();
return shapes.find(shape => shape.id === id) || shapes[0];
}
// Combines built-in pieces with pieces created in the settings editor.
function getAllShapes() {
const preferences = window.GenericBlockGame.loadPreferences();
const shapes = getBoardMode() === "hex" ? SHAPES : SQUARE_SHAPES;
return [...shapes, ...preferences.customPieces.map(shape => {
const [anchorQ, anchorR] = shape.cells[0];
return { ...shape, cells: shape.cells.map(([q, r]) => [q - anchorQ, r - anchorR]) };
})];
}
// Applies each piece's enabled state and user-selected frequency.
function getConfiguredShapes() {
const preferences = window.GenericBlockGame.loadPreferences();
return getAllShapes().map(shape => {
const override = preferences.pieces[shape.id] || {};
return {
...shape,
enabled: override.enabled !== false,
weight: Math.max(0, Number(override.weight ?? shape.weight) || 0),
};
});
}
// Bounds include the whole hex tile, even when a turn gives negative offsets.
function shapeDimensions(shape) {
const tileHeight = cellHeight();
const centers = shape.cells.map(([q, r]) => shapePoint(q, r));
const minX = Math.min(...centers.map(cell => cell.x)) - 0.5;
const minY = Math.min(...centers.map(cell => cell.y)) - tileHeight / 2;
return {
minX,
minY,
width: Math.max(...centers.map(cell => cell.x)) + 0.5 - minX,
height: Math.max(...centers.map(cell => cell.y)) + tileHeight / 2 - minY,
};
}
// Rotate around the anchor by one turn of the current grid.
function rotateShape(shape, turns = 0) {
let cells = shape.cells.map(([x, y]) => [x, y]);
const count = rotationCount();
const rotations = ((turns % count) + count) % count;
for (let turn = 0; turn < rotations; turn += 1) {
cells = cells.map(([q, r]) => count === 6 ? [-r, q + r] : [-r, q]);
}
return { ...shape, cells };
}
// Picks a random shape while respecting each shape's frequency weight.
function randomShape() {
const availableShapes = getConfiguredShapes().filter(shape => shape.enabled && shape.weight > 0);
const pool = availableShapes.length ? availableShapes : getAllShapes();
const totalWeight = pool.reduce((sum, shape) => sum + shape.weight, 0);
let roll = Math.random() * totalWeight;
for (const shape of pool) {
roll -= shape.weight;
if (roll <= 0) return shape;
}
return pool.at(-1);
}
Object.assign(window.GenericBlockGame, {
SHAPES,
SQUARE_SHAPES,
rotationCount,
shapePoint,
cellHeight,
getAllShapes,
getConfiguredShapes,
shapeById,
shapeDimensions,
rotateShape,
randomShape,
});
}());