package lilWorld; import java.util.ArrayList; import java.util.List; import processing.core.PApplet; import processing.core.PImage; import processing.core.PVector; public class Creeper extends Creature{ float wiggleFactor; int crPlus=10; float maxspeed = 5; static List graphics; int graphicsNum; float maxforce = 10; float timeToEat= 1000; public Creeper(PVector l, PVector a, PVector v, int m_, PApplet p) { super(l, a, v, m_, p); wiggleFactor = parent.random(5); //grav = new PVector(0,0.5f); graphicsNum = (int) parent.random(3); lifespan = new Timer((int) parent.random(20000, 85000)); lifespan.start(); } public void setCreeperSketch() { graphics = new ArrayList(); PImage temp = parent.loadImage("creeper.png"); PImage temp2 = parent.loadImage("creeper2.png"); PImage temp3= parent.loadImage("creeper3.png"); graphics.add(temp); graphics.add(temp2); graphics.add(temp3); } public void move() { grav = new PVector(0.0f, Vars.creeperGrav); applyForce(grav); // applyForce(. .. ); // applyForce(. . .); // if((loc.x <= 5) || (loc.x >= parent.width-10)) //manage edges // { // // //vel.x = -vel.x; //pure bounce // PVector edgeForce = new PVector(-vel.x/3,parent.random(0,3)+1); // this.vel.mult(0); // applyForce(edgeForce); // } // if ((loc.y <= 5) || (loc.y >= parent.height-10))//manage edges // { // //vel.y = -vel.y; //pure bounce // PVector edgeForce = new PVector(parent.random(-2,2),-vel.y); // this.vel.mult(0); // applyForce(edgeForce); // } if((loc.x <= 10) || (loc.x >= parent.width-10)) { vel.x = -vel.x/2; //pure bounce } if ((loc.y <= 10) || (loc.y >= parent.height-10)) { vel.y = -vel.y/2; //pure bounce } //here you're adding all vel.add(acc); vel.limit(Vars.creeperSpeed); loc.add(vel); acc.mult(0); PVector wiggle = new PVector(parent.random(-1,1),parent.random(-1,1)); //make wiggly wiggle.mult(wiggleFactor); loc.add(wiggle); // hacky, but it works; loc.x = PApplet.constrain(loc.x,0,parent.width-3); loc.y = PApplet.constrain(loc.y,0,parent.height-3); } public boolean isHungry() { if(lifespan.getTime() > timeToEat) { return true; } else { return false; } } void huntForFood(PVector target) { acc.add(steer(target,false)); //System.out.print("coming to eat!"); } public PVector steer(PVector target, boolean slowdown) { PVector steer; // The steering vector PVector desired = PVector.sub(target,loc); // A vector pointing from the location to the target float d = desired.mag(); // Distance from the target is the magnitude of the vector // If the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { // Normalize desired desired.normalize(); // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed) if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed*(d/100.0f)); // This damping is somewhat arbitrary else desired.mult(maxspeed); // Steering = Desired minus Velocity // PVector swerve = new PVector(parent.random(-.05f,.05f),parent.random(-.05f, .05f)); ///vel.add(swerve); steer = PVector.sub(desired,vel); steer.limit(maxforce); // Limit to maximum steering force } else { steer = new PVector(0,0);//shoot back down. } return steer; } public void show() { parent.pushMatrix(); parent.translate(loc.x,loc.y); parent.rotate(vel.heading2D()); parent.image((PImage)graphics.get(graphicsNum), 0,0, size, size); //parent.fill(0,0,255,255); //parent.ellipse((float) 0+size*0.5f, (float)0+size*0.5f, size, size); parent.fill(255,255,255,255); parent.popMatrix(); } public void grow() { size+=10; mass = size/2; } }