package de.rico.adventure.entity;

import java.util.Comparator;

import de.rico.engine.camera.Camera;
import de.rico.engine.geometry.Point3D;

/**
 * This is a comparator class that can be used to compare
 * the distances to the camera of two entities. Because this comparator
 * uses only the center points of the entities and not
 * their expansion into the world to perform the
 * sorting, the result are not garanteed to be correct in any case.
 * This is just some kind of rough sorting.
 * @author Frank Bruns
 *
 */
public class EntityDistanceToCameraComparator implements Comparator<AbstractEntity>
{

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(AbstractEntity ent1, AbstractEntity ent2)
	{

		// calculates distance to the camera
		float ent1DistSq = Point3D.getDistanceSquared(
				Camera.getInstance().getPosition(),
				ent1.getMesh().getPosition());

		float ent2DistSq = Point3D.getDistanceSquared(
				Camera.getInstance().getPosition(),
				ent2.getMesh().getPosition());

		if(ent1DistSq > ent2DistSq) return 1;
		if(ent1DistSq == ent2DistSq) return 0;
		if(ent1DistSq < ent2DistSq) return -1;
		return 0;
	}

}
