int range; int maxbugs=100; boolean drawLines=false; int live=0; boolean doReset=false; boolean changeLines=false; boolean changePlay=false; int controls=0; PImage play; PImage pause; boolean playing=false; boolean spawning=false; boolean fsc=true; int px=0,py=0; void setup() { size(600,600 ); // surface.setResizable(true); play=loadImage("play.png"); pause=loadImage("pause.png"); //frameRate(5); //noSmooth(); bugs=new ArrayList(0); reset(); document.getElementById("canvas").getContext("2d").globalCompositeOperation = "multiply"; } void draw() { // println("starting frame"); document.getElementById("canvas").getContext("2d").globalCompositeOperation = "source-over"; background(240); document.getElementById("canvas").getContext("2d").globalCompositeOperation = "multiply"; if(playing==true){ if(bugs.size()0||fsc){ document.getElementById("canvas").getContext("2d").globalCompositeOperation = "source-over"; noStroke(); controls-=4; if(mouseY<50){ controls=100; fsc=false; } fill(0,controls); rect(0,0,600,50); if(mouseX<200&&mouseY<50){ fill(0,0,0,controls); rect(0,0,200,50); } else if(mouseX<400&&mouseY<50){ fill(0,0,0,controls); rect(200,0,200,50); } else if(mouseY<50){ fill(0,0,0,controls); rect(400,0,200,50); } tint(255,min(255,controls*5)); if(playing)image(pause,-50,0); else image(play,-50,0); fill(255); textSize(25); if(drawLines){ text("Hide Lines",235,31); } else{ text("Show Lines",235,31); } text("Reset",470,31); tint(255,255); } if(changePlay){ changePlay=false; playing=!playing; } if(doReset){ reset(); doReset=false; } if(changeLines){ changeLines=false; drawLines=!drawLines; } } void mousePressed() { if(mouseX<200&&mouseY<50){ changePlay=true; } else if(mouseX<400&&mouseY<50){ changeLines=true; } else if(mouseY<50){ doReset=true; } else{ spawning=true; newBug(mouseX, mouseY, true); } } void mouseReleased(){ spawning=false; } void mouseDragged(){ if(spawning){ newBug(mouseX, mouseY, true); } } void keyPressed(){ if(key=='r')bugs.clear(); } class bug { PVector loc; PVector newloc; boolean alive; int p; float acc; float rad=2; boolean chase; PVector v; PVector a; bug target; //color col=color(0,0,0); //int t; //int tmax=25; bug(int xi, int yi, int pi, boolean ai) { loc=new PVector(xi, yi); p=pi; alive=ai; acc=-1; if (!alive) { acc=0; p+=5; } chase=false; v=new PVector(0, 0); a=new PVector(0, 0); target=null; } void feed(int f) { p+=f; acc=-1; //col=color(0,255,0); //t=tmax; } void tick() { /* if(t>0){ if(t==1)col=color(0,0,0); t--; } */ if (alive&&random(0, 1)<.01) { p-=floor(p/30); acc=-1; // col=color(255,0,0); // t=tmax; } if (p<4) { alive=false; acc=0; rad=2; loc.x=floor(loc.x); loc.y=floor(loc.y); } if (alive==false) { return; } if (acc==-1) { rad=sqrt(p); acc=.02+2/(rad); //acc=.25; } newloc=new PVector(loc.x, loc.y); bug best=bugs.get(0); float bestD=0, worstD=0; bug worst=bugs.get(0); for (int i=0; ibestD) { bestD=d; best=bugs.get(i); } } //text("p: "+p+", best: "+floor(bestD*100)/100.0+" / "+best.p+", worst: "+floor(worstD*100)/100.0+" / "+worst.p,20,20); if (bestD>-1*worstD&&bestD!=0) { target=best; //go towards PVector bet=best.loc.get(); bet.sub(loc); a=bet.get(); a.normalize(); a.mult(1.2); PVector tv=v.get(); tv.normalize(); a.sub(tv); a.normalize(); a.mult(acc); chase=true; } else if (worstD!=0) { //go away target=worst; PVector bet=worst.loc.get(); bet.sub(loc); a=bet.get(); a.normalize(); a.mult(-1*acc); chase=false; } else { a=new PVector(0, 0); } } void done() { if (alive) { newloc=loc.get(); v.add(a); loc.add(v); v.mult(.95); } if (loc.x<0) { loc.x=5; v.x*=-1; } if (loc.y<0) { loc.y=5; v.y*=-1; } if (loc.x>width) { loc.x=width-5; v.x*=-1; } if (loc.y>height) { loc.y=height-5; v.y*=-1; } } void disp() { if (alive) { live++; //stroke(lerpColor(color(0,0,0),col,t*1.0/tmax)); stroke(0); strokeWeight(rad); line(loc.x, loc.y, newloc.x, newloc.y); // } // if(chase)fill(0,255,0); // else fill(255,0,0); //strokeWeight(2); //stroke(200); if(drawLines==true&&target!=null){ if(chase)stroke(0,255,0); else stroke(255,0,0); strokeWeight(2); line(loc.x, loc.y, target.loc.x,target.loc.y); } //line(loc.x,loc.y,loc.x+(rad/acc/2)*a.x,loc.y+(rad/acc/2)*a.y); } else { noStroke(); fill(0); rect(loc.x-1, loc.y-1, 2, 2); } fill(0); // text(p,loc.x-4,loc.y-rad/2-10); } float des(bug o) { if(this==o)return 0; if (abs(o.loc.x-loc.x)>range||abs(o.loc.y-loc.y)>range)return 0; if (eats(o,this)) { float d=max(1, sq(PVector.dist(loc, o.loc)-rad/2-o.rad/2)); if(d<40)return -1000.0*o.p/d; return -120/d; } return o.p/max(.1, sq(PVector.dist(loc, o.loc)-rad/2-o.rad/2)); } } void newBug(int x, int y, boolean live) { bugs.add(new bug(x, y, 15+floor(random(1, 10)), live)); } boolean within(int a, int b) { return PVector.dist(bugs.get(a).loc, bugs.get(b).loc)=b.p); } ArrayList bugs; void reset(){ bugs.clear(); /* for (int i=0; i