Files
SummerGame/Assets/Scripts/FurnitureController.cs
T

118 lines
3.8 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.EventSystems;
public class FurnitureBuilder : MonoBehaviour
{
public enum EditTool { Move, Rotate }
[Header("Настройки")]
public GameObject currentPrefab;
public LayerMask floorLayer;
[SerializeField] private GameObject shopPanel;
[Header("Режим редактирования (UI)")]
[SerializeField] private GameObject editPanel;
public float rotationSpeed = 5f;
// Внутренние переменные состояния
private GameObject _activeFurniture;
private FurnitureObject _activeFurnitureScript; // <--- ИЗМЕНИЛИ ИМЯ ЗДЕСЬ
private EditTool _currentTool = EditTool.Move;
private Camera _cam;
private void Start()
{
_cam = Camera.main;
currentPrefab = null;
editPanel.SetActive(false);
}
void Update()
{
if (_activeFurniture != null)
{
HandleEditing();
}
else if (currentPrefab != null)
{
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
{
TryPlaceFurniture();
}
}
}
private void HandleEditing()
{
Vector3 screenPos = _cam.WorldToScreenPoint(_activeFurniture.transform.position);
editPanel.transform.position = screenPos + new Vector3(0, 100, 0);
if (Input.GetMouseButton(0) && !IsPointerOverUI())
{
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);
}
}
}
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("Нельзя поставить сюда! Мебель пересекается с препятствием.");
}
}
public void SelectFurnitureAndCloseShop(GameObject prefabToSelect)
{
currentPrefab = prefabToSelect;
shopPanel.SetActive(false);
}
private bool IsPointerOverUI()
{
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;
}
}