Implement debug switching

This commit is contained in:
2025-12-18 01:00:17 +08:00
parent a991b5a0ee
commit 871dd5014b
5 changed files with 73 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ namespace Services {
public class InputService : MonoBehaviour, IInputService
{
public event Action<Vector2Int, Vector2Int> OnSwapRequested;
public event Action<Vector2Int> OnSwitchRequested;
private Camera inputCamera;
private Vector2 pointerDownScreenPos;
@@ -21,14 +22,14 @@ namespace Services {
private void Update()
{
if (TryGetPrimaryPointerState(out var isDown, out var screenPos))
if (TryGetPrimaryPointerState(out bool isDown, out Vector2 screenPos))
{
if (!this.isPointerDown && isDown) {
pointerDownScreenPos = screenPos;
this.pointerDownScreenPos = screenPos;
}
if (this.isPointerDown && !isDown) {
TryEmitSwap(pointerDownScreenPos, screenPos);
TryEmitSwap(this.pointerDownScreenPos, screenPos);
}
this.isPointerDown = isDown;
@@ -38,6 +39,12 @@ namespace Services {
// No pointer available this frame (rare). Ensure we don't get stuck.
this.isPointerDown = false;
}
#if UNITY_EDITOR
if (TryGetSecondaryPointerClick(out screenPos)) {
OnSwitchRequested?.Invoke(WorldToCell(this.inputCamera.ScreenToWorldPoint(screenPos)));
}
#endif
}
private void TryEmitSwap(Vector2 downScreen, Vector2 upScreen)
@@ -105,5 +112,15 @@ namespace Services {
isDown = Input.GetMouseButton(0);
return true;
}
private static bool TryGetSecondaryPointerClick(out Vector2 screenPosition) {
if (Input.GetMouseButtonDown(1)) {
screenPosition = Input.mousePosition;
return true;
}
screenPosition = Input.mousePosition;
return false;
}
}
}