using UnityEngine;
using System.Collections;

public class Battle {
	private Unit unit1;
	private Unit unit2;
	
	public Battle(Unit u1, Unit u2) {
		unit1 = u1;
		unit2 = u2;
	}
	
	public Unit getDead() {
		if (!unit1.isAlive()) return unit1;
		if (!unit2.isAlive()) return unit2;
		return null;
	}
	
	public bool firstUnitWillKill() {
		return willUnit1Attack() && unit1.willKillInBattle(unit2);
	}
	
	public bool willBattleDraw() {
		return !firstUnitWillKill() && !unit1.willDieInBattle(unit2);
	}
	
	public bool firstUnitWillDie() {
		return willUnit2Attack() && unit1.willDieInBattle(unit2);
	}
	
	private bool willUnit1Attack() {
		float dist = unit1.getPosition().distance(unit2.getPosition());
		return dist <= unit1.getReach() + 1.0e-5f;
	}
	
	private bool willUnit2Attack() {
		float dist = unit1.getPosition().distance(unit2.getPosition());
		return dist <= unit2.getReach() + 1.0e-5f;
	}
	
	public void processBattle() {
		if (firstUnitWillKill()) unit2.die();
		else if (firstUnitWillDie()) unit1.die();
	}
}
