-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
135 lines (113 loc) · 2.67 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
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
let CANVAS_WIDTH = 1200, CANVAS_HEIGHT = 550;
const windowOffset = 300;
const POP_SIZE = 50, INITIAL_FOODS = 50, INITIAL_POISONS = 25;
const MUTATION_RATE = 0.01, GENOCIDE_TIME = 15;
let food = [], poison = [];
let individuals;
function isInsideCircle(point, pos, radius) {
return pow(point.x - pos.x, 2) + pow(point.y - pos.y, 2) < pow(radius, 2);
}
function setup() {
frameRate(60);
//CANVAS_WIDTH = windowWidth - windowOffset;
//CANVAS_HEIGHT = windowHeight - windowOffset;
let canvas = createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.parent("canvasDiv");
individuals = new Population(MUTATION_RATE, POP_SIZE);
background(0);
for (let i = 0; i < INITIAL_FOODS; i++)
food.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
});
for (let i = 0; i < INITIAL_POISONS; i++)
poison.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
});
// Cria uma comida nova a cada intervalo
setInterval(_ => {
food.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
});
}, 125);
// Cria um veneno novo a cada intervalo
setInterval(_ => {
poison.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
});
}, 500);
// Diminui o raio de todos
setInterval(_ => {
for (let coiso of individuals.population) {
if (coiso.radius < 4)
coiso.die();
coiso.radius -= 0.5;
coiso.fitness += 1;
}
}, 1000);
// Genocidio
setInterval(_ => individuals.genocide(), GENOCIDE_TIME*1000);
}
function draw() {
background(0);
for (let i of food) {
fill(0, 255, 0);
noStroke();
circle(i.position.x, i.position.y, 2 * i.radius);
}
for (let j of poison) {
fill(255, 0, 0);
noStroke();
circle(j.position.x, j.position.y, 2 * j.radius);
}
if (individuals.population.length) {
for (let coiso of individuals.population) {
coiso.draw();
coiso.move();
coiso.behavior();
}
} else {
individuals.naturalSelection();
individuals.generate();
console.log(`Geração: ${individuals.generation}`);
background(0);
food = [];
for (let i = 0; i < INITIAL_FOODS; i++)
food.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
});
poison = [];
for (let i = 0; i < INITIAL_POISONS; i++)
poison.push({
position: createVector(
random(CANVAS_WIDTH),
random(CANVAS_HEIGHT)
),
radius: 4
})
}
}
function windowResized() {
//CANVAS_WIDTH = windowWidth - windowOffset;
//CANVAS_HEIGHT = windowHeight - windowOffset;
//resizeCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
}