Skip to content

Commit 10966ca

Browse files
committed
Possible to make several updates for 1 draw()
1 parent 4ff096b commit 10966ca

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

p5js/tiled-model/sketch.js

+31-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const tileImages = [];
33
let grid = [];
44

55
const DIM = 25;
6+
const NB_UPDATES_PER_TICK=1;
67

78
function preload() {
89
// Load tile images
@@ -95,37 +96,39 @@ function draw() {
9596
}
9697
}
9798

98-
// Pick cell with least entropy
99-
let gridCopy = grid.slice();
100-
gridCopy = gridCopy.filter((a) => !a.collapsed);
99+
for (let idxUpdate = 0; idxUpdate < NB_UPDATES_PER_TICK; idxUpdate++) {
100+
// Pick cell with least entropy
101+
let gridCopy = grid.slice();
102+
gridCopy = gridCopy.filter((a) => !a.collapsed);
101103

102-
if (gridCopy.length == 0) {
103-
return;
104-
}
105-
gridCopy.sort((a, b) => {
106-
return a.options.length - b.options.length;
107-
});
108-
109-
let len = gridCopy[0].options.length;
110-
let stopIndex = 0;
111-
for (let i = 1; i < gridCopy.length; i++) {
112-
if (gridCopy[i].options.length > len) {
113-
stopIndex = i;
114-
break;
104+
if (gridCopy.length == 0) {
105+
return;
106+
}
107+
gridCopy.sort((a, b) => {
108+
return a.options.length - b.options.length;
109+
});
110+
111+
let len = gridCopy[0].options.length;
112+
let stopIndex = 0;
113+
for (let i = 1; i < gridCopy.length; i++) {
114+
if (gridCopy[i].options.length > len) {
115+
stopIndex = i;
116+
break;
117+
}
115118
}
116-
}
117119

118-
if (stopIndex > 0) gridCopy.splice(stopIndex);
119-
const cell = random(gridCopy);
120-
cell.collapsed = true;
121-
const pick = random(cell.options);
122-
if (pick === undefined) {
123-
startOver();
124-
return;
125-
}
126-
cell.options = [pick];
120+
if (stopIndex > 0) gridCopy.splice(stopIndex);
121+
const cell = random(gridCopy);
122+
cell.collapsed = true;
123+
const pick = random(cell.options);
124+
if (pick === undefined) {
125+
startOver();
126+
return;
127+
}
128+
cell.options = [pick];
127129

128-
grid = optimizedNextGrid(cell);
130+
grid = nextGrid(cell);
131+
}
129132
}
130133

131134
// propagate options from src to dest. If dest is above src, dir == UP.
@@ -135,7 +138,7 @@ function propagate(src, dest, dir) {
135138
return oldLen != dest.options.length;
136139
}
137140

138-
function optimizedNextGrid(pick) {
141+
function nextGrid(pick) {
139142
let touched = [posIdx(pick.i, pick.j)];
140143

141144
while (touched.length > 0) {

0 commit comments

Comments
 (0)