xxxxxxxxxx
92
class FractalTree {
constructor(jitter, level) {
this.jitter = jitter;
this.leavesTiming = level;
this.init();
}
init() {
this.branchCount = 0;
this.branches = [];
this.leaves = [];
let start = createVector(width / 2, height);
let end = createVector(width / 2, height - (height / 4));
let thickness = 8;
let rootBranch = new Branch(start, end, thickness);
this.branches.push(rootBranch);
}
addBranches() {
// 왜 true가 정상작동 안하는지 모르겠네...
// 해결함: setup()에 넣어야할 초기화코드가 draw()에 넣어서 계속
// 초기화되고 있어서 버그가 났음
let bs = this.branches;
// add two branches
for (let i = bs.length - 1; i >= 0; i--) {
if (!bs[i].finished) {
bs.push(bs[i].makeRightBranch());
bs.push(bs[i].makeLeftBranch());
// bonus
let r = random(floor(random(4)));
if (r === 0) {
bs.push(bs[i].makeMiddleBranch());
}
bs[i].finished = true;
}
}
// branchCount++
this.branchCount++;
// init
if (this.branchCount === this.leavesTiming + 1) {
this.init();
}
// addLeaves
if (this.branchCount === this.leavesTiming) {
this.addLeaves();
}
}
addLeaves() {
for (let branch of this.branches) {
if (!branch.finished) {
let tEnd = branch.end;
let leaf = new Leaf(tEnd, this.leavesTiming);
this.leaves.push(leaf);
}
}
}
show() {
// branches
for (let branch of this.branches) {
branch.show();
if (this.jitter) {
branch.jitter();
}
}
// leaves
for (let leaf of this.leaves) {
leaf.show();
}
}
}