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

public class FloorVisibility : MonoBehaviour {
    [SerializeField] private float floorHeightOffset = 2f;
    [SerializeField] private bool dynamicFloorPosition;
    [SerializeField] private List<Renderer> ignorRendererList;
    private int floor;
    private Renderer[] rendererArray;
    private void Awake() => rendererArray = GetComponentsInChildren<Renderer>(true);

    private void Start() {
        floor = LevelGrid.GetFloor(transform.position);
        if (floor == 0 && !dynamicFloorPosition) Destroy(this);
    }

    private void Update() {
        if (dynamicFloorPosition) floor = LevelGrid.GetFloor(transform.position);
        Show(CameraController.Instance.CameraHeight > LevelGrid.FLOOR_HEIGHT * floor + floorHeightOffset || floor == 0);
    }

    private void Show(bool showing) {
        foreach (Renderer rendererItem in rendererArray) {
            if (ignorRendererList.Contains(rendererItem)) continue;
            rendererItem.enabled = showing;
        }
    }
}