using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour
{
	[SerializeField]protected int damage = 10;
	[SerializeField]protected float hitChance = 1f; //in percents
	[SerializeField]protected float fullReloadTime = 1f; //in seconds

	protected float reloadTimer = 0;
	protected StarShip owner;
	protected StarShip enemy;
	
	public bool IsReady {
		get { return reloadTimer <= 0;}
	}
	
	public float ReloadTime {
		get { return reloadTimer / fullReloadTime;}
	}
	
	public virtual void Fire ()
	{
		if (reloadTimer > 0)
		{
			return;
		}
		reloadTimer = fullReloadTime;
		if (enemy != null)
		{
			enemy.OnHit (damage);
		}
	}
	
	public virtual void Init (StarShip owner, StarShip enemy)
	{
		this.owner = owner;
		this.enemy = enemy;
	}

	protected void UpdateReload()
	{
		if (reloadTimer > 0)
		{
			reloadTimer -= Time.deltaTime;
			if (reloadTimer <= 0)
			{
				reloadTimer = 0;
			}
		}
	}

	void Update ()
	{
		UpdateReload ();
	}
}
