105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using UnityEngine;
|
|
|
|
public class FurnitureObject : MonoBehaviour
|
|
{
|
|
[HideInInspector] public FurnitureItemSO runtimeItemData;
|
|
|
|
[Header("Тип объекта")]
|
|
public bool isWallObject = false;
|
|
|
|
[Header("Настройки")]
|
|
public LayerMask obstacleLayers;
|
|
|
|
private int _obstaclesCount = 0;
|
|
public bool IsPlacementValid => _obstaclesCount == 0;
|
|
|
|
private GameObject _highlightBox;
|
|
private Material _highlightMaterial;
|
|
|
|
// --- НОВЫЕ ПЕРЕМЕННЫЕ ДЛЯ УМНОГО ПИВОТА ---
|
|
[HideInInspector] public Vector3 bottomOffset;
|
|
[HideInInspector] public Vector3 centerOffset;
|
|
|
|
void Awake()
|
|
{
|
|
CalculateSmartPivot();
|
|
CreateHighlightBox();
|
|
}
|
|
|
|
private void CalculateSmartPivot()
|
|
{
|
|
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++)
|
|
{
|
|
totalBounds.Encapsulate(new Bounds(colliders[i].center, colliders[i].size));
|
|
}
|
|
|
|
// Локальный центр (для идеального вращения)
|
|
centerOffset = totalBounds.center;
|
|
|
|
// Локальный низ (пол) - отнимаем половину высоты от центра
|
|
bottomOffset = totalBounds.center;
|
|
bottomOffset.y -= totalBounds.extents.y;
|
|
}
|
|
|
|
private void CreateHighlightBox()
|
|
{
|
|
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++) totalBounds.Encapsulate(new Bounds(colliders[i].center, colliders[i].size));
|
|
|
|
_highlightBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
|
Destroy(_highlightBox.GetComponent<Collider>());
|
|
|
|
_highlightBox.transform.SetParent(transform);
|
|
_highlightBox.transform.localPosition = totalBounds.center;
|
|
_highlightBox.transform.localRotation = Quaternion.identity;
|
|
_highlightBox.transform.localScale = totalBounds.size * 1.02f;
|
|
|
|
_highlightMaterial = new Material(Shader.Find("Sprites/Default"));
|
|
_highlightBox.GetComponent<Renderer>().material = _highlightMaterial;
|
|
_highlightBox.SetActive(false);
|
|
}
|
|
|
|
public void SetEditMode(bool active)
|
|
{
|
|
if (_highlightBox != null)
|
|
{
|
|
_highlightBox.SetActive(active);
|
|
if (active) UpdateColor();
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
if (_highlightMaterial != null)
|
|
{
|
|
Color targetColor = IsPlacementValid ? new Color(0, 1, 0, 0.4f) : new Color(1, 0, 0, 0.4f);
|
|
_highlightMaterial.color = targetColor;
|
|
}
|
|
}
|
|
} |