-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
62 lines (54 loc) · 1.45 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
initPOP();
drawPlots();
var terrain = Bodies.fromVertices(6000, 600, [verticies], {
id: -1,
isStatic: true
});
World.add(world, terrain);
document.getElementById('mutationRate').innerHTML = 'Mutation rate: '+MUTATIONRATE*100+"%";
// Main Loop
Matter.Events.on(engine, "beforeUpdate", () => {
genTag.innerHTML = "Generation: " + gens;
aliveTag.innerHTML = "Alive: " + agentsAlive + " of " + POPSIZE;
timerTag.innerHTML = "Timer: "+(TIMER-timer);
if (agentsAlive > 0 && timer < TIMER) {
var bestAgent = agents[0];
agents.forEach(Agent => {
Agent.run();
if (Agent.rect.position.x > bestAgent.rect.position.x && Agent.alive) {
bestAgent = Agent;
}
});
Matter.Render.lookAt(render, bestAgent.rect, { x: 400, y: 200 });
timer++;
} else {
agents.forEach(Agent => {
Agent.kill();
});
avarageScores.push(neat.getAverage());
drawPlots();
neat.evolve();
initPOP();
reset();
}
});
Matter.Events.on(engine, 'collisionStart', (event) => {
event.pairs.forEach((collision) => {
agents.forEach(Agent => {
if (collision.bodyB.id == Agent.rect.id) {
Agent.kill();
} else if (collision.bodyB.id === Agent.circle.id) {
Agent.isTouchingGround = !Agent.isTouchingGround;
}
});
});
});
Matter.Events.on(engine, 'collisionEnd', (event) => {
event.pairs.forEach((collision) => {
agents.forEach(Agent => {
if (collision.bodyB.id === Agent.circle.id) {
Agent.isTouchingGround = !Agent.isTouchingGround;
}
});
});
});