-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameLoop.js
More file actions
55 lines (42 loc) · 1.58 KB
/
gameLoop.js
File metadata and controls
55 lines (42 loc) · 1.58 KB
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
let frameCounter = 0;
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (const organism of organisms) {
organism.move();
organism.consumeFood();
organism.draw();
}
for (const foodSource of foodSources) {
foodSource.draw();
}
if (Math.random() < newOrganismFrequency) {
addRandomOrganism();
}
if (Math.random() < newFoodSourceFrequency) {
addRandomFoodSource();
}
const { redCount, greenCount, blueCount } = countByDominantColor(organisms, organism => organism.color);
organismsDataRed.push(redCount);
organismsDataGreen.push(greenCount);
organismsDataBlue.push(blueCount);
// Remove the oldest entry when the array length exceeds 100
[organismsDataRed, organismsDataGreen, organismsDataBlue].forEach(data => {
if (data.length > 1000) {
data.shift();
}
});
const { redCount: foodRedCount, greenCount: foodGreenCount, blueCount: foodBlueCount } = countByDominantColor(foodSources, foodSource => foodSource.colorPreference);
foodSourcesDataRed.push(foodRedCount);
foodSourcesDataGreen.push(foodGreenCount);
foodSourcesDataBlue.push(foodBlueCount);
// Remove the oldest entry when the array length exceeds 100
[foodSourcesDataRed, foodSourcesDataGreen, foodSourcesDataBlue].forEach(data => {
if (data.length > 1000) {
data.shift();
}
});
frameCounter++;
document.getElementById('frameCounter').innerText = frameCounter;
requestAnimationFrame(gameLoop);
}
gameLoop();