using UnityEngine;
using System.Collections;

public class Burnable : GenericObject {

	[HideInInspector] public bool isBurning;
	public float lifeTime;
	public float burnThickTime; //soglia in cui prende fuoco

	private float burningtime; //tempo in cui sta bruciando
	private float hotTime; //tempo trascorso vicino ad un fuoco

	// Use this for initialization
	void Start () {
		isBurning = false;
	}
	
	// Update is called once per frame
	void Update () {
		if (hotTime >= burnThickTime) {
			isBurning = true;
			if(gameObject.name!="Tree")
			gameObject.renderer.material.color = Color.red;
			else 
			{
				transform.Find("Graphics").renderer.material.color = Color.red;

				var g = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/Flame"), transform.position, 
				                                  Quaternion.identity) as Transform; 
			}
			transform.Find("BurningArea").gameObject.SetActive(true);
				}
		if (isBurning) 
		{
			burningtime += Time.deltaTime;
			if(burningtime >= lifeTime && gameObject.GetComponent("Trail") == null)
				Network.Destroy(gameObject);
		}
	}

	public void Brucia()
	{
		if (isBurning)	return;
		hotTime += Time.deltaTime;
		if(gameObject.name!="Tree")
		gameObject.renderer.material.color = Color.yellow;
		else transform.Find("Graphics").renderer.material.color = Color.yellow;
	}
	
}
