-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (181 loc) · 5.6 KB
/
index.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
const { Engine, Render, Runner, World, Bodies, Body, Events } = Matter;
const cellsHorizontal = 14;
const cellsVertical = 10;
const width = window.innerWidth;
const height = window.innerHeight;
const unitLengthX = width / cellsHorizontal;
const unitLengthY = height / cellsVertical;
const engine = Engine.create();
engine.world.gravity.y = 0;
const { world } = engine;
const render = Render.create({
element: document.body, engine: engine, options: {
wireframes: false,
width, height
}
});
Render.run(render);
Runner.run(Runner.create(), engine);
// Wall
const walls = [
Bodies.rectangle(width / 2, 0, width, 2, { isStatic: true }),
Bodies.rectangle(width / 2, height, width, 2, { isStatic: true }),
Bodies.rectangle(0, height / 2, 2, height, { isStatic: true }),
Bodies.rectangle(width, height / 2, 2, height, { isStatic: true })
];
World.add(world, walls);
// Maze Generation
// for (let i = 0;i < 3;i++) {
// grid.push([]);
// for (let j = 0;j < 3;j++) {
// grid[i].push(false);
// }
// }
const shuffle = (arr) => {
let counter = arr.length;
while (counter > 0) {
const index = Math.floor(Math.random() * counter);
counter--;
const temp = arr[counter];
arr[counter] = arr[index];
arr[index] = temp;
}
return arr;
};
const grid = Array(cellsVertical).fill(null).map(() => Array(cellsHorizontal).fill(false));
const verticals = Array(cellsVertical)
.fill(null)
.map(() => Array(cellsHorizontal - 1).fill(false));
const horizontals = Array(cellsVertical - 1)
.fill(null)
.map(() => Array(cellsHorizontal).fill(false));
const startRow = Math.floor(Math.random() * cellsVertical);
const startColumn = Math.floor(Math.random() * cellsHorizontal);
const stepThroughCell = (row, column) => {
// If i have visted the cell at [row, column], then return
if (grid[row][column]) {
return;
}
// Mark this cell as being visited
grid[row][column] = true;
// Assemble randomly-ordered list of neighbors
const neighbors = shuffle([
[row - 1, column, 'up'],
[row, column + 1, 'right'],
[row + 1, column, 'down'],
[row, column - 1, 'left']
]);
// For each neighbor....
for (let neighbor of neighbors) {
const [nextRow, nextColumn, direction] = neighbor;
// See if that neighbor is out of bounds
if (
nextRow < 0 ||
nextRow >= cellsVertical ||
nextColumn < 0 ||
nextColumn >= cellsHorizontal
) {
continue;
}
// If we have visited that neighbor, continue to next neighbor
if (grid[nextRow][nextColumn]) {
continue;
}
// Remove a wall from either horizontals or verticals
if (direction === 'left') {
verticals[row][column - 1] = true;
} else if (direction === 'right') {
verticals[row][column] = true;
} else if (direction === 'up') {
horizontals[row - 1][column] = true;
} else if (direction === 'down') {
horizontals[row][column] = true;
}
// Visit that next cell
stepThroughCell(nextRow, nextColumn);
}
};
stepThroughCell(startRow, startColumn);
horizontals.forEach((row, rowIndex) => {
row.forEach((open, columnIndex) => {
if (open) {
return;
}
const wall = Bodies.rectangle(columnIndex * unitLengthX + unitLengthX / 2, rowIndex * unitLengthY + unitLengthY, unitLengthX, 10, {
label: 'wall', isStatic: true, render: {
fillStyle: '#FADBD8'
}
});
World.add(world, wall);
});
});
verticals.forEach((row, rowIndex) => {
row.forEach((open, columnIndex) => {
if (open) {
return;
}
const wall = Bodies.rectangle(columnIndex * unitLengthX + unitLengthX, rowIndex * unitLengthY + unitLengthY / 2, 10, unitLengthY, {
label: 'wall', isStatic: true, render: {
fillStyle: '#FADBD8'
}
});
World.add(world, wall);
})
});
// GOAL
const goal = Bodies.rectangle(
width - unitLengthX / 2,
height - unitLengthY / 2,
unitLengthX * 0.7,
unitLengthY * 0.7,
{
label: 'goal',
isStatic: true,
render: {
fillStyle: '#CB4335'
}
}
);
World.add(world, goal);
// BALL
const ballRadius = Math.min(unitLengthX, unitLengthY) / 4;
const ball = Bodies.circle(unitLengthX / 2, unitLengthY / 2, ballRadius, {
label: 'ball',
render: {
fillStyle: '#F1948A'
}
});
World.add(world, ball);
document.addEventListener('keydown', (event) => {
const { x, y } = ball.velocity;
if (event.keyCode === 38) {
Body.setVelocity(ball, { x, y: y - 5 });
}
if (event.keyCode === 39) {
Body.setVelocity(ball, { x: x + 5, y });
}
if (event.keyCode === 40) {
Body.setVelocity(ball, { x, y: y + 5 });
}
if (event.keyCode === 37) {
Body.setVelocity(ball, { x: x - 5, y });
}
});
// WIN CONDITION
Events.on(engine, 'collisionStart', event => {
event.pairs.forEach(collision => {
const labels = ['ball', 'goal'];
if (
labels.includes(collision.bodyA.label) &&
labels.includes(collision.bodyB.label)
) {
document.querySelector('.winner').classList.remove('hidden');
world.gravity.y = 1;
world.bodies.forEach(body => {
if (body.label === 'wall') {
Body.setStatic(body, false);
}
});
}
});
});