2026-07-06 18:39:02 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class FurnitureObject : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("Настройки")]
|
2026-07-06 21:08:53 +03:00
|
|
|
public LayerMask obstacleLayers;
|
2026-08-09 23:12:01 +03:00
|
|
|
public bool isWallObject;
|
2026-07-06 18:39:02 +03:00
|
|
|
private int _obstaclesCount = 0;
|
|
|
|
|
public bool IsPlacementValid => _obstaclesCount == 0;
|
2026-08-11 20:38:39 +03:00
|
|
|
[HideInInspector] public FurnitureItemSO runtimeItemData;
|
2026-07-06 21:08:53 +03:00
|
|
|
private GameObject _highlightBox;
|
|
|
|
|
private Material _highlightMaterial;
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
void Awake()
|
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
CreateHighlightBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateHighlightBox()
|
|
|
|
|
{
|
2026-08-16 14:34:08 +03:00
|
|
|
// Ищем ВСЕ коллайдеры на объекте
|
|
|
|
|
BoxCollider[] colliders = GetComponents<BoxCollider>();
|
|
|
|
|
if (colliders.Length == 0) return;
|
|
|
|
|
|
|
|
|
|
// Создаем "виртуальную коробку", которая охватит первый коллайдер
|
|
|
|
|
Bounds totalBounds = new Bounds(colliders[0].center, colliders[0].size);
|
|
|
|
|
|
|
|
|
|
// Добавляем в эту коробку все остальные коллайдеры (для формы буквы Г)
|
|
|
|
|
for (int i = 1; i < colliders.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Bounds b = new Bounds(colliders[i].center, colliders[i].size);
|
|
|
|
|
totalBounds.Encapsulate(b);
|
|
|
|
|
}
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
_highlightBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
|
|
|
|
Destroy(_highlightBox.GetComponent<Collider>());
|
|
|
|
|
|
|
|
|
|
_highlightBox.transform.SetParent(transform);
|
2026-08-16 14:34:08 +03:00
|
|
|
|
|
|
|
|
// Ставим зеленую коробку в общий центр и задаем общий размер
|
|
|
|
|
_highlightBox.transform.localPosition = totalBounds.center;
|
2026-07-06 21:08:53 +03:00
|
|
|
_highlightBox.transform.localRotation = Quaternion.identity;
|
2026-08-16 14:34:08 +03:00
|
|
|
// Умножаем на 1.02, чтобы зеленая коробка была чуть-чуть больше мебели (для красоты)
|
|
|
|
|
_highlightBox.transform.localScale = totalBounds.size * 1.02f;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
_highlightMaterial = new Material(Shader.Find("Sprites/Default"));
|
|
|
|
|
_highlightBox.GetComponent<Renderer>().material = _highlightMaterial;
|
|
|
|
|
_highlightBox.SetActive(false);
|
2026-07-06 18:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetEditMode(bool active)
|
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
if (_highlightBox != null)
|
2026-07-06 18:39:02 +03:00
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
_highlightBox.SetActive(active);
|
|
|
|
|
if (active) UpdateColor();
|
2026-07-06 18:39:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
|
{
|
|
|
|
|
if (((1 << other.gameObject.layer) & obstacleLayers) != 0)
|
|
|
|
|
{
|
|
|
|
|
_obstaclesCount++;
|
|
|
|
|
UpdateColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
|
{
|
|
|
|
|
if (((1 << other.gameObject.layer) & obstacleLayers) != 0)
|
|
|
|
|
{
|
|
|
|
|
_obstaclesCount--;
|
|
|
|
|
UpdateColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateColor()
|
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
if (_highlightMaterial != null)
|
2026-07-06 18:39:02 +03:00
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
Color targetColor = IsPlacementValid ? new Color(0, 1, 0, 0.4f) : new Color(1, 0, 0, 0.4f);
|
|
|
|
|
_highlightMaterial.color = targetColor;
|
2026-07-06 18:39:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|