-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (41 loc) · 1.11 KB
/
main.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
import SimulationSettings from './src/classes/SimulationSettings.js';
import WorldMap from './src/classes/WorldMap.js';
import AntColony from './src/classes/AntColony.js';
import Renderer from './src/classes/Renderer.js';
const params = new SimulationSettings();
const worldMap = new WorldMap(params);
const renderer = new Renderer('simulationCanvas', params);
function init() {
worldMap.init();
}
// Main loop function to update and render the simulation.
function mainLoop() {
update();
draw(renderer);
requestAnimationFrame(mainLoop, 200);
}
function update() {
updateColonies();
updateFood();
}
function updateColonies() {
worldMap.colonyParams.forEach((param) => {
const colony = worldMap.getColony(param.x, param.y);
if (colony) {
colony.update();
}
});
}
function updateFood() {
worldMap.foodParams.forEach((param) => {
const food = worldMap.getFood(param.x, param.y);
food?.update();
});
}
function draw(renderer) {
renderer.clearCanvas();
renderer.render(worldMap.grid);
}
// Start the simulation
init();
mainLoop();