Ascension

S05E03

Log in to post a comment.

Canvas.setpenopacity(-0.1);

const size = 100;

let turtles = [];
let j, nt;

Turtle.prototype.withinBounds = function() {
    return this.x() > this.bounds[0] && this.x() < this.bounds[1] && this.y() > this.bounds[2] && this.y() < this.bounds[3];
}

Turtle.prototype.setBounds = function(...args) {
    this.bounds = [...args];
}

const spawnTurtle = (x = 0, y = size/6) => {
    const turtle = new Turtle();
    turtle.penup();
    turtle.goto(x, y);
    turtle.seth(270);
    turtle.energy = 20;
    turtle.step = 2;
    turtle.root = true;
    turtle.angle = 30;
    turtle.setBounds(-size, size, -size, size);
    turtle.pendown();
    turtles.push(turtle);
}

spawnTurtle();

function walk(i) {
    turtles.map(turtle => {
        turtle.forward(turtle.energy/100+turtle.step*0.125);
        if (i%21==0) {
            for (j = 0; j < 2; j++) {
                nt = turtle.clone();
                nt.energy = turtle.energy*0.7;
                nt.step = 2.5;
                nt.angle = i%2==0 ? turtle.angle/4 : turtle.angle*8;
                j%2==0 ? nt.left(nt.angle) : nt.right(nt.angle);
                nt.setBounds(...turtle.bounds);
                turtles.push(nt);
            }
        }
        turtle.energy *= 0.994;
    });
    turtles = turtles.filter(turtle => turtle.energy > 0.75 && turtle.withinBounds());
    return i < 260;
}