-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.js
41 lines (34 loc) · 976 Bytes
/
food.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
const food = {
map: {},
render: () => {
Object.entries(food.map).forEach(([x, yArray]) => {
Object.keys(yArray).forEach((y) => {
fill('gray');
ellipse(x * blockSize + blockSize / 2, y * blockSize + blockSize / 2, 10, 10);
})
});
},
generatePiece: () => {
const randomX = Math.floor(Math.random() * rowsAndCols);
const randomY = Math.floor(Math.random() * rowsAndCols);
food.map[randomX] = {
...food.map[randomX],
[randomY]: 'normal',
}
},
startGenerating: () => {
setTimeout(() => {
if (Math.random() > 0.5 && food.getFoodLength() < maxFoodAtOneTime) {
food.generatePiece();
}
food.startGenerating();
}, 1000)
},
getPiece: (x, y) => (food.map[x] || {})[y],
deletePiece: (x, y) => {
delete food.map[x][y];
},
getFoodLength: () => Object.values(food.map).reduce((totalSum, yArray) => {
return totalSum += Object.keys(yArray).length;
}, 0),
}