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

public class CubeController : MonoBehaviour
{
    private Renderer rend;
    public GameObject camera;
    public Material offMaterial;
    public Material onMaterial;

    private int cubeIndex;

    private void Start()
    {
        string cubeName = gameObject.name;
        int.TryParse(cubeName.Substring(cubeName.IndexOf("(") + 1, 1), out cubeIndex);

        rend = GetComponent<Renderer>();
    }

    private void OnTriggerEnter(Collider other)
    {
        
        if (other.gameObject.CompareTag("ball"))
        {
            Debug.Log($"CubeController: {this.name} TRIGGER with BALL {other.gameObject.name}");
            if (rend.material.color == offMaterial.color)
            {
                rend.material = onMaterial;
                camera.GetComponent<MainController>().EnableCubeToPlaySound(cubeIndex);
            }
            else
            {
                rend.material = offMaterial;
                camera.GetComponent<MainController>().EnableCubeToPlaySound(cubeIndex);
            }
        }
    }
}
