xxxxxxxxxx
74
class Conveyor {
constructor(x, y, direction, width, length, speed, color) {
this.x = x;
this.y = y;
this.direction = direction; // Use degrees directly
this.width = width;
this.length = length;
this.speed = speed;
this.color = color;
this.segments = [];
this.numSegments = int(random(10, 17)); // Random number of segments
this.segmentLength = this.length / this.numSegments;
for (let i = 0; i < this.numSegments; i++) {
let segLength = i / this.numSegments > 0.9 ?
this.segmentLength * (1 - (i - this.numSegments * 0.9) / (this.numSegments * 0.1)) :
this.segmentLength;
this.segments.push({x: i * this.segmentLength, length: segLength});
}
}
update() {
// Move the belt
for (let segment of this.segments) {
segment.x -= this.speed;
// Wrap around
if (segment.x < -this.segmentLength) {
segment.x = this.length - this.segmentLength;
}
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.direction);
fill(this.color);
// Draw the conveyor belt segments
for (let segment of this.segments) {
rect(segment.x - this.length / 2, -this.width / 2, segment.length, this.width);
}
pop();
}
}
var c1,c2,c3,c4;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES)
c1 = new Conveyor(50, 0, -90, 50, 100, 1, color('red'))
c2 = new Conveyor(0, 100, 180, 35, 100, 1, color('yellow'))
c3 = new Conveyor(width, 240, 0, 200, 50, 1, color('purple'))
c4 = new Conveyor(210, height, 90, 70, 50, .135, color('green'))
}
function draw() {
background(220);
// hhhhhhh
c1.update()
c1.display()
c2.update()
c2.display()
c3.update()
c3.display()
c4.update()
c4.display()
}