xxxxxxxxxx
111
class Stem {
constructor(x, y, links, growChance, branchDir, isRoot=false){
this.pos = createVector(x,y);
this.links = links;
this.id = stems.length;
this.growChance = growChance;
this.branchDir = branchDir;
this.isRoot = isRoot;
this.alive = true;
this.restLength = stemLength;
this.externalPull = createVector(0,0);
this.flowers = [];
this.leaves = [];
for(let i=0; i<this.links.length; i++){
stems[this.links[i]].links.push(this.id);
}
stemcount++;
}
drawLinks(){
if(!this.alive){
return;
}
for(let i=0;i<this.links.length;i++){
line(this.pos.x, this.pos.y, stems[this.links[i]].pos.x, stems[this.links[i]].pos.y);
}
}
pushPull(){
let move = createVector(0,0);
let linkcount = this.links.length + 1;
if(this.isRoot){
return;
}
for(let i=0;i<this.links.length;i++){
let p = createVector( stems[this.links[i]].pos.x, stems[this.links[i]].pos.y );
move.add( p5.Vector.mult(
p5.Vector.sub(this.pos, p), // the vector towards the linked node
(this.restLength / p5.Vector.dist(this.pos, p)) - 1.0 ) // how close the current lengh is to the rest length
);
}
move.div(linkcount*pullstepdiv)
this.pos.add(move);
this.pos.add(this.externalPull);
this.externalPull = createVector(0,0);
this.pos.add(this.branchDir);
// this.pos.add(createVector(0,-0.15));
}
grow(){
if(!this.alive){
return;
}
//die if energy is low FIX THIS ITS TOO ROUGH RN
if(this.links.length == 1 && random() < (stemcount*4 - pEnergy)*0.02 && stemcount>10 && this.leaves.length == 0){
stemcount--;
this.alive = false;
for (var i = 0; i < stems[this.links[0]].links.length; i++) {
if (stems[this.links[0]].links[i]== this.id) {
stems[this.links[0]].links.splice(i,1);
}
}
return;
}
// grow stem?
if(random() < this.growChance * branchingFalloff**(this.links.length-1)) {
stems.push(new Stem(
this.pos.x,
this.pos.y,
[this.id],
growthFalloff * this.growChance,
createVector( (random() - 0.5) * (this.links.length - 0.9) * horSpread, -1.0 ).setMag(0.5)
));
}
//or leaf
else if (random() < leafChance * leavesFalloff**(this.leaves.length-1) && !this.isRoot){
leaves.push(new Leaf(
this.id,
this.branchDir.heading() + (random()-0.5)*PI,
5
));
}
//or flower
// else if (random() < 1 - this.pos.y/height + 0.01*(energy-flowers.length*100)){
else if (false){
flowers.push(new Flower(
this.id,
this.branchDir.heading() + (random()-0.5)*PI,
20,
ceil(12*random())
));
print('flower!')
}
}
}