class Gnat extends Particle { float nutrition; float consumption_rate; //the amount of value consumed per second float metabolic_rate; //the amount of nutrition used per second float reproduction_rate; //the amount of nutrition needed to reproduce float rot_x_rate; float rot_y_rate; float rot_z_rate; float rot_x; float rot_y; float rot_z; Gnat(int max_dimension) { super(max_dimension); consumption_rate = .5; metabolic_rate = .3 * consumption_rate; nutrition = random(5, 20); reproduction_rate = random(100); rot_x_rate = random(10); rot_y_rate = random(10); rot_z_rate = random(10); } void setConsumptionRate(float consumption_rate) { this.consumption_rate = consumption_rate; } float getConsumptionRate() { return this.consumption_rate; } void setReproductionRate(float reproduction_rate) { this.reproduction_rate = reproduction_rate; } float getReproductionRate() { return this.reproduction_rate; } void update() { if (isDead()) { return; } float time_elapsed = (millis() - last_update_millis) / 1000; rot_x = (rot_x + (rot_x_rate * time_elapsed)) % 360; rot_y = (rot_y + (rot_y_rate * time_elapsed)) % 360; rot_z = (rot_z + (rot_z_rate * time_elapsed)) % 360; super.update(); Environment e = s.getEnvironment(); if (e instanceof Field) { Field f = (Field) e; Vector emitters = f.getEmitters(); Emitter em; float emv; for(int i = 0; i < emitters.size(); i++) { em = (Emitter) emitters.get(i); if (em.getPosition().getDistance(this.position) < em.getRadius() + this.getRadius()) { //emitter and gnat intersect emv = em.getEmissionValue(this.position); if (emv - (consumption_rate * time_elapsed) > 0) { emv -= consumption_rate * time_elapsed; nutrition += consumption_rate * time_elapsed; } else { emv = 0; nutrition += emv; } em.setEmissionValue(emv, this.position); } } } nutrition -= metabolic_rate * time_elapsed; } void setMetabolicRate(float metabolic_rate) { this.metabolic_rate = metabolic_rate; } float getMetabolicRate() { return this.metabolic_rate; } void setNutrition(float nutrition) { this.nutrition = nutrition; } float getNutrition() { return nutrition; } boolean isDead() { return (this.nutrition < 1); } public void display() { float x, y, z; x = position.getValue(0); y = position.getValue(1); z = position.getValue(2); fill(f); emissive(red(f),green(f),blue(f)); pushMatrix(); translate(x, y, z); rotateX(radians(rot_x)); rotateY(radians(rot_y)); rotateZ(radians(rot_z)); //sphere(nutrition); box(nutrition); //model.drawMode(POINTS); //model.enableTexture(); //scale(nutrition * .5); //model.draw(); popMatrix(); } boolean isFertile() { // println(this.nutrition + " " + this.reproduction_rate); return (this.nutrition >= this.reproduction_rate); } Gnat reproduce(Gnat g) { Gnat baby_g = new Gnat(max_dimension); baby_g.setUseTime(this.getUseTime()); baby_g.setHasMaxVelocityMagnitude(this.getHasMaxVelocityMagnitude()); float r1, r2; r1 = random(1); r2 = 1 - r1; baby_g.setConsumptionRate(r1 * this.getConsumptionRate() + r2 * g.getConsumptionRate()); r1 = random(1); r2 = 1 - r1; baby_g.setMetabolicRate(r1 * this.getMetabolicRate() + r2 * g.getMetabolicRate()); r1 = random(1); r2 = 1 - r1; baby_g.setReproductionRate(r1 * this.getReproductionRate() + r2 * g.getReproductionRate()); r1 = random(1); r2 = 1 - r1; baby_g.setInertiaFactor(r1 * this.getInertiaFactor() + r2 * g.getInertiaFactor()); r1 = random(1); r2 = 1 - r1; baby_g.setSelfishFactor(r1 * this.getSelfishFactor() + r2 * g.getSelfishFactor()); r1 = random(1); r2 = 1 - r1; baby_g.setSocialFactor(r1 * this.getSocialFactor() + r2 * g.getSocialFactor()); r1 = random(1); r2 = 1 - r1; baby_g.setDefianceFactor(r1 * this.getDefianceFactor() + r2 * g.getDefianceFactor()); r1 = random(1); r2 = 1 - r1; baby_g.setRandomSearchRadius(r1 * this.getRandomSearchRadius() + r2 * g.getRandomSearchRadius()); r1 = random(1); r2 = 1 - r1; baby_g.setMaxVelocityMagnitude(r1 * this.getMaxVelocityMagnitude() + r2 * g.getMaxVelocityMagnitude()); r1 = random(1); r2 = 1 - r1; baby_g.setFillColor( color(r1 * red(this.getFillColor()) + r2 * red(g.getFillColor()), r1 * green(this.getFillColor()) + r2 * green(g.getFillColor()), r1 * blue(this.getFillColor()) + r2 * blue(g.getFillColor()), r1 * alpha(this.getFillColor()) + r2 * alpha(g.getFillColor()))); //halfway between both parents baby_g.setPosition( (this.getPosition().add(g.getPosition())).multiply(.5) ); baby_g.setNutrition(this.getNutrition() / 2); this.setNutrition(this.getNutrition() / 2); return baby_g; } }