2026-07-05 17:19:53 +03:00
|
|
|
using UnityEngine;
|
2026-07-06 18:39:02 +03:00
|
|
|
using UnityEngine.EventSystems;
|
2026-07-05 17:19:53 +03:00
|
|
|
|
|
|
|
|
public class FurnitureBuilder : MonoBehaviour
|
|
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
public enum EditTool { Move, Rotate }
|
|
|
|
|
|
2026-07-05 17:19:53 +03:00
|
|
|
[Header("Настройки")]
|
|
|
|
|
public GameObject currentPrefab;
|
|
|
|
|
public LayerMask floorLayer;
|
|
|
|
|
[SerializeField] private GameObject shopPanel;
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
[Header("Режим редактирования (UI)")]
|
|
|
|
|
[SerializeField] private GameObject editPanel;
|
|
|
|
|
public float rotationSpeed = 5f;
|
|
|
|
|
|
|
|
|
|
// Внутренние переменные состояния
|
|
|
|
|
private GameObject _activeFurniture;
|
|
|
|
|
private FurnitureObject _activeFurnitureScript; // <--- ИЗМЕНИЛИ ИМЯ ЗДЕСЬ
|
|
|
|
|
private EditTool _currentTool = EditTool.Move;
|
|
|
|
|
private Camera _cam;
|
|
|
|
|
|
2026-07-05 17:19:53 +03:00
|
|
|
private void Start()
|
|
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
_cam = Camera.main;
|
2026-07-05 17:19:53 +03:00
|
|
|
currentPrefab = null;
|
2026-07-06 18:39:02 +03:00
|
|
|
editPanel.SetActive(false);
|
2026-07-05 17:19:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
if (_activeFurniture != null)
|
2026-07-05 17:19:53 +03:00
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
HandleEditing();
|
|
|
|
|
}
|
|
|
|
|
else if (currentPrefab != null)
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
2026-07-05 17:19:53 +03:00
|
|
|
{
|
|
|
|
|
TryPlaceFurniture();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
private void HandleEditing()
|
2026-07-05 17:19:53 +03:00
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
Vector3 screenPos = _cam.WorldToScreenPoint(_activeFurniture.transform.position);
|
|
|
|
|
editPanel.transform.position = screenPos + new Vector3(0, 100, 0);
|
|
|
|
|
|
|
|
|
|
if (Input.GetMouseButton(0) && !IsPointerOverUI())
|
2026-07-05 17:19:53 +03:00
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
if (_currentTool == EditTool.Move)
|
|
|
|
|
{
|
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, floorLayer))
|
|
|
|
|
{
|
|
|
|
|
_activeFurniture.transform.position = hit.point;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (_currentTool == EditTool.Rotate)
|
|
|
|
|
{
|
|
|
|
|
float rotX = Input.GetAxis("Mouse X");
|
|
|
|
|
_activeFurniture.transform.Rotate(Vector3.up, -rotX * rotationSpeed);
|
|
|
|
|
}
|
2026-07-05 17:19:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
|
|
|
void TryPlaceFurniture()
|
|
|
|
|
{
|
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, floorLayer))
|
|
|
|
|
{
|
|
|
|
|
_activeFurniture = Instantiate(currentPrefab, hit.point, Quaternion.identity);
|
|
|
|
|
|
|
|
|
|
// <--- ИЗМЕНИЛИ ИМЯ ЗДЕСЬ
|
|
|
|
|
_activeFurnitureScript = _activeFurniture.GetComponent<FurnitureObject>();
|
|
|
|
|
|
|
|
|
|
_activeFurnitureScript.SetEditMode(true);
|
|
|
|
|
editPanel.SetActive(true);
|
|
|
|
|
_currentTool = EditTool.Move;
|
|
|
|
|
currentPrefab = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectMoveTool() { _currentTool = EditTool.Move; }
|
|
|
|
|
public void SelectRotateTool() { _currentTool = EditTool.Rotate; }
|
|
|
|
|
|
|
|
|
|
public void ConfirmPlacement()
|
|
|
|
|
{
|
|
|
|
|
if (_activeFurnitureScript != null && _activeFurnitureScript.IsPlacementValid)
|
|
|
|
|
{
|
|
|
|
|
_activeFurnitureScript.SetEditMode(false);
|
|
|
|
|
_activeFurniture = null;
|
|
|
|
|
_activeFurnitureScript = null;
|
|
|
|
|
editPanel.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Нельзя поставить сюда! Мебель пересекается с препятствием.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 17:19:53 +03:00
|
|
|
public void SelectFurnitureAndCloseShop(GameObject prefabToSelect)
|
|
|
|
|
{
|
|
|
|
|
currentPrefab = prefabToSelect;
|
|
|
|
|
shopPanel.SetActive(false);
|
|
|
|
|
}
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
|
|
|
private bool IsPointerOverUI()
|
2026-07-05 17:19:53 +03:00
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
if (EventSystem.current != null)
|
|
|
|
|
{
|
|
|
|
|
if (EventSystem.current.IsPointerOverGameObject()) return true;
|
|
|
|
|
if (Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2026-07-05 17:19:53 +03:00
|
|
|
}
|
|
|
|
|
}
|