class system { organism[] bug= new organism[15]; int numbug; food[] f= new food[25]; int numfood; int splitto; int splitmax; system(int numbug_, int numfood_) { numbug=numbug_; numfood=numfood_; initialize(); splitto=5; splitmax=14-1; } //************************************************************************ //**UTILITY FUNCTIONS int getNUMBUG() {return numbug;} void setNUMBUG(int numbug_) {numbug = numbug_; checknumbug();} int getNUMFOOD() {return numfood;} void setNUMFOOD(int numfood_) {numfood = numfood_; checknumfood();} void checknumbug() { if (numbug>bug.length) {numbug=bug.length;} if (numbug<0) {numbug=0;} }//end function checknumbug void checknumfood() { if (numfood>f.length) {numfood=f.length;} if (numfood<0) {numfood=0;} }//end function checknumfood void initialize() { float Lx, Ly; //all are initialized even if not used for(int i=0; i< bug.length ; i=i+1) { Lx=random(10,width-10); Ly=random(10,height-10); bug[i] = new organism(5,Lx,Ly,1,1); } for(int i=0; i< f.length ; i=i+1) { Lx=random(10,width-10); Ly=random(10,height-10); f[i] = new food(Lx,Ly,10,color(random(50,100),random(50,200),random(50,255),150)); } } void render(float beat_) { checknumbug(); checknumfood(); for(int i=0; i< numbug ; i=i+1) { bug[i].render(beat_); } for(int i=0; i< numfood ; i=i+1) { f[i].render(); } }//end of function render //** END UTILITY FUNCTIONS //************************************************************************ void simulate(float timer_) { color hold; int fhunt; fhunt=0; checknumbug();checknumfood(); for(int i=0; i< numbug ; i=i+1) { //Look to see which piece of food is closest to head fhunt=findclosest(i); //if the bug is next to the food the bug eats it if(bug[i].near(f[fhunt].getX(), f[fhunt].getY(), 10.0)) { hold=color(red(f[fhunt].getC()),green(f[fhunt].getC()),blue(f[fhunt].getC()),alpha(f[fhunt].getC())); f[fhunt].eaten(); bug[i].setNUMSEG(bug[i].getNUMSEG()+1); bug[i].setSEGC(bug[i].getNUMSEG()-1, color(red(hold),green(hold),blue(hold),alpha(hold))); //fhunt=findclosest(i); bug[i].seek(f[fhunt].getX(),f[fhunt].getY()); timer=0; } //if the bug is not next to the food the bug moves toward it else { bug[i].seek(f[fhunt].getX(),f[fhunt].getY());} //the bug loses a segment if past been timemax since eaten if(timer_==timemax) { bug[i].setNUMSEG(bug[i].getNUMSEG()-1); } //the bug splits if now at max size if(bug[i].getNUMSEG()==splitmax) { splitbug(i); } }// end of for loop through the bugs }//end of simulate function int findclosest(int bi) { int closest; float distfood, minimum; closest=0; distfood=width*10; minimum=width*10;//this to make sure big to begin with for(int i=0; isplitmax-1-splitto; i=i-1) { hold=color(red(bug[f].getNUMC(i)),green(bug[f].getNUMC(i)),blue(bug[f].getNUMC(i)),alpha(bug[f].getNUMC(i))); bug[t].setSEGC(count, color(red(hold),green(hold),blue(hold),alpha(hold))); count=count+1; } } }//end of population class