using UnityEngine;
using System.Collections;

public class LightSource : MonoBehaviour {

    [HideInInspector]
    public bool isLighted = false;
    public bool isPowerup = true;
    public float safeRadius = 1.7f;

    private ParticleSystem system;
    private Light light;

    void Start()
    {
        system = transform.GetComponentInChildren<ParticleSystem>();
        light = transform.GetComponentInChildren<Light>();
    }

    void Update()
    {
        if(system)
            system.enableEmission = isLighted;

        if (light)
            light.enabled = isLighted;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player" && !isLighted)
        {
            isLighted = true;
            if(isPowerup)
                other.SendMessage("UpdateSafePoint", transform.position);
        }
    }

    void TurnOff()
    {
        isLighted = false;
    }

    void OnDrawGizmosSelected()
    {
        Vector3 gizmoPos = transform.position;
        gizmoPos.y = 0;
        Gizmos.DrawWireSphere(gizmoPos, safeRadius);
    }
}
