xxxxxxxxxx
78
let MARGIN = 128;
let CITY_SIZE = 600;
class Road {
constructor(level, position, angle) {
this.level = level;
this.position = position.copy();
this.angle = angle;
this.building = true;
}
update() {
if (!this.building) {
return;
}
this.position.add(cos(this.angle)*this.level, sin(this.angle)*this.level);
if (red(get(this.position.x, this.position.y)) >= 80) {
this.building = false;
}
if (this.position.x > width - MARGIN ||
this.position.y > height - MARGIN ||
this.position.x < MARGIN ||
this.position.y < MARGIN) {
this.building = false;
}
if (random() > 0.99) {
this.angle += random([-PI/4, -PI/8, PI/8, PI/4]);
}
if (random() > 0.99 && this.level == 5) {
roads.push(new Road(this.level, this.position, this.angle + random([PI/2, -PI/2]) + random(-0.01,0.01)));
}
if (random() > 0.7 && this.level == 5) {
roads.push(new Road(this.level-2, this.position, this.angle + random([PI/2, -PI/2]) + random(-0.01,0.01)));
}
if (random() > 0.8 && this.level == 3) {
roads.push(new Road(this.level-2, this.position, this.angle + random([PI/2, -PI/2])));
}
if (random() > 0.98 && this.level == 3) {
roads.push(new Road(this.level, this.position, this.angle + random([PI/2, -PI/2])));
}
if (random() > 0.995 && this.level == 1) {
roads.push(new Road(this.level, this.position, this.angle + random([PI/2, -PI/2])));
}
}
draw() {
if (!this.building) {
return;
}
strokeWeight(this.level);
stroke(map(this.level, 1, 5, 100, 230));
point(this.position.x, this.position.y);
}
}
let roads = [];
function setup() {
createCanvas(CITY_SIZE + MARGIN*2, CITY_SIZE + MARGIN*2);
roads = [];
for (let i = 0; i < ceil(random(1, 5)); i++) {
roads.push(new Road(5, createVector(random(width/2, width/2), random(width/2, width/2)), random(TWO_PI)));
}
background(0);
stroke(230);
}
function draw() {
roads.forEach(road => { road.draw(); road.update(); });
}
function mousePressed() {
roads = [];
for (let i = 0; i < ceil(random(1, 5)); i++) {
roads.push(new Road(5, createVector(random(width/2, width/2), random(width/2, width/2)), random(TWO_PI)));
}
background(0);
}