xxxxxxxxxx
88
class ComplexCreature {
constructor() {
this.pos = createVector(width/2, height/2);
this.vel = createVector(2, 0);
this.segments = [];
this.numSegments = 8;
// Initialize segments
for (let i = 0; i < this.numSegments; i++) {
this.segments.push(new Segment(this.pos.x - i * 30, this.pos.y, i));
}
// Head properties
this.antennaePhase = 0;
this.eyePhase = 0;
}
update() {
// Update velocity with sine wave for organic movement
this.vel.x = cos(frameCount * 0.02) * 3 + 2;
// Update position
this.pos.add(this.vel);
// Wrap around screen
if (this.pos.x > width + 50) {
this.pos.x = -50;
for (let segment of this.segments) {
segment.pos.x = this.pos.x - (segment.index * 30);
}
}
// Update antennae and eye movement
this.antennaePhase = frameCount * 0.1;
this.eyePhase = frameCount * 0.05;
// Update segments
let speed = this.vel.mag();
let leadPos = this.pos.copy();
for (let segment of this.segments) {
segment.update(leadPos, speed);
leadPos = segment.pos.copy();
}
}
display() {
// Draw segments
for (let segment of this.segments) {
segment.display(this.vel.mag());
}
this.drawHead();
}
drawHead() {
push();
translate(this.pos.x, this.pos.y);
// Head
fill(100, 160, 210);
noStroke();
ellipse(0, 0, 40, 40);
stroke(70, 130, 180);
strokeWeight(2);
let antLeft = createVector(-15, -15);
let antRight = createVector(15, -15);
let antWave = sin(this.antennaePhase) * 10;
line(antLeft.x, antLeft.y,
antLeft.x - 20 + antWave, antLeft.y - 20);
line(antRight.x, antRight.y,
antRight.x + 20 + antWave, antRight.y - 20);
// Eyes
let eyeX = sin(this.eyePhase) * 3;
fill(255);
ellipse(-10 + eyeX, -5, 12, 12);
ellipse(10 + eyeX, -5, 12, 12);
fill(0);
ellipse(-10 + eyeX, -5, 6, 6);
ellipse(10 + eyeX, -5, 6, 6);
pop();
}
}