xxxxxxxxxx
/*
Emojis galore!
â
Author:
Jason Labbe
â
Site:
jasonlabbe3d.com
*/
â
var emojis = ["đ", "đ˛", "âšī¸", "đ ", "đŖ", "đ", "đ", "đ¤"];
var balls = [];
var grounds;
var emojiSize = 50;
var emojiRad = emojiSize / 2.0;
var squashTimeLength = 20;
â
â
function preload() {
soundFormats("wav", "mp3");
fallingSound = loadSound("falling.wav");
smackSound = loadSound("smack.wav");
gruntSound = loadSound("grunt.wav");
}
â
â
function setup() {
createCanvas(windowWidth, windowHeight);
textSize(emojiSize);
mouseX = width;
grounds = [
[700, height + 500],
[500, height / 2 + 200],
[300, height / 2 + 100],
[0, height / 2]
]
}
â
â
function draw() {
background(0);
stroke(255);
strokeWeight(3);
for (let i = 0; i < grounds.length; i++) {
if (i > 0) {
line(grounds[i][0], grounds[i][1], grounds[i - 1][0], grounds[i][1]);
}
}
noStroke();
fill(255, 255, 0);
if (frameCount % 15 == 0) {
balls.push(new Ball(0, 0));
}
for (let i = balls.length - 1; i > -1; i--) {
balls[i].sim();
push();
translate(balls[i].pos.x, balls[i].pos.y);
// Fake squash & stretch!
if (balls[i].squashTimer > -1) {
if (balls[i].squashTimer > squashTimeLength - 5) {
translate(0, emojiRad * 0.5);
scale(
map(balls[i].squashTimer, squashTimeLength, squashTimeLength - 5, 1.25, 0.5),
map(balls[i].squashTimer, squashTimeLength, squashTimeLength - 5, 0.5, 1.8)
);
} else {
scale(
map(balls[i].squashTimer, squashTimeLength, 0, 0.5, 1),
map(balls[i].squashTimer, squashTimeLength, 0, 1.8, 1)
);
}
balls[i].squashTimer--;
}
rotate(radians(2 * balls[i].pos.x + balls[i].angle));
text(emojis[balls[i].hits], -30, 15); // Need to hard-code values here to get the pivot just right.. :(
pop();
if (balls[i].pos.y > height || balls[i].kill) {
/*if (balls[i].pos.y > height && !fallingSound.isPlaying() && random() > 0.5) {
fallingSound.rate(random(1, 2));
fallingSound.play();
}*/
balls.splice(i, 1);
}
}
}
â
â
function Ball(x, y) {
this.pos = new p5.Vector(x, y);
this.vel = new p5.Vector(random(2.5, 15), 2);
this.acc = new p5.Vector();
this.angle = random(360);
this.hits = 0;
this.squashTimer = -1;
this.kill = false;
this.sim = function() {
this.acc.add(new p5.Vector(0, 0.7));
let mousePos = new p5.Vector(mouseX, mouseY);
let d = mousePos.dist(this.pos);
if (d < 150) {
let force = this.pos.copy();
force.sub(mousePos);
force.mult(0.02);
this.acc.add(force);
}
this.vel.mult(0.99);
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
for (let i = 0; i < grounds.length; i++) {
let x = grounds[i][0];
let y = grounds[i][1];
if (this.pos.x > x) {
if (this.pos.y > y - emojiRad) {
this.pos.y = y - emojiRad;
this.vel.y *= -1;
this.squashTimer = squashTimeLength;
if (this.hits < emojis.length - 1) {
this.hits++;
/*if (random() > 0.75) { // Only play it sometimes, otherwise.. jesus!
if (random() > 0.5) {
smackSound.rate(random(0.75, 2));
smackSound.play();
} else {
gruntSound.rate(random(1.5, 2));
gruntSound.play();
}
}*/
} else {
this.kill = true;
}
}
break;
}
}
}
}