p5js/demo_vehicle.js
2022-12-12 20:04:55 +08:00

94 lines
2.0 KiB
JavaScript

let vehicle1;
let vehicle2;
let path;
function setup() {
cursor(CROSS);
createCanvas(600, 600);
vehicle1 = new Vehicle(300, 300, "#CCCCCC");
vehicle1.velocity = createVector(6, 0);
vehicle2 = new Vehicle(300, 300, "#CC3333");
vehicle2.velocity = randomVector(6);
path = new Path(0, 300, 600, 300, 20);
}
function draw() {
追逐鼠标();
// 逃避鼠标();
// 小车追逐();
// 小车巡游();
// 小车巡线();
// 道路追逐();
}
function 追逐鼠标() {
background(64);
vehicle1.seek(createVector(mouseX, mouseY));
vehicle1.move();
vehicle1.turn();
vehicle1.drawPath();
vehicle1.show();
}
function 逃避鼠标() {
background(64);
vehicle1.flee(createVector(mouseX, mouseY));
vehicle1.move();
vehicle1.turn();
vehicle1.drawPath();
vehicle1.show();
}
function 小车追逐() {
background(64);
vehicle1.seek(vehicle2.position, 20);
vehicle2.move();
vehicle1.move();
if (vehicle2.turn()) vehicle2.velocity = randomVector(random(2, 5));
vehicle1.turn();
vehicle1.drawPath();
vehicle2.show("point");
vehicle1.show();
}
function 小车巡游() {
background(64);
vehicle1.wander();
vehicle1.move();
vehicle1.turn();
vehicle1.drawPath();
vehicle1.show();
}
function 小车巡线() {
background(0);
path.end.y = mouseY;
vehicle1.follow(path, 20);
vehicle1.move();
vehicle1.turn();
path.show();
vehicle1.drawPath();
vehicle1.show();
}
/**
* 未完成
*/
function 道路追逐() {
background(0);
// path.end.y = mouseY;
vehicle2.follow(path, 50);
vehicle1.seek(vehicle2.position, 100);
vehicle2.flee(vehicle1.position, 100);
vehicle2.move();
vehicle1.move();
vehicle2.turn();
vehicle1.turn();
path.show();
vehicle2.drawPath();
vehicle1.drawPath();
vehicle2.show();
vehicle1.show();
}