using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircuitCube : MonoBehaviour {

	public int state;
	public Material onMaterial;
	public Material offMaterial;
	public GameObject cube;

    private bool isPlayer = false;

    private string otherTag = "Player";

	void Awake () {
		if (state == 1 || state == 2) {
			cube.GetComponent<Renderer> ().material = onMaterial;
		} else {
			cube.GetComponent<Renderer> ().material = offMaterial;
		}
	}

	void Update () {
		if (state == 1 || state == 2) {
			cube.GetComponent<Renderer> ().material = onMaterial;
		} else {
			cube.GetComponent<Renderer> ().material = offMaterial;
		}
	}

    void FixedUpdate()
    {
        if (Mathf.Abs(gameObject.GetComponent<Rigidbody>().velocity.x) > 0.5 && !isPlayer)
        {
            playSound();
        }
    }


    private void playSound()
    {
        AkSoundEngine.PostEvent("Play_carga", gameObject);
    }

    void OnTriggerStay(Collider other)
    {
        if (other.CompareTag(otherTag) && state == 1)
        {
            PlayerController player = other.GetComponent<PlayerController>();
            StartCoroutine(player.Die());
        }
    }

    void OnCollisionStay(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayer = true;
        }
    }

    void OnCollisionExit(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayer = false;
        }
    }

}
