
// Entity.java


/******************************************************************************
/ Entity.java modellerar en spelpjäs.
/
/*****************************************************************************/

class Entity
{
	
	public static final int    MAX_HEALTH = 5;
	public static final int    MIN_HEALT = 0;
	
	// -----
	
	private int      row;
	private int      column;
	private String    type;
	private int       health;
	
	// -----
	
	public Entity ()
	{
		row = 1;
		column = 1;
		type = "?";
		health = MAX_HEALTH;
	}
	
	public Entity (int row, int column, String type, int health)
	{
		this.row = row;
		this.column = column;
		this.type = type;
		this.health = health;
	}
	
	public Entity (Entity e)
	{
		this.row = e.row;
		this.column = e.column;
		this.type = e.type;
		this.health = e.health;
	}
	
	// -----
	
	public int getRow ()
	{
		return this.row;
	}
	
	public int getColumn ()
	{
		return this.column;
	}
	
	public String getType ()
	{
		return this.type;
	}
	
	public int getHealth ()
	{
		return this.health;
	}
	
	public void setRow (int row)
	{
		this.row = row;
	}
	
	public void setColumn (int column)
	{
		this.column = column;
	}
	
	public void setType (String typ)
	{
		this.type = type;
	}
	
	public String toString ()
	{
		return "Type   : " + type + "\r\n" + 
		       "Row    : " + row + "\r\n" +
			   "Column : " + column + "\r\n" +
			   "Health : " + health;
	}
	
	// -----
	
	/**************************************************************************
	/ Calculate the distance from this entity to another entity e
	/*************************************************************************/
	public double distanceToEntity (Entity e)
	{
		return Math.sqrt ((e.row - this.row) * (e.row - this.row) + 
	                     (e.column - this.column) * (e.column - this.column));
	}
	
	/**************************************************************************
	/ Take a hit from another entity e.
	/
	/ If both this entity's health and entitiy e's health > 0 and both entities
	/ are in the same position then subtract 1 from BOTH entities's health.
	/ If entity e's health > this entity's health > 0 then subtract 1 from this
	/ entity's health.
	/ If 0 < entity e's health <= this entity's health then subtract 1 from
	/ entity e's health.
	/*************************************************************************/
	public void takeHit (Entity e)
	{
		if ((this.row == e.row) && (this.column == e.column))
		{
			if ((this.health > 0) && (e.health > 0))
			{
				this.health--;
				e.health--;
			}
		}
		else
		{
			if (e.health > this.health)
			{
				if (this.health > 0)
					this.health--;
			}
			else
			{
				if (e.health > 0)
					e.health--;
			}
		}
	}
}