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
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
public class FurnitureController : MonoBehaviour
|
2026-07-05 17:19:53 +03:00
|
|
|
|
{
|
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;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
[Tooltip("Слой пола (по нему кликаем)")]
|
2026-07-05 17:19:53 +03:00
|
|
|
|
public LayerMask floorLayer;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
[Tooltip("Слой стен (о них будем биться и скользить)")]
|
|
|
|
|
|
public LayerMask wallLayer;
|
|
|
|
|
|
|
2026-07-05 17:19:53 +03:00
|
|
|
|
[SerializeField] private GameObject shopPanel;
|
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
|
[Header("Режим редактирования (UI)")]
|
|
|
|
|
|
[SerializeField] private GameObject editPanel;
|
|
|
|
|
|
public float rotationSpeed = 5f;
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
[Header("Связь с камерой")]
|
|
|
|
|
|
public MonoBehaviour cameraController;
|
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
|
private GameObject _activeFurniture;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
private FurnitureObject _activeFurnitureScript;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
private EditTool _currentTool = EditTool.Move;
|
|
|
|
|
|
private Camera _cam;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
private bool _isInteracting = false;
|
|
|
|
|
|
private Vector3 _lastInputPos;
|
|
|
|
|
|
private Vector3 _dragOffset;
|
2026-07-05 17:19:53 +03:00
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
2026-07-06 18:39:02 +03:00
|
|
|
|
_cam = Camera.main;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
if (cameraController == null && _cam != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
cameraController = _cam.GetComponent("CameraController") as MonoBehaviour;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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 21:08:53 +03:00
|
|
|
|
// 1. НАЧАЛО ВЗАИМОДЕЙСТВИЯ (Зажали палец)
|
|
|
|
|
|
if (Input.GetMouseButtonDown(0) && !IsPointerOverUI())
|
|
|
|
|
|
{
|
|
|
|
|
|
_lastInputPos = Input.mousePosition;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
if (_currentTool == EditTool.Move)
|
|
|
|
|
|
{
|
|
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
|
RaycastHit[] hits = Physics.SphereCastAll(ray, 0.5f, 100f);
|
|
|
|
|
|
|
|
|
|
|
|
bool foundFurniture = false;
|
|
|
|
|
|
foreach (var hit in hits)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (hit.transform.IsChildOf(_activeFurniture.transform) || hit.transform == _activeFurniture.transform)
|
|
|
|
|
|
{
|
|
|
|
|
|
foundFurniture = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (foundFurniture)
|
|
|
|
|
|
{
|
|
|
|
|
|
// МАГИЯ СМЕЩЕНИЯ: Запоминаем разницу между центром мебели и точкой на полу
|
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit floorHit, 100f, floorLayer))
|
|
|
|
|
|
{
|
|
|
|
|
|
_dragOffset = _activeFurniture.transform.position - floorHit.point;
|
|
|
|
|
|
_dragOffset.y = 0; // Игнорируем высоту
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_dragOffset = Vector3.zero;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StartInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_currentTool == EditTool.Rotate)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. ПРОЦЕСС (Держим палец/мышь и ведем)
|
|
|
|
|
|
if (Input.GetMouseButton(0) && _isInteracting)
|
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))
|
|
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
|
Vector3 currentPos = _activeFurniture.transform.position;
|
|
|
|
|
|
|
|
|
|
|
|
// ДОБАВЛЯЕМ СМЕЩЕНИЕ: теперь мы тянем за ту точку, за которую схватились!
|
|
|
|
|
|
Vector3 targetPos = hit.point + _dragOffset;
|
|
|
|
|
|
targetPos.y = currentPos.y;
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 moveDir = targetPos - currentPos;
|
|
|
|
|
|
float moveDist = moveDir.magnitude;
|
|
|
|
|
|
|
|
|
|
|
|
if (moveDist > 0.001f)
|
|
|
|
|
|
{
|
|
|
|
|
|
BoxCollider box = _activeFurniture.GetComponent<BoxCollider>();
|
|
|
|
|
|
if (box != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 center = _activeFurniture.transform.TransformPoint(box.center);
|
|
|
|
|
|
Vector3 halfExtents = Vector3.Scale(box.size, _activeFurniture.transform.lossyScale) * 0.49f;
|
|
|
|
|
|
|
|
|
|
|
|
if (Physics.BoxCast(center, halfExtents, moveDir.normalized, out RaycastHit wallHit, _activeFurniture.transform.rotation, moveDist, wallLayer))
|
|
|
|
|
|
{
|
|
|
|
|
|
float safeDist = Mathf.Max(0f, wallHit.distance - 0.01f);
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 remainingMove = moveDir - (moveDir.normalized * safeDist);
|
|
|
|
|
|
Vector3 slideMove = Vector3.ProjectOnPlane(remainingMove, wallHit.normal);
|
|
|
|
|
|
|
|
|
|
|
|
if (slideMove.magnitude > 0.001f)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 newCenter = center + moveDir.normalized * safeDist;
|
|
|
|
|
|
if (Physics.BoxCast(newCenter, halfExtents, slideMove.normalized, out RaycastHit slideHit, _activeFurniture.transform.rotation, slideMove.magnitude, wallLayer))
|
|
|
|
|
|
{
|
|
|
|
|
|
slideMove = slideMove.normalized * Mathf.Max(0f, slideHit.distance - 0.01f);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
targetPos = currentPos + (moveDir.normalized * safeDist) + slideMove;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
targetPos.y = currentPos.y;
|
|
|
|
|
|
_activeFurniture.transform.position = targetPos;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_currentTool == EditTool.Rotate)
|
|
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
|
float deltaX = Input.mousePosition.x - _lastInputPos.x;
|
|
|
|
|
|
_activeFurniture.transform.Rotate(Vector3.up, -deltaX * rotationSpeed * 0.2f);
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
_lastInputPos = Input.mousePosition;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. КОНЕЦ ВЗАИМОДЕЙСТВИЯ (Отпустили палец)
|
|
|
|
|
|
if (Input.GetMouseButtonUp(0) && _isInteracting)
|
|
|
|
|
|
{
|
|
|
|
|
|
EndInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. ПОЗИЦИОНИРОВАНИЕ UI
|
|
|
|
|
|
if (!_isInteracting)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateUIPosition();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void StartInteraction()
|
|
|
|
|
|
{
|
|
|
|
|
|
_isInteracting = true;
|
|
|
|
|
|
if (editPanel.activeSelf) editPanel.SetActive(false);
|
|
|
|
|
|
if (_currentTool == EditTool.Move) LockCamera(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void EndInteraction()
|
|
|
|
|
|
{
|
|
|
|
|
|
_isInteracting = false;
|
|
|
|
|
|
UpdateUIPosition();
|
|
|
|
|
|
editPanel.SetActive(true);
|
|
|
|
|
|
if (_currentTool == EditTool.Move) LockCamera(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateUIPosition()
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 screenPos = _cam.WorldToScreenPoint(_activeFurniture.transform.position);
|
|
|
|
|
|
|
|
|
|
|
|
if (screenPos.z > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!editPanel.activeSelf) editPanel.SetActive(true);
|
|
|
|
|
|
float yOffset = Screen.height * 0.15f;
|
|
|
|
|
|
editPanel.transform.position = new Vector3(screenPos.x, screenPos.y + yOffset, editPanel.transform.position.z);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (editPanel.activeSelf) editPanel.SetActive(false);
|
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);
|
|
|
|
|
|
_currentTool = EditTool.Move;
|
|
|
|
|
|
currentPrefab = null;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
_isInteracting = false;
|
|
|
|
|
|
LockCamera(false);
|
|
|
|
|
|
|
|
|
|
|
|
UpdateUIPosition();
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
public void SelectMoveTool()
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentTool = EditTool.Move;
|
|
|
|
|
|
LockCamera(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SelectRotateTool()
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentTool = EditTool.Rotate;
|
|
|
|
|
|
LockCamera(true);
|
|
|
|
|
|
}
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
|
|
|
|
|
public void ConfirmPlacement()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_activeFurnitureScript != null && _activeFurnitureScript.IsPlacementValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
_activeFurnitureScript.SetEditMode(false);
|
|
|
|
|
|
_activeFurniture = null;
|
|
|
|
|
|
_activeFurnitureScript = null;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
editPanel.SetActive(false);
|
|
|
|
|
|
|
|
|
|
|
|
LockCamera(false);
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CancelPlacement()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_activeFurniture != null)
|
2026-07-06 18:39:02 +03:00
|
|
|
|
{
|
2026-07-06 21:08:53 +03:00
|
|
|
|
Destroy(_activeFurniture);
|
|
|
|
|
|
_activeFurniture = null;
|
|
|
|
|
|
_activeFurnitureScript = null;
|
|
|
|
|
|
editPanel.SetActive(false);
|
|
|
|
|
|
|
|
|
|
|
|
LockCamera(false);
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
private void LockCamera(bool isLocked)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (cameraController != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
cameraController.enabled = !isLocked;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|