// tjhCreature.java import java.util.*; import java.awt.*; import tjh2dVector; import tjhGene; import tjhCell; import tjhBuckets; /** A creature in our psuedo-biological simulation consists of a tree structure of tjhCell's. Also, the creature has a separate genome representation that is copied when it reproduces. Otherwise, evolution would follow the Lamarkian model which ain't no good. */ public class tjhCreature extends Object { //----------------------------------------------------- // static constants //----------------------------------------------------- /** maximum speed of the creature */ protected final static float SPEED=1.0F; //----------------------------------------------------- // data //----------------------------------------------------- /** the applet class CellLife uses this to check if this creature died since it last looked */ public boolean has_died; /** the current momentum (direction and speed) of the creature */ protected tjh2dVector momentum; /** the tree of tjhCell's that form the creature's body */ protected tjhCell root_cell; /** the tree of tjhGene's that form the creature's genome */ protected tjhGene genome; /** set to true if a pink bounce just occurred */ protected boolean pink_bounce; //----------------------------------------------------- // public //----------------------------------------------------- /** main function that is called for the creature each time step in the simulation. The creature moves itself and also waves its arms about either randomly or under their own control, depending on whether the neurons are activated. Returns true if it bounced off another creature's pink bit (casual sex!) */ public boolean DoTimeStep(Graphics g,tjhBuckets buckets,tjh2dVector limit) { pink_bounce=false; // until maybe proved wrong in tjhCell.CanMoveTo() // try to move but change direction if can't (should eventually be controlled by the creature) if(!root_cell.MoveIfCan(momentum,limit,buckets)) { momentum = tjh2dVector.mult(tjh2dVector.Cartesian((float)Math.random()*360.0F),SPEED); } root_cell.MoveArms(g,buckets,limit); // recurses down to all the cells return pink_bounce; } /** this is a new creature, instantiate it from the genes of another (parent) creature */ public void InheritFrom(tjhCreature other,tjhBuckets buckets,tjh2dVector limit) { // copy the genome (possibly with mutations) then instantiate from it genome.Copy(other.genome); root_cell.Instantiate(genome,this,buckets,limit); } /** initialize a simple creature from scratch */ public boolean InitSimple(tjh2dVector limit,tjhBuckets buckets) { // here we initialize the genome then instantiate from it genome.InitSimple(); root_cell.Instantiate(genome,this,buckets,limit); return true; } /** draw the creature */ public void Draw(Graphics g) { root_cell.Draw(g); } /** default constructor */ public tjhCreature() { root_cell = new tjhCell(); genome = new tjhGene(); has_died = false; momentum = tjh2dVector.mult(tjh2dVector.Cartesian((float)Math.random()*360.0F),SPEED); } }