屏幕问题

This commit is contained in:
2026-01-28 16:20:19 +08:00
parent 7464cd8b6c
commit 7d7e2fcdf0
20 changed files with 771 additions and 479 deletions
@@ -7,25 +7,42 @@ namespace AibisDream
{
public class ShapeDragger : MonoBehaviour, IBlockShapeDragger, IInteraction
{
[Header("拖拽设置")]
#region
[SerializeField] private bool isDraggable = true;
[SerializeField] private float dragScale = 1.1f;
// [SerializeField] private float dragScale = 1.1f;
[SerializeField] private int dragLayerId;
[SerializeField] private int dragLayerOrder;
[SerializeField] private int idleLayerId;
[SerializeField] private int idleLayerOrder;
#endregion
// 拖拽状态
private DragState _currentState = DragState.Idle;
private DragState _currentState = DragState.Placed;
public DragState CurrentState
{
get => _currentState;
set
{
if (value == _currentState) return;
// 处理状态切换
HandleStateChange(value);
_currentState = value;
}
}
private SnapResult _snapResult;
// 组件引用
private BlockShape _blockShape;
private EventTriggerEx _eventTrigger;
public DragState CurrentState => _currentState;
private SpriteRenderer _spriteRenderer;
public bool IsDraggable { get => isDraggable; set => isDraggable = value; }
public IBlockPuzzleGrid TargetGrid => BlockPuzzleSystem.Instance.grid;
public IBlockShape BlockShape => _blockShape;
// 事件
public event Action<IBlockShape> OnDragStart;
public event Action<IBlockShape> OnDragUpdate;
@@ -35,6 +52,7 @@ namespace AibisDream
{
_blockShape = GetComponent<BlockShape>();
_eventTrigger = GetComponent<EventTriggerEx>();
_spriteRenderer = GetComponent<SpriteRenderer>();
RegisterEvent();
}
@@ -53,7 +71,7 @@ namespace AibisDream
private void Update()
{
if (_currentState == DragState.Dragging)
if (CurrentState == DragState.Dragging)
{
UpdateDrag();
}
@@ -67,7 +85,7 @@ namespace AibisDream
}
// 如果当前物体不在拖拽中则进入拖拽逻辑
if (_currentState == DragState.Idle || _currentState == DragState.Placed)
if (CurrentState == DragState.Idle || CurrentState == DragState.Placed)
{
StartDrag();
}
@@ -86,52 +104,36 @@ namespace AibisDream
TargetGrid?.TryRemoveShapeFromGrid(_blockShape);
TargetGrid?.ResetPreviewGrid();
_currentState = DragState.Dragging;
// 处理层级和缩放
HandleDragStart();
CurrentState = DragState.Dragging;
OnDragStart?.Invoke(_blockShape);
}
private void HandleDragStart()
{
// 调整缩放和层级
transform.localScale = dragScale * Vector3.one;
}
public void CancelDrag()
{
throw new NotImplementedException();
}
public void EndDrag()
{
TargetGrid?.ResetPreviewGrid();
OnDragEnd?.Invoke(_blockShape, CurrentState == DragState.Placed);
// 先吸附到回收槽
var recycled = BlockPuzzleSystem.Instance.TrySnapToRecycleSlot(_blockShape);
if (recycled)
{
CurrentState = DragState.Idle;
return;
}
// 如果吸附到了就靠过去
// 吸附到网格
TargetGrid?.ResetPreviewGrid();
if (_snapResult.canSnap)
{
_currentState = DragState.Placed;
CurrentState = DragState.Placed;
transform.localScale = Vector3.one;
TargetGrid.TrySnapToGrid(_blockShape, _snapResult.RootCell);
// 处理Cursor
_eventTrigger.OnPointerUp(null);
OnDragEnd?.Invoke(_blockShape, _currentState == DragState.Placed);
}
// TODO: 无法吸附直接不处理
// else
// {
// _currentState = DragState.Idle;
// // transform.position = _originalPosition;
// transform.localScale = Vector3.one;
// // BlockPuzzleSystem.Instance.shapeRack.RecycleBlockShape(_blockShape);
// }
// TODO: 都没吸附到,则恢复原位
TargetGrid.GridVisualizer.UpdateAllCellsVisual();
}
@@ -154,6 +156,24 @@ namespace AibisDream
OnDragUpdate?.Invoke(_blockShape);
}
private void HandleStateChange(DragState newState)
{
switch (newState)
{
case DragState.Idle:
_spriteRenderer.sortingOrder = dragLayerOrder;
_spriteRenderer.sortingLayerID = dragLayerId;
break;
case DragState.Dragging:
_spriteRenderer.sortingOrder = dragLayerOrder;
_spriteRenderer.sortingLayerID = dragLayerId;
break;
case DragState.Placed:
_spriteRenderer.sortingOrder = idleLayerOrder;
_spriteRenderer.sortingLayerID = idleLayerId;
break;
}
}
private Vector3 GetMouseWorldPosition()
{