2026-07-05 17:19:53 +03:00
|
|
|
|
using UnityEngine;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
using UnityEngine.EventSystems;
|
2026-08-09 23:12:01 +03:00
|
|
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
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("Настройки")]
|
2026-08-18 01:15:31 +03:00
|
|
|
|
public FurnitureItemSO currentItemData;
|
2026-07-05 17:19:53 +03:00
|
|
|
|
public LayerMask floorLayer;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
public LayerMask wallLayer;
|
2026-07-11 18:05:11 +03:00
|
|
|
|
public LayerMask furnitureLayer;
|
2026-07-09 15:12:40 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
[Header("Связь с UI")]
|
2026-07-05 17:19:53 +03:00
|
|
|
|
[SerializeField] private GameObject shopPanel;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
[SerializeField] private GameObject editPanel;
|
2026-07-09 15:12:40 +03:00
|
|
|
|
[SerializeField] private GameObject rotate90Button;
|
2026-08-18 01:15:31 +03:00
|
|
|
|
public FurniturePaletteUI paletteUI; // Ссылка на скрипт палитры цветов
|
|
|
|
|
|
|
|
|
|
|
|
[Header("Настройки взаимодействия")]
|
2026-07-06 18:39:02 +03:00
|
|
|
|
public float rotationSpeed = 5f;
|
2026-08-09 23:12:01 +03:00
|
|
|
|
public float doubleClickThreshold = 0.3f;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
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;
|
2026-07-09 13:35:27 +03:00
|
|
|
|
private Vector3 _dragOffset;
|
2026-08-09 23:12:01 +03:00
|
|
|
|
private float _lastClickTime = 0f;
|
2026-07-09 13:35:27 +03:00
|
|
|
|
|
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-08-09 23:12:01 +03:00
|
|
|
|
currentItemData = null;
|
2026-07-06 18:39:02 +03:00
|
|
|
|
editPanel.SetActive(false);
|
2026-07-09 13:35:27 +03:00
|
|
|
|
if (rotate90Button != null) rotate90Button.SetActive(false);
|
2026-07-05 17:19:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// 1. Если мебель уже выбрана и находится в режиме редактирования
|
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();
|
|
|
|
|
|
}
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// 2. Если мы выбрали предмет в магазине и готовы его поставить
|
2026-08-09 23:12:01 +03:00
|
|
|
|
else if (currentItemData != null)
|
2026-07-06 18:39:02 +03:00
|
|
|
|
{
|
|
|
|
|
|
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
2026-07-05 17:19:53 +03:00
|
|
|
|
TryPlaceFurniture();
|
|
|
|
|
|
}
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// 3. Режим ожидания: ловим двойной клик по УЖЕ стоящей мебели
|
2026-07-09 15:12:40 +03:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Input.GetMouseButtonDown(0) && !shopPanel.activeSelf && !IsPointerOverUI())
|
2026-07-22 19:15:14 +03:00
|
|
|
|
{
|
|
|
|
|
|
float timeSinceLastClick = Time.time - _lastClickTime;
|
|
|
|
|
|
|
|
|
|
|
|
if (timeSinceLastClick <= doubleClickThreshold)
|
|
|
|
|
|
{
|
|
|
|
|
|
TrySelectPlacedFurniture();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 23:12:01 +03:00
|
|
|
|
_lastClickTime = Time.time;
|
2026-07-22 19:15:14 +03:00
|
|
|
|
}
|
2026-07-09 15:12:40 +03:00
|
|
|
|
}
|
2026-07-05 17:19:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (!_activeFurnitureScript.isWallObject && Physics.Raycast(ray, out RaycastHit floorHit, 100f, floorLayer))
|
2026-07-06 21:08:53 +03:00
|
|
|
|
{
|
|
|
|
|
|
_dragOffset = _activeFurniture.transform.position - floorHit.point;
|
2026-07-09 13:35:27 +03:00
|
|
|
|
_dragOffset.y = 0;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
2026-07-09 13:35:27 +03:00
|
|
|
|
else _dragOffset = Vector3.zero;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
|
|
|
|
|
StartInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_currentTool == EditTool.Rotate)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartInteraction();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Input.GetMouseButton(0) && _isInteracting)
|
2026-07-05 17:19:53 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
|
|
2026-07-06 18:39:02 +03:00
|
|
|
|
if (_currentTool == EditTool.Move)
|
|
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (_activeFurnitureScript.isWallObject)
|
2026-07-06 18:39:02 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, wallLayer))
|
2026-07-06 21:08:53 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
_activeFurniture.transform.position = hit.point;
|
|
|
|
|
|
_activeFurniture.transform.rotation = Quaternion.LookRotation(hit.normal);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, floorLayer))
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Вычисляем настоящий низ мебели
|
|
|
|
|
|
Vector3 worldBottomOffset = _activeFurniture.transform.TransformVector(_activeFurnitureScript.bottomOffset);
|
|
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
Vector3 currentPos = _activeFurniture.transform.position;
|
|
|
|
|
|
Vector3 targetPos = hit.point + _dragOffset;
|
2026-08-18 01:15:31 +03:00
|
|
|
|
|
|
|
|
|
|
// Жестко привязываем дно к полу
|
|
|
|
|
|
targetPos.y = hit.point.y - worldBottomOffset.y;
|
2026-07-11 18:05:11 +03:00
|
|
|
|
|
|
|
|
|
|
Vector3 moveDir = targetPos - currentPos;
|
|
|
|
|
|
float moveDist = moveDir.magnitude;
|
|
|
|
|
|
|
|
|
|
|
|
if (moveDist > 0.001f)
|
2026-07-06 21:08:53 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
BoxCollider box = _activeFurniture.GetComponent<BoxCollider>();
|
|
|
|
|
|
if (box != null)
|
2026-07-06 21:08:53 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
Vector3 center = _activeFurniture.transform.TransformPoint(box.center);
|
|
|
|
|
|
Vector3 halfExtents = Vector3.Scale(box.size, _activeFurniture.transform.lossyScale) * 0.49f;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (Physics.BoxCast(center, halfExtents, moveDir.normalized, out RaycastHit wallHit, _activeFurniture.transform.rotation, moveDist, wallLayer))
|
2026-07-06 21:08:53 +03:00
|
|
|
|
{
|
2026-07-11 18:05:11 +03:00
|
|
|
|
float safeDist = Mathf.Max(0f, wallHit.distance - 0.01f);
|
|
|
|
|
|
Vector3 remainingMove = moveDir - (moveDir.normalized * safeDist);
|
|
|
|
|
|
Vector3 slideMove = Vector3.ProjectOnPlane(remainingMove, wallHit.normal);
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-18 01:15:31 +03:00
|
|
|
|
|
|
|
|
|
|
targetPos.y = hit.point.y - worldBottomOffset.y;
|
2026-07-11 18:05:11 +03:00
|
|
|
|
_activeFurniture.transform.position = targetPos;
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
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;
|
2026-07-11 18:05:11 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Вращаем вокруг геометрического центра коллайдеров
|
|
|
|
|
|
Vector3 worldCenter = _activeFurniture.transform.TransformPoint(_activeFurnitureScript.centerOffset);
|
|
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (_activeFurnitureScript.isWallObject)
|
2026-08-18 01:15:31 +03:00
|
|
|
|
_activeFurniture.transform.RotateAround(worldCenter, _activeFurniture.transform.forward, -deltaX * rotationSpeed * 0.2f);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
else
|
2026-08-18 01:15:31 +03:00
|
|
|
|
_activeFurniture.transform.RotateAround(worldCenter, 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (Input.GetMouseButtonUp(0) && _isInteracting) EndInteraction();
|
|
|
|
|
|
if (!_isInteracting) UpdateUIPosition();
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-09 15:12:40 +03:00
|
|
|
|
private void TrySelectPlacedFurniture()
|
|
|
|
|
|
{
|
|
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 1000f, furnitureLayer, QueryTriggerInteraction.Collide))
|
2026-07-09 15:12:40 +03:00
|
|
|
|
{
|
|
|
|
|
|
FurnitureObject furnScript = hit.collider.GetComponentInParent<FurnitureObject>();
|
|
|
|
|
|
if (furnScript != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_activeFurniture = furnScript.gameObject;
|
|
|
|
|
|
_activeFurnitureScript = furnScript;
|
|
|
|
|
|
_activeFurnitureScript.SetEditMode(true);
|
|
|
|
|
|
|
|
|
|
|
|
_currentTool = EditTool.Move;
|
|
|
|
|
|
if (rotate90Button != null) rotate90Button.SetActive(false);
|
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Показываем палитру цветов
|
2026-08-11 20:38:39 +03:00
|
|
|
|
if (paletteUI != null) paletteUI.ShowColors(_activeFurnitureScript);
|
|
|
|
|
|
|
2026-07-09 15:12:40 +03:00
|
|
|
|
_isInteracting = false;
|
|
|
|
|
|
LockCamera(false);
|
|
|
|
|
|
UpdateUIPosition();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
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;
|
2026-07-09 13:35:27 +03:00
|
|
|
|
float targetX = screenPos.x;
|
|
|
|
|
|
float targetY = screenPos.y + yOffset;
|
|
|
|
|
|
|
|
|
|
|
|
RectTransform rect = editPanel.GetComponent<RectTransform>();
|
|
|
|
|
|
if (rect != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
float paddingX = (rect.rect.width / 2f) * editPanel.transform.lossyScale.x;
|
|
|
|
|
|
float paddingY = (rect.rect.height / 2f) * editPanel.transform.lossyScale.y;
|
|
|
|
|
|
targetX = Mathf.Clamp(targetX, paddingX, Screen.width - paddingX);
|
|
|
|
|
|
targetY = Mathf.Clamp(targetY, paddingY, Screen.height - paddingY);
|
|
|
|
|
|
}
|
|
|
|
|
|
editPanel.transform.position = new Vector3(targetX, targetY, editPanel.transform.position.z);
|
2026-07-06 21:08:53 +03:00
|
|
|
|
}
|
2026-07-11 18:05:11 +03:00
|
|
|
|
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);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Определяем, куда пускать луч (на стену или на пол)
|
2026-08-09 23:12:01 +03:00
|
|
|
|
LayerMask targetLayer = currentItemData.isWallObject ? wallLayer : floorLayer;
|
2026-07-11 18:05:11 +03:00
|
|
|
|
|
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 100f, targetLayer))
|
2026-07-06 18:39:02 +03:00
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
_isInteracting = true; // Блокируем новые клики, пока идет асинхронная загрузка
|
2026-08-09 23:12:01 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Считаем стартовый поворот (для стен - "лицом" от стены)
|
2026-08-09 23:12:01 +03:00
|
|
|
|
Quaternion spawnRot = Quaternion.identity;
|
|
|
|
|
|
if (currentItemData.isWallObject)
|
|
|
|
|
|
spawnRot = Quaternion.LookRotation(hit.normal);
|
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Асинхронно загружаем префаб через Addressables
|
2026-08-09 23:12:01 +03:00
|
|
|
|
currentItemData.prefabRef.InstantiateAsync(hit.point, spawnRot).Completed += handle =>
|
2026-07-11 18:05:11 +03:00
|
|
|
|
{
|
2026-08-09 23:12:01 +03:00
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
_activeFurniture = handle.Result;
|
|
|
|
|
|
_activeFurnitureScript = _activeFurniture.GetComponent<FurnitureObject>();
|
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 1. ИНЪЕКЦИЯ ДАННЫХ В ПРЕФАБ
|
|
|
|
|
|
// ==========================================
|
2026-08-11 20:38:39 +03:00
|
|
|
|
_activeFurnitureScript.runtimeItemData = currentItemData;
|
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 2. ПРИМЕНЕНИЕ ЦВЕТОВ (НОВАЯ АРХИТЕКТУРА)
|
|
|
|
|
|
// ==========================================
|
2026-08-09 23:12:01 +03:00
|
|
|
|
FurnitureVisuals visuals = _activeFurniture.GetComponent<FurnitureVisuals>();
|
2026-08-18 01:15:31 +03:00
|
|
|
|
if (visuals != null)
|
2026-08-09 23:12:01 +03:00
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Автоматически красим все детали первыми доступными цветами из списков
|
|
|
|
|
|
visuals.ApplyDefaultColors(currentItemData);
|
2026-08-09 23:12:01 +03:00
|
|
|
|
}
|
2026-08-11 20:38:39 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// Открываем обновленную палитру с вкладками
|
2026-08-11 20:38:39 +03:00
|
|
|
|
if (paletteUI != null) paletteUI.ShowColors(_activeFurnitureScript);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 3. УМНЫЙ ПИВОТ (АВТО-ВЫРАВНИВАНИЕ)
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
if (!currentItemData.isWallObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Вычисляем, где находится математическое дно у модели с учетом ее поворота
|
|
|
|
|
|
Vector3 worldBottomOffset = _activeFurniture.transform.TransformVector(_activeFurnitureScript.bottomOffset);
|
|
|
|
|
|
|
|
|
|
|
|
// Смещаем объект так, чтобы его дно оказалось ровно в точке клика на полу
|
|
|
|
|
|
_activeFurniture.transform.position = hit.point - worldBottomOffset;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 4. ПЕРЕХОД В РЕЖИМ РЕДАКТИРОВАНИЯ
|
|
|
|
|
|
// ==========================================
|
2026-08-09 23:12:01 +03:00
|
|
|
|
_activeFurnitureScript.SetEditMode(true);
|
|
|
|
|
|
_currentTool = EditTool.Move;
|
|
|
|
|
|
if (rotate90Button != null) rotate90Button.SetActive(false);
|
2026-07-09 13:35:27 +03:00
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
|
currentItemData = null; // Очищаем карточку из "буфера" контроллера
|
2026-08-09 23:12:01 +03:00
|
|
|
|
_isInteracting = false;
|
|
|
|
|
|
LockCamera(false);
|
|
|
|
|
|
UpdateUIPosition();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
Debug.LogError($"Не удалось загрузить {currentItemData.itemName}. Проверьте галочку Addressable на префабе!");
|
2026-08-09 23:12:01 +03:00
|
|
|
|
_isInteracting = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-09 13:35:27 +03:00
|
|
|
|
public void Rotate90Degrees()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_activeFurniture != null)
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
Vector3 worldCenter = _activeFurniture.transform.TransformPoint(_activeFurnitureScript.centerOffset);
|
|
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
if (_activeFurnitureScript.isWallObject)
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
_activeFurniture.transform.RotateAround(worldCenter, _activeFurniture.transform.forward, 90f);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
|
_activeFurniture.transform.RotateAround(worldCenter, Vector3.up, 90f);
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 euler = _activeFurniture.transform.eulerAngles;
|
|
|
|
|
|
_activeFurniture.transform.eulerAngles = new Vector3(euler.x, Mathf.Round(euler.y / 90f) * 90f, euler.z);
|
2026-07-11 18:05:11 +03:00
|
|
|
|
}
|
2026-07-09 13:35:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
public void SelectMoveTool() { _currentTool = EditTool.Move; if (rotate90Button != null) rotate90Button.SetActive(false); LockCamera(false); }
|
|
|
|
|
|
public void SelectRotateTool() { _currentTool = EditTool.Rotate; if (rotate90Button != null) rotate90Button.SetActive(true); 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-08-18 01:15:31 +03:00
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
editPanel.SetActive(false);
|
2026-08-18 01:15:31 +03:00
|
|
|
|
if (paletteUI != null) paletteUI.HideColors(); // Прячем палитру
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
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-08-09 23:12:01 +03:00
|
|
|
|
Addressables.ReleaseInstance(_activeFurniture);
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
_activeFurniture = null;
|
|
|
|
|
|
_activeFurnitureScript = null;
|
2026-08-18 01:15:31 +03:00
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
editPanel.SetActive(false);
|
2026-08-18 01:15:31 +03:00
|
|
|
|
if (paletteUI != null) paletteUI.HideColors(); // Прячем палитру
|
|
|
|
|
|
|
2026-07-06 21:08:53 +03:00
|
|
|
|
LockCamera(false);
|
2026-07-06 18:39:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 23:12:01 +03:00
|
|
|
|
public void SelectFurnitureAndCloseShop(FurnitureItemSO itemToSelect)
|
2026-07-05 17:19:53 +03:00
|
|
|
|
{
|
2026-08-09 23:12:01 +03:00
|
|
|
|
currentItemData = itemToSelect;
|
2026-07-05 17:19:53 +03:00
|
|
|
|
shopPanel.SetActive(false);
|
|
|
|
|
|
}
|
2026-07-06 18:39:02 +03:00
|
|
|
|
|
2026-07-11 18:05:11 +03:00
|
|
|
|
private void LockCamera(bool isLocked) { if (cameraController != null) cameraController.enabled = !isLocked; }
|
2026-07-06 21:08:53 +03:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|