xxxxxxxxxx
246
let myFireflies = [];
let howMany;
let pianoClip;
let pianoClip2;
let clips = [];
let spacing;
let osc ,env, decTime;
let oscTimer, oscTimerMax;
let size;
function preload() {
clips.push(loadSound("shortPianoC.mp3"));
clips.push(loadSound("shortPianoG.mp3"));
clips.push(loadSound("shortPianoE.mp3"));
clips.push(loadSound("shortPianoB.mp3"));
}
function setup() {
createCanvas(400, 400);
smooth(.5);
colorMode(HSB);
spacing = 7;
howMany = 5;
for (let i = 0; i < howMany; i++) {
myFireflies.push(new Firefly(random(1, 10), random(width), random(height)));
}
noStroke();
background(10);
//toggleNormalize(1);
}
function keyPressed() {
myFireflies.push(new Firefly(random(1, 10), 0, 0));
print("hi");
}
function draw() {
howMany = myFireflies.length;
//background(250);
background(10);
for (let i = 0; i < howMany; i++) {
myFireflies[i].update();
myFireflies[i].display();
myFireflies[i].playSound();
myFireflies[i].multiply();
}
}
class Firefly {
constructor(tmass, tx, ty) {
this.mass = tmass;
this.pos = createVector(tx, ty);
this.vel = createVector(10, 10);
this.acc = createVector(0, 0);
this.mScale = 1;
this.drag = createVector(0.5, 0.5);
this.c = color(random(0, 255), 200, 200);
//Movement
this.timer = 0;
this.timeMax = 100;
this.speed = 10;
this.myForce = createVector(random(-this.speed, this.speed), random(-this.speed, this.speed));
//Sound
this.clip = clips[round(random(0, 3))];
this.env = new p5.Envelope();
this.env.setADSR(0.005, 0.01, 0, 0.01);
this.env.setRange(1, 0);
this.osc = new p5.Oscillator();
this.osc.setType('sine');
this.osc.amp(this.env);
//this.osc.start();
this.osc.freq(800);
this.oscTimer = 0;
this.oscTimerMax = 0;
this.freqLow = random(600, 800);
this.freqHigh = random(900, 1000);
//multiplication
this.spawned = false;
this.lifeTimer =0;
this.jump();
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.vel.add(f);
}
update() {
//fill(255,20,40);
fill(this.c);
ellipse(mouseX, mouseY, spacing, spacing);
this.vel.div(1.02);
this.pos.add(this.vel);
this.acc.mult(0);
this.timer++;
if (this.timer > this.timeMax) {
this.jump();
}
//PACMANING
if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.x > width) {
this.pos.x = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
}
if (this.pos.y > height) {
this.pos.y = 0;
}
///
}
jump()
{
this.timer = 0;
this.speed = random(4, 20);
this.myForce = createVector(random(-this.speed, this.speed), random(-this.speed, this.speed));
this.vel = this.myForce;
this.timeMax = ceil(random(80, 120));
//this.clip.play();
}
display() {
//fill(240,200,0);
fill(this.c);
this.size = map(this.vel.mag(), 0, 15, 15, 5)
this.size = constrain(this.size, 5, 15);
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
playSound() {
this.oscTimerMax = map(this.vel.mag(), 0, 10, 30, 17);
//original frequency range is 900 to 1600
this.osc.freq(map(this.vel.mag(), 0, 10, this.freqLow, this.freqHigh));
this.mappedMaxVol = map(this.size,1,20,0.1,0.4);
this.clip.setVolume(map(this.vel.mag(), 0, 10, 0.1, this.mappedMaxVol));
this.oscTimer++;
if (this.oscTimer > this.oscTimerMax) {
this.clip.play();
this.clip.pan(map(this.pos.x, 0, width, -1.0, 1.0));
this.osc.pan(map(this.pos.x, 0, width, -1.0, 1.0));
this.env.play();
this.oscTimer = 0;
fill(100);
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
}
multiply() {
this.lifeTimer++;
//print(this.lifeTimer);
let fx = 0;
let fy = 0;
for (let i = 0; i < myFireflies.length; i++) {
fx = myFireflies[i].pos.x;
fy = myFireflies[i].pos.y;
if ((this.lifeTimer > 100 )&&(myFireflies.length < 20) &&this.pos.dist(myFireflies[i].pos) < 10 && (i != myFireflies.indexOf(this)))
{
myFireflies.push(new Firefly(random(1, 10), fx + random(spacing,spacing*2), fy + random(spacing,spacing*10)));
myFireflies[myFireflies.length - 1].c = color(hue(this.c) + random(0, 10), 200, 200);
}
}
}
}