[Interaction] Zip up edge cases, base for interactions is now done

This commit is contained in:
Michal Pikulski
2025-09-04 15:01:28 +02:00
parent 05a8a5445f
commit 4215d4b437
4 changed files with 51 additions and 25 deletions

View File

@@ -3,7 +3,8 @@ using System;
public class Interactable : MonoBehaviour, ITouchInputConsumer
{
public event Action Interacted;
public event Action StartedInteraction;
public event Action<bool> InteractionComplete;
private ObjectiveStepBehaviour stepBehaviour;
@@ -15,14 +16,9 @@ public class Interactable : MonoBehaviour, ITouchInputConsumer
// Called by InputManager when this interactable is clicked/touched
public void OnTouchPress(Vector2 worldPosition)
{
if (stepBehaviour != null && !stepBehaviour.IsStepUnlocked())
{
DebugUIMessage.Show("Item is not unlocked yet");
Debug.Log("[Puzzles] Tried to interact with locked step: " + gameObject.name);
return;
}
// Defer lock check to follower arrival
Debug.Log($"[Interactable] OnTouchPress at {worldPosition} on {gameObject.name}");
Interacted?.Invoke();
StartedInteraction?.Invoke();
}
public void OnTouchPosition(Vector2 screenPosition)
@@ -33,10 +29,18 @@ public class Interactable : MonoBehaviour, ITouchInputConsumer
// Called when the follower arrives at this interactable
public bool OnFollowerArrived(FollowerController follower)
{
// Check if step is locked here
if (stepBehaviour != null && !stepBehaviour.IsStepUnlocked())
{
DebugUIMessage.Show("Item is not unlocked yet");
Debug.Log("[Puzzles] Tried to interact with locked step: " + gameObject.name);
InteractionComplete?.Invoke(false);
return false;
}
var requirements = GetComponents<InteractionRequirementBase>();
if (requirements.Length == 0)
{
// No requirements: allow default pickup
InteractionComplete?.Invoke(true);
return true;
}
bool anySuccess = false;
@@ -48,6 +52,7 @@ public class Interactable : MonoBehaviour, ITouchInputConsumer
break;
}
}
InteractionComplete?.Invoke(anySuccess);
if (!anySuccess)
{
Debug.Log($"[Interactable] No interaction requirements succeeded for {gameObject.name}");

View File

@@ -12,14 +12,12 @@ public class LevelSwitch : MonoBehaviour
void Awake()
{
_isActive = true;
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
interactable = GetComponent<Interactable>();
if (interactable != null)
{
interactable.Interacted += OnInteracted;
interactable.StartedInteraction += OnStartedInteraction;
}
ApplySwitchData();
}
@@ -28,7 +26,7 @@ public class LevelSwitch : MonoBehaviour
{
if (interactable != null)
{
interactable.Interacted -= OnInteracted;
interactable.StartedInteraction -= OnStartedInteraction;
}
}
@@ -52,7 +50,7 @@ public class LevelSwitch : MonoBehaviour
}
}
private async void OnInteracted()
private async void OnStartedInteraction()
{
Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
if (switchData != null && !string.IsNullOrEmpty(switchData.targetLevelSceneName) && _isActive)

View File

@@ -17,14 +17,22 @@ public class ObjectiveStepBehaviour : MonoBehaviour
if (interactable == null)
interactable = GetComponent<Interactable>();
if (interactable != null)
interactable.Interacted += OnInteracted;
{
interactable.StartedInteraction += OnStartedInteraction;
interactable.InteractionComplete += OnInteractionComplete;
// Removed old Interacted subscription
}
PuzzleManager.Instance?.RegisterStepBehaviour(this);
}
void OnDisable()
{
if (interactable != null)
interactable.Interacted -= OnInteracted;
{
interactable.StartedInteraction -= OnStartedInteraction;
interactable.InteractionComplete -= OnInteractionComplete;
// Removed old Interacted unsubscription
}
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
}
@@ -47,10 +55,18 @@ public class ObjectiveStepBehaviour : MonoBehaviour
return isUnlocked;
}
private void OnInteracted()
private void OnStartedInteraction()
{
// Optionally handle started interaction (e.g. visual feedback)
}
private void OnInteractionComplete(bool success)
{
if (!isUnlocked) return;
Debug.Log($"[Puzzles] Step interacted: {stepData?.stepId} on {gameObject.name}");
PuzzleManager.Instance?.OnStepCompleted(stepData);
if (success)
{
Debug.Log($"[Puzzles] Step interacted: {stepData?.stepId} on {gameObject.name}");
PuzzleManager.Instance?.OnStepCompleted(stepData);
}
}
}

View File

@@ -16,7 +16,8 @@ public class Pickup : MonoBehaviour
interactable = GetComponent<Interactable>();
if (interactable != null)
{
interactable.Interacted += OnInteracted;
interactable.StartedInteraction += OnStartedInteraction;
interactable.InteractionComplete += OnInteractionComplete;
}
ApplyItemData();
}
@@ -25,7 +26,8 @@ public class Pickup : MonoBehaviour
{
if (interactable != null)
{
interactable.Interacted -= OnInteracted;
interactable.StartedInteraction -= OnStartedInteraction;
interactable.InteractionComplete -= OnInteractionComplete;
}
}
@@ -67,7 +69,7 @@ public class Pickup : MonoBehaviour
}
}
private void OnInteracted()
private void OnStartedInteraction()
{
if (pickupInProgress) return;
var playerObj = GameObject.FindGameObjectWithTag("Player");
@@ -84,16 +86,14 @@ public class Pickup : MonoBehaviour
Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing.");
return;
}
// Use GameManager for playerStopDistance and followerPickupDelay
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
float followerPickupDelay = GameManager.Instance.FollowerPickupDelay;
// Subscribe to cancellation event
cachedPlayerController = playerController;
void OnPlayerArrived()
{
playerController.OnArrivedAtTarget -= OnPlayerArrived;
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
pickupInProgress = true; // Only lock when follower is about to be dispatched
pickupInProgress = true;
StartCoroutine(DispatchFollower());
}
void OnPlayerMoveCancelled()
@@ -138,4 +138,11 @@ public class Pickup : MonoBehaviour
playerController.MoveToAndNotify(stopPoint);
}
}
private void OnInteractionComplete(bool success)
{
// Only handle post-pickup logic here (e.g., feedback, item destruction)
if (!success) return;
// Optionally, add logic to disable the pickup or provide feedback
}
}