-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmazeRaycastedLight.js
156 lines (146 loc) · 4.62 KB
/
mazeRaycastedLight.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
// @ts-check
kaplay({
scale: 0.5,
background: [0, 0, 0],
});
loadSprite("bean", "sprites/bean.png");
loadSprite("steel", "sprites/steel.png");
const TILE_WIDTH = 64;
const TILE_HEIGHT = TILE_WIDTH;
function createMazeMap(width, height) {
const size = width * height;
function getUnvisitedNeighbours(map, index) {
const n = [];
const x = Math.floor(index / width);
if (x > 1 && map[index - 2] === 2) n.push(index - 2);
if (x < width - 2 && map[index + 2] === 2) n.push(index + 2);
if (index >= 2 * width && map[index - 2 * width] === 2) {
n.push(index - 2 * width);
}
if (index < size - 2 * width && map[index + 2 * width] === 2) {
n.push(index + 2 * width);
}
return n;
}
const map = new Array(size).fill(1, 0, size);
map.forEach((_, index) => {
const x = Math.floor(index / width);
const y = Math.floor(index % width);
if ((x & 1) === 1 && (y & 1) === 1) {
map[index] = 2;
}
});
const stack = [];
const startX = Math.floor(Math.random() * (width - 1)) | 1;
const startY = Math.floor(Math.random() * (height - 1)) | 1;
const start = startX + startY * width;
map[start] = 0;
stack.push(start);
while (stack.length) {
const index = stack.pop();
const neighbours = getUnvisitedNeighbours(map, index);
if (neighbours.length > 0) {
stack.push(index);
const neighbour =
neighbours[Math.floor(neighbours.length * Math.random())];
const between = (index + neighbour) / 2;
map[neighbour] = 0;
map[between] = 0;
stack.push(neighbour);
}
}
return map;
}
function createMazeLevelMap(width, height, options) {
const symbols = options?.symbols || {};
const map = createMazeMap(width, height);
const space = symbols[" "] || " ";
const fence = symbols["#"] || "#";
const detail = [
space,
symbols["╸"] || "╸", // 1
symbols["╹"] || "╹", // 2
symbols["┛"] || "┛", // 3
symbols["╺"] || "╺", // 4
symbols["━"] || "━", // 5
symbols["┗"] || "┗", // 6
symbols["┻"] || "┻", // 7
symbols["╻"] || "╻", // 8
symbols["┓"] || "┓", // 9
symbols["┃"] || "┃", // a
symbols["┫"] || "┫", // b
symbols["┏"] || "┏", // c
symbols["┳"] || "┳", // d
symbols["┣"] || "┣", // e
symbols["╋ "] || "╋ ", // f
];
const symbolMap = options?.detailed
? map.map((s, index) => {
if (s === 0) return space;
const x = Math.floor(index % width);
const leftWall = x > 0 && map[index - 1] == 1 ? 1 : 0;
const rightWall = x < width - 1 && map[index + 1] == 1 ? 4 : 0;
const topWall = index >= width && map[index - width] == 1 ? 2 : 0;
const bottomWall =
index < height * width - width && map[index + width] == 1
? 8
: 0;
return detail[leftWall | rightWall | topWall | bottomWall];
})
: map.map((s) => {
return s == 1 ? fence : space;
});
const levelMap = [];
for (let i = 0; i < height; i++) {
levelMap.push(symbolMap.slice(i * width, i * width + width).join(""));
}
return levelMap;
}
const level = addLevel(
createMazeLevelMap(15, 15, {}),
{
pos: vec2(100, 100),
tileWidth: TILE_WIDTH,
tileHeight: TILE_HEIGHT,
tiles: {
"#": () => [
sprite("steel"),
tile({ isObstacle: true }),
// area()
],
},
},
);
const bean = level.spawn(
[
sprite("bean"),
anchor("center"),
pos(32, 32),
tile(),
agent({ speed: 640, allowDiagonals: true }),
"bean",
],
1,
1,
);
onClick(() => {
const pos = level.fromScreen(mousePos());
bean.setTarget(vec2(
Math.floor(pos.x / TILE_WIDTH) * TILE_WIDTH + TILE_WIDTH / 2,
Math.floor(pos.y / TILE_HEIGHT) * TILE_HEIGHT + TILE_HEIGHT / 2,
));
});
onUpdate(() => {
const pts = [bean.pos];
// This is overkill, since you theoretically only need to shoot rays to grid positions
for (let i = 0; i < 360; i += 1) {
const hit = level.raycast(bean.pos, Vec2.fromAngle(i).scale(64 * 15));
if (hit) pts.push(hit.point);
}
pts.push(pts[1]);
drawPolygon({
pos: vec2(100, 100),
pts: pts,
color: rgb(255, 255, 100),
});
});