xxxxxxxxxx
52
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
let v;
let desired;
let targets = [];
function setup() {
createCanvas(640, 360);
desired = createVector(width/2, height/2);
makeTargets();
v = new Vehicle(targets.length, random(width), random(height));
}
function makeTargets() {
targets = [];
for (let i = 0; i < 8; i++) {
targets.push(createVector(random(width), random(height)))
}
}
function draw() {
background(220);
// Draw a rectangle to show the Vehicle's goal
rectMode(CENTER);
stroke(0);
strokeWeight(2);
fill(0, 100);
rect(desired.x, desired.y, 36, 36);
// Draw the targets
targets.forEach( target => {
fill(0, 100);
stroke(0);
strokeWeight(2);
ellipse(target.x, target.y, 30, 30);
});
// Update the Vehicle
v.steer(targets);
v.update();
v.display();
}
function mousePressed() {
makeTargets();
}