﻿using UnityEngine;
using System.Collections;

public class LightBarrier : MonoBehaviour {

    public Transform ownerTran;
    
    int callMe10Times = 0;      // this is hard-coded in case the player doesn't move

    void Start() {
        StartCoroutine(Z_ActivateCollider());
    }

    IEnumerator Z_ActivateCollider() {
        while (true) {
		    yield return new WaitForSeconds(1);
            if (!ownerTran && callMe10Times == 10)  // this will generate a bug, not activating the barrier
                break;                              // it was needed, but wrong implemented
            else {
                callMe10Times++;
                if (ownerTran && (ownerTran.position - transform.position).magnitude > 3f) {
                    GetComponent<BoxCollider>().enabled = true;
                    break;
                }
            }
        }
    }


	void OnCollisionEnter(Collision collision) {
        GameObject other = collision.collider.gameObject;
        if (other.tag == "Player") {        // destroy the player that collides with
            other.GetComponent<Player>().Z_Explode();
        }
    }

}
