Files
JuegoSim/Assets/_Project/Scripts/Code/Movement Scripts/GizmoRotateElement.cs
T
2026-07-21 08:56:10 +03:00

89 lines
2.8 KiB
C#

using UnityEngine;
public class GizmoRotateElement : MonoBehaviour
{
[SerializeField] private GizmoDirection direction;
[SerializeField] private float rotationSensitivity = 0.3f;
[SerializeField] private Material SelectedMat;
[SerializeField] private bool enableSnapping = true;
[SerializeField] private float snapAngle = 15f;
private Transform attachedTransform;
private AxisRotateStrategy rotateStrategy;
private GhostManagerV2 ghostManager;
private Material OGMat;
private bool isDragging;
private Vector2 lastMousePos;
private void Start()
{
OGMat = GetComponent<Renderer>().material;
ghostManager = GhostManagerV2.getInstance();
attachedTransform = transform.GetComponentInParent<IInteractable>().transform;
}
// private void Update()
// {
// if (!isDragging || rotateStrategy == null) return;
// Vector2 delta = (Vector2)Input.mousePosition - lastMousePos;
// lastMousePos = Input.mousePosition;
// float degrees = (delta.x + delta.y) * rotationSensitivity;
// if (degrees != 0f)
// rotateStrategy.Rotate(degrees);
// }
public void onSelectEnter()
{
ghostManager.SetupExistingBlock(attachedTransform.gameObject);
// The gizmo root (parent) position determines the pivot
Vector3 lockedPosition = transform.parent.position;
rotateStrategy = new AxisRotateStrategy(GetLocalAxis(), lockedPosition)
{
EnableSnapping = enableSnapping,
SnapAngle = snapAngle
};
rotateStrategy.BeginRotate();
ghostManager.SetMovementStrategy(rotateStrategy);
if (!InputModeManager.Has(InputMode.Gizmo))
InputModeManager.Push(InputMode.Gizmo);
lastMousePos = Input.mousePosition;
isDragging = true;
GetComponent<MeshRenderer>().material = SelectedMat;
Debug.Log($"Rotate drag started on {direction} axis");
}
public void onSelectExit()
{
isDragging = false;
if (InputModeManager.Has(InputMode.Gizmo))
InputModeManager.Pop(InputMode.Gizmo);
if (rotateStrategy != null)
{
rotateStrategy.EndRotate();
ghostManager.TryCommitOrCancel();
rotateStrategy = null;
Debug.Log("Rotate drag ended");
}
}
public void ResetMat()
{
GetComponent<MeshRenderer>().material = OGMat;
}
private AxisRotateStrategy.LocalAxis GetLocalAxis() => direction switch
{
GizmoDirection.X => AxisRotateStrategy.LocalAxis.Right,
GizmoDirection.Y => AxisRotateStrategy.LocalAxis.Up,
GizmoDirection.Z => AxisRotateStrategy.LocalAxis.Forward,
_ => AxisRotateStrategy.LocalAxis.Up,
};
}