-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
254 lines (208 loc) · 5.37 KB
/
app.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/** @type HTMLCanvasElement */
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const colors = ['green', 'brown']
// [
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [1, 1, 0, 0, 1, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 1],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0, 0, 0],
// ]
class BlobMap {
/**
* @param width {number}
* @param height {number}
* @param tileSize {number}
*/
constructor(width, height, tileSize) {
this.width = width
this.height = height
this.tileSize = tileSize
this.tileBitSize = tileSize / 3
this.init()
}
init = () => {
/** @type number[][] */
this.data = []
for (let r = 0; r < this.height; ++r) {
for (let i = 0; i < 3; ++i) {
const row = []
for (let c = 0; c < this.width; ++c) {
for (let j = 0; j < 3; ++j) {
row.push(0)
}
}
this.data.push(row)
}
}
}
isCellEmpty = ([x, y]) => {
return !this.data[y * 3 + 1][x * 3 + 1]
}
/**
* If corner already has at least 3 surrounding edges, fill
* the corner and any remaining edges.
* @param start {[number, number]}
* @param end {[number, number]}
*/
fillCorners = ([x1, y1], [x2, y2]) => {
const corner = []
const edges = []
if (x1 < x2) {
corner.push([1, -1], [1, -2], [2, -1], [2, -2])
edges.push([0, -1], [3, -1], [1, -3])
} else if (x1 > x2) {
corner.push([-1, -1], [-1, -2], [-2, -1], [-2, -2])
edges.push([0, -1], [-3, -1], [-1, -3])
} else if (y1 < y2) {
corner.push([-1, 1], [-2, 1], [-1, 2], [-2, 2])
edges.push([-1, 0], [-1, 3], [-3, 1])
} else if (y1 > y2) {
corner.push([-1, -1], [-2, -1], [-1, -2], [-2, -2])
edges.push([-1, 0], [-1, -3], [-3, -1])
}
if (
edges.filter(([x, y]) => this.getBit(x1 * 3 + 1 + x, y1 * 3 + 1 + y))
.length >= 2
) {
console.log('---', [x1, y1], [x2, y2])
console.log(corner)
for (const [a, b] of [...corner, ...edges]) {
this.setBit(x1 * 3 + 1 + a, y1 * 3 + 1 + b)
}
}
}
getBit = (x, y) => {
return x >= 0 && x < this.width * 3 && y >= 0 && y < this.height * 3
? this.data[y][x]
: 0
}
setBit = (x, y) => {
if (x >= 0 && x < this.width * 3 && y >= 0 && y < this.height * 3) {
this.data[y][x] = 1
}
}
/**
* @param start {[number, number]}
* @param end {[number, number]}
*/
setPath = (start, end) => {
const [x1, y1] = start
const xs = [x1 * 3 + 1]
const ys = [y1 * 3 + 1]
if (end) {
const [x2, y2] = end
if (x1 < x2) {
xs.push(xs[0] + 1, xs[0] + 2, xs[0] + 3)
} else if (x1 > x2) {
xs.push(xs[0] - 1, xs[0] - 2, xs[0] - 3)
}
if (y1 < y2) {
ys.push(ys[0] + 1, ys[0] + 2, ys[0] + 3)
} else if (y1 > y2) {
ys.push(ys[0], ys[0] - 1, ys[0] - 2, ys[0] - 3)
}
}
for (const x of xs) {
for (const y of ys) {
this.setBit(x, y)
}
}
if (end) {
this.fillCorners(start, end)
}
}
/**
* @param point {[number, number]}
* @param maxX {number}
* @param maxY {number}
*/
getAdjacent = ([x, y], maxX, maxY) => {
const adjacent = []
for (const [a, b] of [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1],
]) {
if (a >= 0 && a < maxX && b >= 0 && b < maxY) {
adjacent.push([a, b])
}
}
return adjacent
}
generate = () => {
this.init()
const visited = [[randomI(this.width), randomI(this.height)]]
while (visited.length) {
const currentI =
randomI(10) < 1 ? randomI(visited.length) : visited.length - 1
const current = visited[currentI]
const emptyAdjacent = this.getAdjacent(
current,
this.width,
this.height,
).filter(this.isCellEmpty)
if (emptyAdjacent.length === 0) {
visited.splice(currentI, 1)
continue
}
const nextI = randomI(emptyAdjacent.length)
const next = emptyAdjacent[nextI]
this.setPath(current, next)
visited.push(next)
}
}
}
const map = new BlobMap(20, 20, 24)
document.getElementById('refresh').onclick = refresh
refresh()
function refresh() {
map.generate()
renderMap(map)
}
function randomI(i) {
return Math.floor(Math.random() * i)
}
/**
* @param map {BlobMap}
*/
function renderMap(map) {
for (let row = 0; row < map.data.length / 3; ++row) {
for (let col = 0; col < map.data[0].length / 3; ++col) {
renderTile(col, row, map)
}
}
}
/**
* @param col {number}
* @param row {number}
* @param map {BlobMap}
*/
function renderTile(col, row, { data, tileSize, tileBitSize }) {
const x = col * tileSize
const y = row * tileSize
// ctx.strokeStyle = '#fff'
ctx.lineWidth = 1
ctx.strokeRect(x, y, tileSize, tileSize)
for (let c = 0; c < 3; ++c) {
for (let r = 0; r < 3; ++r) {
ctx.fillStyle = colors[data[row * 3 + r][col * 3 + c]]
ctx.lineWidth = 1
ctx.fillRect(
x + tileBitSize * c,
y + tileBitSize * r,
tileBitSize,
tileBitSize,
)
}
}
}