//Master class for managing garden class Garden{ GrowTree [] tree; //L-System based dynamically growing tree Hopper [] hopper; //Chutes for breeding trees Vector genePool; //Storage queue for new trees Chromosome [] freshMeat; //Genetic code of parents for new tree Grass [] grass; //Background drawing of grass PImage backdrop; //Image to stamp successful parent trees on to int [] printTree; //Trees due to be printed onto "backdrop" float germinationRate = 0.4; //Probability of tree part growing seeds Garden(int trees){ hopper = new Hopper[2]; freshMeat = new Chromosome[trees * 2]; tree = new GrowTree[trees]; genePool = new Vector(); hopper[0] = new Hopper(width - 40, height / 2, 1); hopper[1] = new Hopper(40, height / 2, -1); for(int i = 0; i < freshMeat.length; i++){ freshMeat[i] = new Chromosome(); } for(int i = 0; i < tree.length; i++){ tree[i] = new GrowTree(i, constrain(random(-50, 50) + (width / (tree.length + 1)) * (i + 1), 0, width), height + 10, freshMeat[i *2], freshMeat[(i * 2) + 1]); } sound[growSound].stop(); sound[growSound].play(); freshMeat = new Chromosome[2]; backdrop = new PImage(width, height); printTree = new int[2]; printTree[0] = -1; printTree[1] = -1; Vector temp = new Vector(); for(float x = random(3); x < width; x += random(2, 4)){ temp.add(new Grass(x)); } grass = new Grass[temp.size()]; for(int i = 0; i < grass.length; i++){ grass[i] = (Grass)temp.remove(0); } background(0); for(int k = 0; k < grass.length; k++){ grass[k].draw(); } loadPixels(); backdrop.copy(g, 0, 0, width, height, 0, 0, width, height); } void draw(){ backdrop.updatePixels(); image(backdrop, 0, 0); if(printTree[0] != -1 && printTree[1] != -1){ for(int i = 0; i < printTree.length; i++){ tree[printTree[i]].draw(); printTree[i] = -1; noStroke(); fill(0, 0, 0, 10); rect(0, 0, width, height); for(int j = 0; j < grass.length; j++){ grass[j].draw(); } loadPixels(); backdrop.copy(g, 0, 0, width, height, 0, 0, width, height); } } for(int i = 0; i < tree.length; i++){ tree[i].draw(); if(tree[i].sucking || tree[i].fading < 30){ if(tree[i].treeSwallowed() && freshMeat[0] != null && freshMeat[1] != null){ tree[i] = new GrowTree(i, constrain(random(-50, 50) + (width / (tree.length + 1)) * (i + 1), 0, width), height + 10, freshMeat[0], freshMeat[1]); sound[growSound].stop(); sound[growSound].play(); freshMeat[0] = null; freshMeat[1] = null; } else if(tree[i].fading == 0 || tree[i].treeSwallowed()){ freshMeat[1] = new Chromosome(); freshMeat[0] = new Chromosome(); tree[i] = new GrowTree(i, constrain(random(-50, 50) + (width / (tree.length + 1)) * (i + 1), 0, width), height + 10, freshMeat[0], freshMeat[1]); sound[growSound].stop(); sound[growSound].play(); freshMeat[1] = null; freshMeat[0] = null; } } } for(int i = 0; i < hopper.length; i++){ hopper[i].draw(); } for(int i = 0; i < tree.length; i++){ tree[i].seed.draw(); } if(genePool.size() > 1){ Integer temp = (Integer)genePool.remove(0); int seedA = temp.intValue(); temp = (Integer)genePool.remove(0); int seedB = temp.intValue(); makeTree(seedA, seedB); } } //Print out the genetic code of the current trees void printTrees(){ for(int i = 0; i < tree.length; i++){ println(i+":"+tree[i].id.decode()); } } //Add genetic code of chosen trees to the queue for breeding void uploadChromosome(int num){ if(genePool.size() > 0){ Integer temp = (Integer)genePool.get(genePool.size() - 1); if(temp.intValue() != num){ genePool.add(new Integer(num)); } } else{ genePool.add(new Integer(num)); } tree[num].createSeed(); } //Print parents, destroy a random unchosen tree and get genetic code ready void makeTree(int seedA, int seedB){ freshMeat[0] = tree[seedA].id; freshMeat[1] = tree[seedB].id; printTree[0] = seedA; printTree[1] = seedB; while(true){ int i = (int)random(tree.length); if(i != seedA && i != seedB){ tree[i].swallowTree(); break; } } } }