Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active August 3, 2022 14:20
Show Gist options
  • Save KrabCode/f9b1d6696f45e7b8c8d48cf1663ddb1c to your computer and use it in GitHub Desktop.
Save KrabCode/f9b1d6696f45e7b8c8d48cf1663ddb1c to your computer and use it in GitHub Desktop.
// TODO
// Extract key variables to the top
// Sinewave water level
// Drain blood somehow
ArrayList<P> ps = new ArrayList<P>();
ArrayList<L> ls = new ArrayList<L>();
float levelY;
void setup(){
fullScreen(P2D);
colorMode(HSB,1,1,1,1);
levelY = height;
}
void draw(){
background(0);
if(mousePressed){
spawn();
}
updateLines();
updateParticles();
drawLevel();
}
void drawLevel(){
noStroke();
fill(1,1,1,0.5);
rect(0, levelY, width, height-levelY);
}
void spawn(){
float d = dist(mouseX, mouseY, pmouseX, pmouseY);
if(floor(d) < 1){
ps.add(new P(mouseX, mouseY));
return;
}
for(float i = 0; i <= d; i+= 2){
float lerp = norm(i, 0, d);
float x = lerp(pmouseX, mouseX, lerp);
float y = lerp(pmouseY, mouseY, lerp);
ps.add(new P(x,y));
}
}
void updateLines(){
if(mousePressed && !ls.isEmpty()){
ls.get(ls.size()-1).vs.add(new V(mouseX, mouseY));
}
ArrayList<L> lsBin = new ArrayList<L>();
for(L line : ls){
line.update();
if(line.isEmpty()){
lsBin.add(line);
}
}
ls.removeAll(lsBin);
}
void mousePressed(){
ls.add(new L(mouseX, mouseY));
}
void updateParticles(){
ArrayList<P> psBin = new ArrayList<P>();
for(P p : ps){
p.update();
if(p.toBin()){
psBin.add(p);
levelY -= 2*PI*p.r / (float) width;
}
}
ps.removeAll(psBin);
}
class P{
PVector pos;
PVector spd;
float r = 2 + abs(randomGaussian()) * 4;
int born = frameCount;
int fadeIn = 5;
int shape = floor(random(1.5));
P(float x, float y){
PVector offset = new PVector(
randomGaussian(),
randomGaussian()).mult(3);
pos = new PVector(x, y).add(offset);
spd = PVector.sub(pos, new PVector(x, y));
}
void update(){
PVector acc = new PVector(0, 0.5);
spd.add(acc);
spd.mult(.99);
pos.add(spd);
float fadeInNorm = constrain(norm(frameCount, born, born+fadeIn),0,1);
pushMatrix();
translate(pos.x, pos.y);
rotate(spd.heading());
if(shape == 0){
strokeWeight(r*2);
stroke(1,1,1,fadeInNorm);
noFill();
point(0, 0);
}
if(shape == 1){
noStroke();
fill(1,1,1,fadeInNorm);
rect(0, 0, r*2 ,r*2);
}
popMatrix();
}
boolean toBin(){
return pos.y + r > levelY;
}
}
class L{
ArrayList<V> vs = new ArrayList<V>();
L(float x, float y){
vs.add(new V(x,y));
}
void update(){
ArrayList<V> vsBin = new ArrayList<V>();
noFill();
strokeWeight(10);
strokeCap(ROUND);
stroke(1,0,1);
beginShape();
for(V v : vs){
vertex(v.pos.x, v.pos.y);
if(v.toBin()){
vsBin.add(v);
}
}
endShape();
vs.removeAll(vsBin);
}
boolean isEmpty(){
return vs.isEmpty();
}
}
class V{
PVector pos;
int born = frameCount;
int lifetime = 60;
V(float x, float y){
pos = new PVector(x,y);
}
boolean toBin(){
return lifeNorm() >= 1;
}
float lifeNorm(){
return norm(frameCount, born, born+lifetime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment