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

public class PlaceTileEffect : MonoBehaviour
{ 
     public GameObject tilePrefab; // Префаб плитки
    public GameObject wall;       // Объект стены с компонентом Renderer

    private float tileWidth;      // Ширина плитки
    private float tileHeight;     // Высота плитки
    private float groutWidth;     // Ширина затирки

    void Start()
    {
        // Пример установки размеров плитки и затирки
        SetTileSize(0.3f, 0.3f);
        SetGroutWidth(0.02f);
        PlaceTiles();
    }

    public void SetTileSize(float width, float height)
    {
        tileWidth = width;
        tileHeight = height;
    }

    public void SetGroutWidth(float width)
    {
        groutWidth = width;
    }

    void PlaceTiles()
    {
        if (tilePrefab == null || wall == null)
        {
            Debug.LogError("TilePrefab or Wall is not assigned.");
            return;
        }

        // Получаем границы стены из её компонента Renderer
        Renderer wallRenderer = wall.GetComponent<Renderer>();
        Bounds bounds = wallRenderer.bounds;

        // Получаем координату z центра границ стены
        float posZ = bounds.center.z;

        // Рассчитываем количество плиток по горизонтали и вертикали
        int tileCountHorizontal = Mathf.FloorToInt((bounds.size.x - groutWidth) / (tileWidth + groutWidth));
        int tileCountVertical = Mathf.FloorToInt((bounds.size.y - groutWidth) / (tileHeight + groutWidth));

        // Расставляем плитки
        for (int y = 0; y <= tileCountVertical; y++)
        {
            for (int x = 0; x <= tileCountHorizontal; x++)
            {
                // Вычисляем позицию каждой плитки относительно границ стены
                float posX = bounds.max.x - x * (tileWidth + groutWidth) - tileWidth / 2;
                float posY = bounds.max.y - y * (tileHeight + groutWidth) - tileHeight / 2;

                // Создаем новую плитку и масштабируем её
                GameObject newTile = Instantiate(tilePrefab, new Vector3(posX, posY, posZ), Quaternion.identity);
                newTile.transform.localScale = new Vector3(tileWidth, tileHeight, newTile.transform.localScale.z);
                newTile.transform.parent = transform; // Установить как дочерний объект для удобства
            }
        }
    }
}