Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created June 3, 2023 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/e9e3e50692ab8737a2e5a38f46e70e78 to your computer and use it in GitHub Desktop.
Save KrabCode/e9e3e50692ab8737a2e5a38f46e70e78 to your computer and use it in GitHub Desktop.
import java.util.*;
PImage s;
ArrayList<P> ps = new ArrayList<P>();
ArrayList<M> ms = new ArrayList<M>();
ArrayList<M> mbin = new ArrayList<M>();
HashMap<Integer, PImage> imgs = new HashMap<Integer,PImage>();
void setup(){
fullScreen();
s = loadImage("s.png");
colorMode(HSB,1,1,1,1);
ps.add(new P(40,80,100,80));
ps.add(new P(190,80,150,110));
ps.add(new P(420,80,110,100));
ps.add(new P(140,190,50,50));
ps.add(new P(355,190,45,45));
ps.add(new P(220,255,130,100));
}
void draw(){
background(36/255f);
// debug();
updateMushrooms();
drawMushrooms();
}
void mouseDragged(){
PVector pos = randomPosNearMouse(30);
int dragSpawn = 4;
if(frameCount % dragSpawn == 0){
ms.add(new M(pos.x, pos.y));
}
}
void mousePressed(){
int pressCount = 10;
for(int i = 0; i < pressCount; i++){
PVector pos = randomPosNearMouse(100);
ms.add(new M(pos.x, pos.y));
}
}
PVector randomPosNearMouse(float radius){
PVector p = new PVector(mouseX, mouseY);
p.add(randomGaussian()*radius,
randomGaussian()*radius
);
return p;
}
void updateMushrooms(){
int ambientSpawn = 10;
if(frameCount % ambientSpawn == 0){
// if(ms.size() < ambientCount){
ms.add(new M());
// }
}
Collections.sort(ms, new Comparator<M>() {
public int compare(M o1, M o2) {
return new Float(o1.y).compareTo(o2.y);
}});
ms.removeAll(mbin);
mbin.clear();
}
void drawMushrooms(){
for(M m : ms){
m.draw();
}
}
void debug(){
image(s,0,0);
noFill();
stroke(255);
strokeWeight(3);
for(P p : ps){
rect(p.x,p.y,p.w,p.h);
}
}
class P{
int x,y,w,h;
P(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}
class M{
int i = floor(random(1)*ps.size());
float x = random(width);
float y = random(height);
float born = frameCount;
float grow = 10 + random(5);
float rest = 30+random(30);
float shrink = 20+random(10);
float hue = (0.55+random(0.65))%1;
float sat = abs(randomGaussian())*0.35;
float br = 0.75+random(0.25);
M(){
}
M(float x, float y){
this.x = x;
this.y = y;
grow = 6+random(2);
}
void draw(){
int fc = frameCount;
float scale = constrain(
norm(fc,born,born+grow),
0,1);
tint(hue,sat,br,scale);
if(fc >= born + grow + rest){
scale = 1-constrain(
norm(fc,born+grow+rest,born+grow+rest+shrink),
0,1);
tint(hue,sat,br,max(0.5,scale));
}
if(fc > born+grow + rest + shrink){
mbin.add(this);
}
push();
imageMode(CENTER);
translate(x,y);
scale(scale);
image(getImage(i), 0, 0);
pop();
}
}
PImage getImage(int i){
PImage img = imgs.get(i);
P p = ps.get(i);
if(img == null){
img = s.get(p.x,p.y,p.w,p.h);
imgs.put(i, img);
println(i);
}
return img;
}
void push(){
pushMatrix();
}
void pop(){
popMatrix();
}
@KrabCode
Copy link
Author

KrabCode commented Jun 3, 2023

s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment