//Tasha Wallage
//Period 7

// This class provides a food source for the Organism class though it is not
// able to mutate and evolve.
    public class Food implements Species
   {
      private int energy;
      private World wd;
      private Location myLoc;
      private double age, deathRate;
   	   
       public Food(World wd, int range)
      {
         this.wd = wd;
         int x = (int)(Math.random()*range);
         int y = (int)(Math.random()*range);
         myLoc = new Location(x, y);
         energy = 100;
	 age = 0;
	 deathRate = 0.01;
      }
       public Food(World wd, int x, int y, int energy)
      {
         myLoc = new Location(x, y);
         this.energy = energy;
         this.wd = wd;
	 age = 0;
	 deathRate = 0.01;
      }
      //main method that determines what a Food will do next
      //may randomly breed if there is available space
      //may die with a chance of 1%
       public void act()
      {
      	 age+=(1.0/365.25);
	 if(age >= 1)
		deathRate = 0.5; 
	 else if(wd.getVacantSpot(myLoc)!=null && (Math.random() < (.02)))
            wd.add(breed());
         die();
      }
       public Location getLoc()
      {
         return myLoc;
      }
      //1% chance that the Food dies
       public void die()
      {
         if(Math.random()< deathRate)
            wd.remove(this);
      }	
      //creates a new Food item identical to it (asexual)
       public Food breed()
      {
         int eng = 0;
         if((int)Math.random()*2%2==0)
            if((int)Math.random()*2%2==0)
               eng += (int)(Math.random()*5);
            else
               eng -= (int)(Math.random()*5);
         else
            eng = energy;
         Location avLoc = wd.getVacantSpot(getLoc());
      
         return new Food(wd, avLoc.getX(), avLoc.getY(), eng);
      }
      //returns the amount of energy gained if this Food is eaten
       public int getEnergy()
      {
         return energy;
      }
      //returns class name
       public String getName()
      {
         return "Food";
      }
      public int getSpeed()
      {
      	return 1;
      }
       public String toString()
      {
         return "F["+myLoc.getX()+", "+myLoc.getY()+"] E"+energy;
      }
   }