xxxxxxxxxx
198
let individuals = []; // array to hold individual objects
let Exist = false;
let CanvasHeight = 600;
let TotalPopulation=0;
let KnownPopulation = 0;
let LastConnectedPopulation = 0;
let FrameRate = 30;
let ConnectedCount =0;
function setup() {
createCanvas(windowWidth, CanvasHeight);
noStroke();
}
function draw() {
background(50);
let t = frameCount / FrameRate; // update time
frameRate(FrameRate);
let BornRate = 0.75;
// create a individuals each frame according to bornrate
if(random(1)<BornRate){
individuals.push(new individual()); // append
if(Exist == true){
TotalPopulation++; //record population
}
}
for(let i = 0;i<individuals.length;i++) {
individuals[i].joinIndividuals(individuals.slice(i));
}
// loop through individuals with a for..of loop
for (let people of individuals) {
people.update(t); // update individual position
people.display(t); // draw individual
}
// if (mouseIsPressed === true) {
// individuals[1].self(individuals);
//}
}
function mouseReleased() {
if(Exist == false){
let Meball = new individual();
Meball.isMe = true;
Meball.size = 20;
individuals.push(Meball); // append
Exist = true;
}
}
// individual class
class individual{
constructor(){
// initialize coordinates
this.posX = 0;
this.posY = random(-50, 0);
this.initialangle = random(0, 2 * PI);
this.size = random(2, 15);
this.initialR = random(0, 255);
this.initialG = random(0, 255);
this.initialB = random(0, 255);
this.initialA = 255;
this.FamiliarRGBA = [0,0,255,255];
this.Familiar = false;
this.AgeFactor = random(1,2);
this.isMe = false;
this.HorizontalSpeed = 3; //横向移动速度
this.VerticalSpeed = 4; //纵向移动速度
this.CurrentHeight = CanvasHeight/2;
this.isConnected = false;
// radius of individual spiral
// chosen so the individuals are uniformly spread out in area
this.radius = sqrt(random(pow(CanvasHeight / 2, 2)));
}
update(time){
// x position follows a circle
let w = 0.5; // angular speed
let angle = w * time + this.initialangle;
// different size individuals fall at slightly different y speeds
if(this.isMe == false){
this.posX += pow(this.size, 0.5);
this.posY = CanvasHeight / 2 + this.radius * sin(angle);
}else{
//Controll your ball movement
this.posX += this.HorizontalSpeed;
if (keyIsDown(UP_ARROW) && this.CurrentHeight >=0){
this.CurrentHeight-=this.VerticalSpeed;
} else if (keyIsDown(DOWN_ARROW)&&this.CurrentHeight <= CanvasHeight) {
this.CurrentHeight+=this.VerticalSpeed;
}
this.posY = this.CurrentHeight;
}
// delete individual if past end of screen
if (this.posX > width) {
if(this.isMe == true){
Exist = false;
console.log('Total Population:' + TotalPopulation); //显示生死间的总人口
console.log('Total Known Population:' + KnownPopulation); //认识过的总人口
console.log('Last Known Population:' + this.CountLastKnown(individuals)); //最后一刻的连接数量
//console.log('Ever Connected:' + ConnectedCount); //建立过的连接
TotalPopulation= 0;
KnownPopulation= 0;
LastConnectedPopulation = 0;
}
let index = individuals.indexOf(this);
individuals.splice(index, 1);
}
}
display(time){
let tempA = this.initialA-75*(sin(time*this.AgeFactor)+1);
if(this.isMe == true){
tempA = this.initialA;
}
if(this.Familiar == true){
fill(this.FamiliarRGBA); //碰到的熟人变成蓝色
}else{
this.initialR += random([-5,5]);
this.initialG += random([-5,5]);
this.initialB += random([-5,5]);
fill(this.initialR,this.initialG,this.initialB,tempA); //陌生人逐渐变色
}
ellipse(this.posX, this.posY, this.size);
}
joinIndividuals(individuals) {
individuals.forEach(element =>{
let dis = dist(this.posX,this.posY,element.posX,element.posY);
if(dis<55) {
if(this.isMe ==true){ //如果碰到我则连上不同颜色的线
stroke('rgba(255,255,0,1)');
if(element.Familiar == false){
KnownPopulation ++;
}
element.Familiar = true; //设置成熟人
}else if(element.isMe ==true){
stroke('rgba(255,255,0,1)');
if(this.Familiar == false){
KnownPopulation ++;
}
this.Familiar = true;
}else{ //否则就是白线
stroke('rgba(255,255,255,0.1)');
}
line(this.posX,this.posY,element.posX,element.posY);
}
});
}
//计算最后的连接数量
CountLastKnown(individuals) {
individuals.forEach(element =>{
let dis = dist(this.posX,this.posY,element.posX,element.posY);
if(dis<55) {
if(this.isMe ==true){ //
LastConnectedPopulation++; //
}else if(element.isMe ==true){
LastConnectedPopulation++;
}
}
});
LastConnectedPopulation-=1;
return LastConnectedPopulation;
}
//测试连线
//self(individuals) {
// individuals.forEach(element =>{
// let dis = dist(mouseX,mouseY,element.posX,element.posY);
// if(dis<55) {
// stroke('rgba(255,255,0,1)');
// line(mouseX,mouseY,element.posX,element.posY);
// element.Familiar = true;
// }
// });
//}
}