xxxxxxxxxx
88
let cells = [];
let friction = 0.1;
function setup(){
createCanvas(700,700);
//let cell = new Cell();
//cells.push(cell);
for(let i = 0; i < 10; i++){
cells.push(new Cell());
}
}
function draw() {
background(200);
for(let i = 0; i < cells.length; i++){
cells[i].move();
cells[i].show();
}
}
function mouseClicked() {
for(let i = cells.length - 1; i >= 0; i--) {
if(cells[i].clicked(mouseX, mouseY) && cells[i].speed == 0){
let cellA = cells[i].mitosis();
let cellB = cells[i].mitosis();
let dir = random(0, Math.PI * 2);
cellA.dir = dir;
cellB.dir = dir - Math.PI;
cellA.speed = 3;
cellB.speed = 3;
cells.push(cellA);
cells.push(cellB);
cells.splice(i, 1);
}
}
}
function Cell(pos, r, c) {
this.speed = 0;
this.dir = 0;
if(pos){
this.pos = pos.copy();
}else{
this.pos = createVector(random(width), random(height));
}
this.r = r || 120;
this.c = c || color(random(100,255), 0, random(100, 255), 100);
this.move = function (){
var vel = p5.Vector.random2D();
this.pos.add(vel);
this.pos.x = this.pos.x + Math.sin(this.dir) * this.speed;
this.pos.y = this.pos.y + Math.cos(this.dir) * this.speed;
this.speed = this.speed - friction;
if (this.speed < 0){
this.speed = 0;
}
}
this.show = function (){
noStroke();
fill(this.c);
ellipse(this.pos.x, this.pos.y, this.r, this.r);
}
this.mitosis = function(){
let cellA = new Cell(this.pos, this.r * 0.8, this.c);
return cellA;
}
this.clicked = function(x, y) {
let d = dist(this.pos.x, this.pos.y, x, y);
if (d < this.r/2){
return true;
}else{
return false;
}
}
}