xxxxxxxxxx
178
let bees = [];
function setup() {
createCanvas(800, 600);
bees.push(new NormalBee(width / 2, height / 2)); // Crear la primera abeja normal en el centro
}
function draw() {
background(200, 220, 255);
for (let bee of bees) {
bee.update();
bee.display();
}
if (mouseIsPressed) {
if (random(1) < 0.5) {
bees.push(new NormalBee(mouseX, mouseY)); // Crear nueva abeja normal en la posición del ratón
} else {
bees.push(new FancyBee(mouseX, mouseY)); // Crear nueva abeja fancy en la posición del ratón
}
}
}
class Bee {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 12.5; // Un cuarto del tamaño original (50 / 4)
this.wingSize = 5; // Un cuarto del tamaño original (20 / 4)
this.speed = 2;
this.wingAngle = 0;
this.wingDirection = 1;
this.angle = 0; // Ángulo de movimiento de la abeja
this.noiseOffset = random(1000); // Offset de ruido para cada abeja
}
update() {
this.followMouse();
this.separate();
this.flapWings();
}
followMouse() {
let targetX = mouseX + map(noise(this.noiseOffset), 0, 1, -50, 50);
let targetY = mouseY + map(noise(this.noiseOffset + 1000), 0, 1, -50, 50);
let dx = targetX - this.x;
let dy = targetY - this.y;
this.angle = atan2(dy, dx); // Actualizar el ángulo hacia el ratón
this.x += cos(this.angle) * this.speed;
this.y += sin(this.angle) * this.speed;
this.noiseOffset += 0.01; // Incrementar el offset de ruido para movimiento continuo
}
separate() {
let desiredSeparation = this.size * 2;
let steerX = 0;
let steerY = 0;
let count = 0;
for (let other of bees) {
if (other !== this) {
let d = dist(this.x, this.y, other.x, other.y);
if (d < desiredSeparation && d > 0) {
let diffX = this.x - other.x;
let diffY = this.y - other.y;
let angle = atan2(diffY, diffX);
diffX = cos(angle) / d;
diffY = sin(angle) / d;
steerX += diffX;
steerY += diffY;
count++;
}
}
}
if (count > 0) {
steerX /= count;
steerY /= count;
}
if (steerX !== 0 || steerY !== 0) {
this.angle = atan2(steerY, steerX);
this.x += cos(this.angle) * this.speed;
this.y += sin(this.angle) * this.speed;
}
}
flapWings() {
this.wingAngle += this.wingDirection * 0.1;
if (this.wingAngle > PI / 6 || this.wingAngle < -PI / 6) {
this.wingDirection *= -1;
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle + PI); // Rotar la abeja para que la cabeza apunte al ratón
this.drawBody();
this.drawWings();
this.drawHead();
pop();
}
drawWings() {
fill(255, 255, 255, 150);
this.drawWing(this.wingAngle, -this.size * 0.5);
this.drawWing(-this.wingAngle, this.size * 0.5);
}
drawWing(angle, offsetY) {
push();
rotate(angle);
ellipse(this.size * 0.5, offsetY, this.wingSize * 2, this.wingSize);
pop();
}
drawEyes() {
fill(0);
const eyeSize = this.size * 0.2;
ellipse(-this.size * 1.1, -this.size * 0.1, eyeSize, eyeSize);
ellipse(-this.size * 1.1, this.size * 0.1, eyeSize, eyeSize);
}
}
class NormalBee extends Bee {
drawBody() {
fill(255, 200, 0);
ellipse(0, 0, this.size * 1.5, this.size);
this.drawStripes();
}
drawStripes() {
fill(0);
const stripeWidth = this.size * 0.2;
const stripeHeight = this.size * 0.5;
rect(-this.size * 0.5, -this.size * 0.25, stripeWidth, stripeHeight);
rect(0, -this.size * 0.25, stripeWidth, stripeHeight);
rect(this.size * 0.5, -this.size * 0.25, stripeWidth, stripeHeight);
}
drawHead() {
fill(255, 200, 0);
ellipse(-this.size, 0, this.size * 0.75, this.size * 0.75);
this.drawEyes();
}
}
class FancyBee extends Bee {
drawBody() {
fill(255, 150, 0); // Color de cuerpo diferente para FancyBee
ellipse(0, 0, this.size * 1.5, this.size); // Usar una elipse similar a la NormalBee
this.drawStripes();
}
drawStripes() {
fill(0);
const stripeWidth = this.size * 0.2;
const stripeHeight = this.size * 0.5;
rect(-this.size * 0.5, -this.size * 0.25, stripeWidth, stripeHeight);
rect(0, -this.size * 0.25, stripeWidth, stripeHeight);
rect(this.size * 0.5, -this.size * 0.25, stripeWidth, stripeHeight);
}
drawWings() {
fill(255, 255, 255, 200);
this.drawWing(this.wingAngle, -this.size * 0.5);
this.drawWing(-this.wingAngle, this.size * 0.5);
}
drawHead() {
fill(255, 150, 0); // Color de cabeza diferente para FancyBee
ellipse(-this.size, 0, this.size * 0.75, this.size * 0.75); // Usar una elipse similar a la NormalBee
this.drawEyes();
}
}