update
This commit is contained in:
parent
d1d5aeacfe
commit
2784cac35b
36
vehicle.js
36
vehicle.js
@ -51,7 +51,7 @@ function 小车追逐() {
|
||||
if (vehicle2.turn()) vehicle2.velocity = randomVector(random(2, 5));
|
||||
vehicle1.turn();
|
||||
vehicle1.drawPath();
|
||||
vehicle2.show();
|
||||
vehicle2.show("point");
|
||||
vehicle1.show();
|
||||
}
|
||||
|
||||
@ -67,15 +67,14 @@ function randomVector(magnitude) {
|
||||
|
||||
class Vehicle {
|
||||
constructor(x, y, color) {
|
||||
this.path = [];
|
||||
this.color = color;
|
||||
this.position = createVector(x, y);
|
||||
this.path = [];
|
||||
this.pathes = [];
|
||||
this.velocity = createVector(0, 0);
|
||||
this.heading = this.velocity.heading();
|
||||
this.acceleration = createVector(0, 0);
|
||||
this.maxVelocity = 5;
|
||||
this.maxAcceleration = 1;
|
||||
this.maxAcceleration = 0.25;
|
||||
}
|
||||
|
||||
seek(target, slowdownDistance = 100) {
|
||||
@ -144,12 +143,12 @@ class Vehicle {
|
||||
while (x < 0 || x >= width) x = (x + width) % width;
|
||||
while (y < 0 || y >= height) y = (y + height) % height;
|
||||
const edgeCrossed = x != this.position.x || y != this.position.y;
|
||||
if (edgeCrossed) {
|
||||
this.pathes.push(this.path);
|
||||
this.path = [];
|
||||
}
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
if (edgeCrossed) {
|
||||
this.path.pop();
|
||||
this.path.push(null);
|
||||
}
|
||||
return edgeCrossed;
|
||||
}
|
||||
|
||||
@ -180,23 +179,28 @@ class Vehicle {
|
||||
noFill();
|
||||
stroke(50);
|
||||
strokeWeight(1);
|
||||
if (this.pathes.length >= 64) this.pathes.shift();
|
||||
[...this.pathes, this.path].forEach((path) => {
|
||||
if (path.length >= 1024) path.shift();
|
||||
beginShape();
|
||||
path.forEach((v) => vertex(v.x, v.y));
|
||||
endShape();
|
||||
if (this.path.length >= 1024) this.path.shift();
|
||||
beginShape();
|
||||
this.path.forEach((v) => {
|
||||
if (!v) {
|
||||
endShape();
|
||||
beginShape();
|
||||
} else {
|
||||
vertex(v.x, v.y);
|
||||
}
|
||||
});
|
||||
endShape();
|
||||
pop();
|
||||
}
|
||||
|
||||
show() {
|
||||
show(mode = "triangle") {
|
||||
push();
|
||||
translate(this.position.x, this.position.y);
|
||||
rotate(this.heading);
|
||||
stroke(this.color);
|
||||
fill(this.color);
|
||||
triangle(0, 0, -10, 2.5, -10, -2.5);
|
||||
if (mode == "point") circle(0, 0, 5);
|
||||
else triangle(0, 0, -10, 2.5, -10, -2.5);
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user