/*
	Mutton ... i get eaten
*/

import java.awt.*;

public class Mutton extends Thing {
	static Mutton [] theMuttons = new Mutton [100000];	// array holding all muttons
	static int muttonCt = 0;		
	static double muttonSpawnProb = Sim2D.muttonSpawnProb; 	// probability of spawning muttons
	int myMuttonNumber;					// location of this mutton in the the muttons array
//	double maxRadius = 3.0;					// max circular size of all muttons
	double maxRadius = Sim2D.muttonMaxSize;					// max circular size of all muttons
	double radius;							// actual circular size
	double maxRandomStepSize = Sim2D.muttonMaxStepSize;			// maximum to change position each time-step
	Color muttonColor = Color.white;			// color for drawing
	static int maxMuttonCt = 0;
	static int minMuttonCt = 0;


	public Mutton (double initX, double initY) {
		super (initX,initY);
		radius = Math.random()*maxRadius;	// set a random radius for this mutton
		addMutton(this);
	}
	
	public void step () {
		// check for collision with Gluttons
		for (int i=0;i<Glutton.gluttonCt;i++) {
			Glutton curGlutton = Glutton.theGluttons[i];
			// calculate distance between center of glutton and mutton
			double xDistSqrd = Math.pow(curGlutton.coord[0]-this.coord[0],2);
			double yDistSqrd = Math.pow(curGlutton.coord[1]-this.coord[1],2);
			double cenDist = Math.sqrt(xDistSqrd + yDistSqrd);
			// if distance less than sum of radii then a collision has occurred
			if (cenDist < (curGlutton.radius + this.radius)) {
				curGlutton.ateAMutton(this);
			}
		}
			
		
		// 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
		}
		
	}
	
	public void drawYourself (Graphics g, double scale, double [] offset) {
		g.setColor(muttonColor);
		
		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 static void spawnMuttons () {
		if (Math.random() < muttonSpawnProb) { makeRandomMutton(); }
	}
	
	public static void makeRandomMutton () {
		double initX = Math.random()*maxX;
		double initY = Math.random()*maxY;
		new Mutton (initX, initY);
	}
	
	public static void addMutton (Mutton newMutton) {
		newMutton.myMuttonNumber = muttonCt;
		theMuttons[muttonCt] = newMutton;
		muttonCt ++;
	}
	
	public void removeMe () {
		super.removeMe();
		int swapId = myMuttonNumber;
		theMuttons[swapId] = theMuttons[muttonCt-1];
		theMuttons[swapId].myMuttonNumber = swapId;
		muttonCt --;
	}

	public static void calcMaxAndMinMuttonPopulation(){

		if (muttonCt > maxMuttonCt){
		   maxMuttonCt = muttonCt;
		}

		if (muttonCt < minMuttonCt){
			maxMuttonCt = muttonCt;
		}

	}
	
}
		
