-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersongrid.js
46 lines (46 loc) · 1.5 KB
/
persongrid.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
class Grid {
constructor(res) {
this.rows = floor(height / res);
this.cols = floor(width / res);
this.res = res;
this.personarray = new Array(this.rows);
for(let i = 0; i < this.personarray.length; i++) {
this.personarray[i] = new Array(this.cols);
for(let j = 0; j < this.cols; j++) {
this.personarray[i][j] = [];
for(let k = 0; k < floor(random(1, 10)); k++) {
this.personarray[i][j].push(new Person());
}
}
}
}
update() {
let x = floor(random(this.rows));
let y = floor(random(this.cols));
let a = floor(random(this.personarray[x][y]));
let tx = floor(random(this.rows));
let ty = floor(random(this.cols));
for(let i = 0; i < a; i++) {
this.move(x, y, tx, ty, a);
}
}
move(x, y, tx, ty, a) {
let container = [];
for(let i = a; i >= 0; i--) {
container.push(this.personarray[x][y][i].copy());
this.personarray.splice(i, 1);
}
for(let i = 0; i < container.length; i++) {
this.personarray[tx][ty].push(container[i]);
}
}
show() {
stroke(255);
noFill();
for(let j = 0; j < this.rows; j++) {
for(let i = 0; i < this.cols; i++) {
rect(i * this.res, j * this.res, this.res, this.res);
}
}
}
}