xxxxxxxxxx
369
//Dust Bunny Disco!
let buds = []; // array of creatures
let treats = []; //array of food
let enviro;
let wallpaperTexture;
let floorTexture;
let windowTexture;
let phonographImg;
let flowerImg;
var volume = 0.1;
let numCreatures= 5;
let numTreats = 10;
let startSong = false;
let excited = false;
let countJump = 0;
let yChange = 0;
let song, analyzer;
function preload(){
phonographImg = createImg("http://pngimg.com/uploads/gramophone/gramophone_PNG30.png","phonograph");
windowTexture = createImg("https://lh3.googleusercontent.com/proxy/aMLduj82sbixkOfSpHzVCLCcR4jt1LZAm1RK88mS9fK74cP3M7lVT9IP4_rG0oQ1JZaMYmKTG7cUERN2DEhBoYaeh92KIakozOxIbbegby_M1_53vYbK", "window");
wallpaperTexture = createImg("https://media.freestocktextures.com/cache/c2/25/c225c6880b78ca5a658ec53afaa84ac6.jpg", "wallpaper");
floorTexture = createImg( "https://media.freestocktextures.com/cache/c2/f1/c2f14e87ab32ecc59254100b0a07340b.jpg", "floor");
flowerImg = createImg("https://i.pinimg.com/originals/b6/1e/4c/b61e4ce751522a71c40d3e5602c9ec1d.png", "flower");
wallpaperTexture.hide();
floorTexture.hide();
windowTexture.hide();
phonographImg.hide();
flowerImg.hide();
song = loadSound("Jockers Dance Orchestra - The Royal Vagabond-[AudioTrimmer.com].mp3");
}
function setup() {
frameRate(30);
createCanvas(900, 640);
// Create objects
for (let i = 0; i < numCreatures; i++) {
buds.push(new bud());
}
for (let i=0; i< numTreats; i ++ ){
treats.push(new treat(random(0, 400), random(0,400)));
}
enviro = new environment();
}
function draw() {
background("tan");
enviro.display();
for (let i = 0; i < buds.length; i++) {
buds[i].live();
buds[i].display();
}
for(let i = 0; i < treats.length; i ++){
treats[i].gravity();
treats[i].display();
}
bunEat();
}
function mousePressed(){
treats.push(new treat(mouseX, mouseY));
numTreats += 1;
}
function bunEat(){
for (let i = 0; i < numCreatures; i++){
for(let j= 0; j< numTreats; j ++){
if (dist(treats[j].cx, treats[j].cy, buds[i].cx, buds[i].cy) <= buds[i].r){
treats[j].cx = -140;
buds[i].r +=5;
if (buds[i].r > 75){
numCreatures += 1;
buds[i].r = 30;
buds.push(new bud());
buds[buds.length - 1].cx = buds[i].cx;
buds[buds.length - 1].cy = buds[i].cy;
buds[buds.length - 1].xdir = buds[i].xdir * (-1);
}
}
}
}
}
class bud {
constructor() {
this.cx = random(width);
this.cy = random(0, 500);
this.tailcx = 0;
this.tailcy = 0;
this.r = random(30, 55);
this.xdir = random(-2, 2);
this.ydir = 0;
this.xmomentum = 0;
this.ymomentum = 0;
this.fill = "white";
this.furLayer = 9;
this.jump = false;
}
gravity() {
if (this.cy < 480) {
this.cy += 5;
}
}
friction(){
this.xmomentum = this.xmomentum/2
}
mouseCheck() {
if (dist(mouseX, mouseY, this.cx, this.cy) < 50) {
if (mouseX > this.cx){ this.xmomentum -= 10}
else {this.xmomentum += 10}
}
if ((dist(mouseX, mouseY, this.cx, this.cy) < this.r) && mouseIsPressed && (excited == false)) {
this.cx = mouseX;
this.cy = mouseY;
}
}
move(){
this.cx += this.xdir;
if (this.cx < 0 + this.r || this.cx > width - this.r){
this.xdir *= -1;
}
if (excited ==true ){
if(countJump < 61){
let yMax = -30;
let yFinal = yMax + yChange;
yChange += 1;
this.yMomentum = yFinal;
}
else if (countJump == 61){
countJump = 0;
this.yMomentum = 0;
this.cy = 500;
yChange = 0;
}
this.cy = this.cy + this.yMomentum;
countJump += 1;
}
}
live(){
this.mouseCheck();
this.gravity();
this.friction();
this.move();
}
drawBunny(x, y, r){
push();
let opacity = 255
fill(230, opacity);
noStroke();
push();
rectMode(CENTER);
rect(x - 8, y -20, 12, 80, 12);
rect(x + 8, y - 20, 12, 80, 12);
pop();
ellipse(x, y, r, r);
for(let i = 0; i< this.furLayer ; i ++){
fill(225, opacity - 20*i)
ellipse(x, y, r + 5*i);
}
//if (dist(mouseX, mouseY, this.cx, this.cy) < 200){this.bunnyFace(x, y);}
this.bunnyFace(x, y);
}
bunnyFace(x, y){
// slow down reaction
push();
frameRate(10);
let bx = x; //bunny x and y
let by = y;
let slopex;
let slopey;
if (dist(mouseX, mouseY, this.cx, this.cy) < 300){
slopex = (bx - mouseX);
slopey = (by - mouseY);
}
else if (this.xdir >= 0){
slopex = (bx - 700);
slopey = (by);
}
else{
slopex = bx + 700;
slopey = by;
}
let inBetween = (slopex**2 + slopey**2)**0.5;
let multiply = this.r / inBetween / 1.5;
let finalTailx = slopex * multiply;
let finalTaily = slopey * multiply;
//tail
var a = 0.9;
var b = 1.0 - a;
this.tailcx = finalTailx + this.cx;
this.tailcy = finalTaily + this.cy;
push();
fill(255, 180);
ellipse(this.tailcx, this.tailcy, 25, 25);
pop();
//face
push();
fill(78,52,35);
let eye1x = this.cx - finalTailx;
let eye1y = this.cy - finalTaily;
let eye2x = this.cx - finalTailx - 10;
let eye2y = this.cy - finalTaily;
if (excited == false){
ellipse(eye1x, eye1y, 5, 8);
ellipse(eye2x, eye2y, 5, 8);
}
else{
strokeWeight(3);
stroke(78,52,35);
beginShape(LINES);
vertex(eye1x - 3, eye1y + 3);
vertex(eye1x, eye1y);
vertex(eye1x, eye1y);
vertex(eye1x + 3, eye1y + 3);
endShape();
beginShape(LINES);
vertex(eye2x - 3, eye2y + 3);
vertex(eye2x, eye2y);
vertex(eye2x, eye2y);
vertex(eye2x + 3, eye2y + 3);
endShape();
}
pop();
pop();
}
display() {
this.drawBunny(this.cx + this.xdir + this.xmomentum, this.cy, this.r,);
}
}
class treat {
constructor(x, y) {
this.cx = x;
this.cy = y;
this.color = random(0, 255);
this.r = 15;
}
gravity(){
if (this.cy < 500) { this.cy += 10; }
}
display() {
ellipse(this.cx, this.cy, this.r, this.r);
}
}
class environment {
constructor(){
this.rayOffset1 = 0;
this.rayOffset2 = 0;
}
backgroundArt(){
noStroke();
image(wallpaperTexture, 0, 0);
image(floorTexture, 0, 450);
fill(130, 50, 0, 130);
rect(0, 0, 1000, 1000);
image(phonographImg, -180, -180);
fill(130, 50, 0, 50);
rect(0, 0, 1000, 1000);
push();
translate(600, 350);
rotate(PI/2.5);
image(flowerImg, 0, 0);
pop();
//fill(130, 50, 0, 180);
//rect(0, 0, 1000, 1000);
fill("brown");
}
sunBeam(){
//ugly for loops to make the beams look fuzzy
push();
let opacity = 20;
//fill(255, opacity);
//light coming in from window
let beamFrequency = 0.5;
let beamMove = (sin((millis()/200)*beamFrequency) )/5;
let beamAmount = beamMove * 20;
this.rayOffset1 = beamAmount;
let beamLayer = 5;
translate(700, -150);
rotate(PI/5);
for (let i = 0; i< beamLayer ; i ++){
fill(255, opacity - 20*i);
//ellipse(x, y, r + 5*i)
rect(0 + this.rayOffset1 + (15*i), 0, 400 + this.rayOffset1 + (15*i), 1200);
rect(-200 + this.rayOffset1 + (15*i), 0, 1300);
rect(200 - this.rayOffset1 + (15*i), 0, 300 + (15*i), 1400);
}
pop();
}
musicButton(){
let buttonMove = 0;
fill(140);
rect(140, 340, 40, 40, 10);
fill('brown');
push();
rect(147, 347, 30, 30, 40);
fill(0, 50);
rect(145, 345, 30, 30, 40);
pop();
rect(150 - buttonMove, 350 - buttonMove, 30, 30, 40);
if (mouseIsPressed &&( (mouseX < 175 && mouseX > 145)&&(mouseY < 375 && mouseY > 345))){
if (startSong == false) {
song.loop();
song.setVolume(0.8);
startSong = true;
excited = true;
} else {
song.pause();
startSong = false;
excited = false;
} }
}
display(){
this.backgroundArt();
this.sunBeam();
this.musicButton();
}
}