Делаю режим строительства
This commit is contained in:
1 parent
09be8e6737
commit
4ccd30d788
11 files changed
+1422
-163
No files matched your search
@@ -1,47 +1,118 @@
|
||||
using System;
|
||||
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 (Input.GetMouseButtonDown(0)&&!shopPanel.activeSelf)
|
||||
if (_activeFurniture != null)
|
||||
{
|
||||
if (currentPrefab != null)
|
||||
HandleEditing();
|
||||
}
|
||||
else if (currentPrefab != null)
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
||||
{
|
||||
TryPlaceFurniture();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TryPlaceFurniture()
|
||||
private void HandleEditing()
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
if (Physics.Raycast(ray, out hit, 100f, floorLayer))
|
||||
Vector3 screenPos = _cam.WorldToScreenPoint(_activeFurniture.transform.position);
|
||||
editPanel.transform.position = screenPos + new Vector3(0, 100, 0);
|
||||
|
||||
if (Input.GetMouseButton(0) && !IsPointerOverUI())
|
||||
{
|
||||
Instantiate(currentPrefab, hit.point, Quaternion.identity);
|
||||
currentPrefab = null;
|
||||
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);
|
||||
}
|
||||
public void SelectFurniture(GameObject prefabToSelect)
|
||||
|
||||
private bool IsPointerOverUI()
|
||||
{
|
||||
currentPrefab = prefabToSelect;
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user