/*
	Glutton .... a Thing that eats and eats
*/

import java.awt.*;

public class Glutton extends Thing {
	static Glutton [] theGluttons = new Glutton [100000];	// array holding all gluttons
	static int gluttonCt = 0;			//
	double minRadius = Sim2D.gluttonMinSize;				// minimum size for the glutton
	int myGluttonNumber;					// location of this glutton in the the gluttons array
	double radius = minRadius;			// circular size of the glutton
	int eatenThisStep = 0;				// a counter of Things eaten this time step
	double shrinkProb = Sim2D.gluttonNoFoodShrinkProb;			// probability of shrinking
	double amountToShrink = Sim2D.gluttonNoFoodShrinkAmount;		// how much to shrink if shrinking event occurs
	double maxRandomStepSize = Sim2D.gluttonMaxStepSize;		// maximum to change position each time step
	Color gluttonColor = Color.orange;	// color for drawing
	static double averageGluttonRadius = Sim2D.gluttonMinSize;
	static double muttonVolConsumed;
	static double averageMuttonVolConsumed;



	public Glutton (double initX, double initY) {
		super(initX,initY);
		addGlutton(this);
		
	}
	
	public void step () {
		// shrinking if no food found
		if (eatenThisStep == 0) {						// if any food found
			if (Math.random() < shrinkProb) { 			// if no food than shrink stochastically
				radius += -amountToShrink;
			}
			if (radius < minRadius) { radius = minRadius; } // don't shrink beyond minimum
		}
		
		
		// moving in space
		double xInc = (2*Math.random()-1)*maxRandomStepSize;	// choose random x-direction step size and direction
		double yInc = (2*Math.random()-1)*maxRandomStepSize;	// choose random y-direction step size and direction
		
		coord[0] += xInc;								// increment x-position
		coord[1] += yInc;								// increment y-position
		
		// crude boundary constraint
		if (((coord[0]+radius) > maxX) | ((coord[0]-radius) < 0)) {
			coord[0] += -2*xInc;	// go other way if past boundary
		}
		if (((coord[1]+radius) > maxY) | ((coord[1]-radius) < 0)) {
			coord[1] += -2*yInc;	// go other way if past boundary
		}
		
		// reset counters
		eatenThisStep = 0;
		
	}
	
	
	public void drawYourself (Graphics g, double scale, double [] offset) {
		g.setColor(gluttonColor);
		
		int xCenter = (int) ((coord[0]-offset[0])*scale);
		int yCenter = (int) ((coord[1]-offset[1])*scale);
		int pixelRadius = (int) (radius*scale);
		int pixelDiameter = (int) (2*radius*scale);
		
		g.drawOval(xCenter-pixelRadius,yCenter-pixelRadius,pixelDiameter,pixelDiameter);
	}
	
	public void ateAMutton (Mutton eatenM) {
		// add volume of mutton to this glutton and change radius accordingly
		double muttonVol = Math.PI*eatenM.radius*eatenM.radius;
		muttonVolConsumed = muttonVol;
		double newVol = muttonVol + Math.PI*radius*radius;
		radius = Math.sqrt(newVol/Math.PI);
		eatenM.removeMe = true;		// set flag so this Mutton will be removed
		eatenThisStep++;
	}
	
	public static void makeRandomGlutton () {
		double initX = Math.random()*maxX;
		double initY = Math.random()*maxY;
		new Glutton (initX, initY);
	}
	
	public static void addGlutton (Glutton newGlutton) {
		newGlutton.myGluttonNumber = gluttonCt;
		theGluttons[gluttonCt] = newGlutton;
		gluttonCt ++;
	}

    public static void calcAvgGluttonRadius(){
		double total = 0.0;
		for (int i = 0; i < gluttonCt; i++) {
			total = total + theGluttons[i].radius;
		}
       averageGluttonRadius = total/(double)(gluttonCt);
	}

	public static void calcAvgMuttonVolConsumed(){
		double total = 0.0;
		for (int i = 0; i < gluttonCt; i++) {
			total = total + theGluttons[i].muttonVolConsumed;
		}
		averageMuttonVolConsumed = total/(double)(gluttonCt);
	}
}
