[Puzzles][Input] Update input handling to not mistakingly miss frames. Add debug frame input debugging.

This commit is contained in:
Michal Pikulski
2025-09-03 16:55:21 +02:00
parent 93242b2702
commit 1dfcee3d2c
9 changed files with 196 additions and 26 deletions

View File

@@ -31,15 +31,23 @@ public class DebugUIMessage : MonoBehaviour
textGO.transform.SetParent(canvas.transform);
messageText = textGO.AddComponent<Text>();
messageText.alignment = TextAnchor.MiddleCenter;
messageText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
messageText.fontSize = 32;
// Try to load a custom font from Resources/Fonts/DebugFont.ttf
Font customFont = Resources.Load<Font>("Fonts/DebugFont");
if (customFont != null)
messageText.font = customFont;
else
messageText.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
messageText.fontSize = 16;
messageText.color = Color.yellow;
var outline = textGO.AddComponent<Outline>();
outline.effectColor = Color.black;
outline.effectDistance = new Vector2(2, -2);
var rect = messageText.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0.5f, 0.1f);
rect.anchorMax = new Vector2(0.5f, 0.1f);
rect.anchorMin = new Vector2(0.5f, 0.5f);
rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.anchoredPosition = Vector2.zero;
rect.sizeDelta = new Vector2(800, 100);
rect.sizeDelta = new Vector2(400, 40);
messageText.text = "";
}
@@ -59,4 +67,3 @@ public class DebugUIMessage : MonoBehaviour
messageText.enabled = false;
}
}

View File

@@ -4,22 +4,41 @@ using System;
public class InputManager : MonoBehaviour
{
public static InputManager Instance { get; private set; }
private static InputManager _instance;
public static InputManager Instance
{
get
{
if (_instance == null)
{
_instance = FindAnyObjectByType<InputManager>();
if (_instance == null)
{
var go = new GameObject("InputManager");
_instance = go.AddComponent<InputManager>();
DontDestroyOnLoad(go);
}
}
return _instance;
}
}
private PlayerInput playerInput;
private InputAction touchPressAction;
private InputAction touchPositionAction;
private ITouchInputConsumer defaultConsumer;
private bool isTouchHeld = false;
private bool lastFrameInteracted = false;
void Awake()
{
if (Instance != null && Instance != this)
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
_instance = this;
DontDestroyOnLoad(gameObject);
playerInput = GetComponent<PlayerInput>();
if (playerInput == null)
@@ -62,17 +81,20 @@ public class InputManager : MonoBehaviour
private void OnTouchPressStarted(InputAction.CallbackContext ctx)
{
// Touch started (finger down)
isTouchHeld = true;
Vector3 _screenPos = Camera.main.ScreenToWorldPoint(touchPositionAction.ReadValue<Vector2>());
Vector2 screenPos = new Vector2(_screenPos.x, _screenPos.y);
if (!TryDelegateToInteractable(screenPos))
lastFrameInteracted = TryDelegateToInteractable(screenPos);
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(screenPos);
isTouchHeld = true;
}
private void OnTouchPressCanceled(InputAction.CallbackContext ctx)
{
// Touch released (finger up)
isTouchHeld = false;
// Reset lastFrameInteracted for next frame
lastFrameInteracted = false;
// Optionally, you can notify consumers of release if needed
}
@@ -84,11 +106,8 @@ public class InputManager : MonoBehaviour
// Convert to world position
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
}
else
{
defaultConsumer?.OnTouchPosition(pos); // For drag or hover
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
}
}
@@ -100,7 +119,8 @@ public class InputManager : MonoBehaviour
Vector2 pos = touchPositionAction.ReadValue<Vector2>();
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D);
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(worldPos2D);
}
}

View File

@@ -10,20 +10,21 @@ public class ObjectiveStepBehaviour : MonoBehaviour
void Awake()
{
interactable = GetComponent<Interactable>();
}
void OnEnable()
{
if (interactable == null)
interactable = GetComponent<Interactable>();
if (interactable != null)
{
interactable.Interacted += OnInteracted;
}
// Register with PuzzleManager
PuzzleManager.Instance?.RegisterStepBehaviour(this);
}
void OnDestroy()
void OnDisable()
{
if (interactable != null)
{
interactable.Interacted -= OnInteracted;
}
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
}

View File

@@ -3,7 +3,25 @@ using System.Collections.Generic;
public class PuzzleManager : MonoBehaviour
{
public static PuzzleManager Instance { get; private set; }
private static PuzzleManager _instance;
public static PuzzleManager Instance
{
get
{
if (_instance == null)
{
_instance = FindAnyObjectByType<PuzzleManager>();
if (_instance == null)
{
var go = new GameObject("PuzzleManager");
_instance = go.AddComponent<PuzzleManager>();
DontDestroyOnLoad(go);
}
}
return _instance;
}
}
private HashSet<PuzzleStepSO> completedSteps = new HashSet<PuzzleStepSO>();
private HashSet<PuzzleStepSO> unlockedSteps = new HashSet<PuzzleStepSO>();
@@ -15,12 +33,12 @@ public class PuzzleManager : MonoBehaviour
void Awake()
{
if (Instance != null && Instance != this)
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
_instance = this;
DontDestroyOnLoad(gameObject);
}