Rework interactables into a flatter hierarchy, reenable puzzles as well

This commit is contained in:
Michal Pikulski
2025-09-11 13:00:26 +02:00
parent 3a40d1a151
commit e1ff13db30
32 changed files with 981 additions and 1018 deletions

View File

@@ -1,93 +1,97 @@

using Input;
using Interactions;
using UnityEngine;
/// <summary>
/// Manages the state and interactions for a single puzzle step, including unlock/lock logic and event handling.
/// </summary>
[RequireComponent(typeof(Interactable))]
public class ObjectiveStepBehaviour : MonoBehaviour
namespace PuzzleS
{
/// <summary>
/// The data object representing this puzzle step.
/// Manages the state and interactions for a single puzzle step, including unlock/lock logic and event handling.
/// </summary>
public PuzzleStepSO stepData;
private Interactable interactable;
private bool isUnlocked = false;
void Awake()
[RequireComponent(typeof(Interactable))]
public class ObjectiveStepBehaviour : MonoBehaviour
{
interactable = GetComponent<Interactable>();
}
/// <summary>
/// The data object representing this puzzle step.
/// </summary>
public PuzzleStepSO stepData;
private Interactable _interactable;
private bool _isUnlocked = false;
void OnEnable()
{
if (interactable == null)
interactable = GetComponent<Interactable>();
if (interactable != null)
void Awake()
{
interactable.StartedInteraction += OnStartedInteraction;
interactable.InteractionComplete += OnInteractionComplete;
_interactable = GetComponent<Interactable>();
}
PuzzleManager.Instance?.RegisterStepBehaviour(this);
}
void OnDisable()
{
if (interactable != null)
void OnEnable()
{
interactable.StartedInteraction -= OnStartedInteraction;
interactable.InteractionComplete -= OnInteractionComplete;
if (_interactable == null)
_interactable = GetComponent<Interactable>();
if (_interactable != null)
{
_interactable.interactionStarted.AddListener(OnInteractionStarted);
_interactable.interactionComplete.AddListener(OnInteractionComplete);
}
PuzzleManager.Instance?.RegisterStepBehaviour(this);
}
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
}
/// <summary>
/// Unlocks this puzzle step, allowing interaction.
/// </summary>
public void UnlockStep()
{
isUnlocked = true;
Debug.Log($"[Puzzles] Step unlocked: {stepData?.stepId} on {gameObject.name}");
// Optionally, show visual feedback for unlocked state
}
/// <summary>
/// Locks this puzzle step, preventing interaction.
/// </summary>
public void LockStep()
{
isUnlocked = false;
Debug.Log($"[Puzzles] Step locked: {stepData?.stepId} on {gameObject.name}");
// Optionally, show visual feedback for locked state
}
/// <summary>
/// Returns whether this step is currently unlocked.
/// </summary>
public bool IsStepUnlocked()
{
return isUnlocked;
}
/// <summary>
/// Handles the start of an interaction (can be used for visual feedback).
/// </summary>
private void OnStartedInteraction()
{
// Optionally handle started interaction (e.g. visual feedback)
}
/// <summary>
/// Handles completion of the interaction, notifies PuzzleManager if successful and unlocked.
/// </summary>
/// <param name="success">Whether the interaction was successful.</param>
private void OnInteractionComplete(bool success)
{
if (!isUnlocked) return;
if (success)
void OnDestroy()
{
Debug.Log($"[Puzzles] Step interacted: {stepData?.stepId} on {gameObject.name}");
PuzzleManager.Instance?.OnStepCompleted(stepData);
if (_interactable != null)
{
_interactable.interactionStarted.RemoveListener(OnInteractionStarted);
_interactable.interactionComplete.RemoveListener(OnInteractionComplete);
}
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
}
/// <summary>
/// Unlocks this puzzle step, allowing interaction.
/// </summary>
public void UnlockStep()
{
_isUnlocked = true;
Debug.Log($"[Puzzles] Step unlocked: {stepData?.stepId} on {gameObject.name}");
// Optionally, show visual feedback for unlocked state
}
/// <summary>
/// Locks this puzzle step, preventing interaction.
/// </summary>
public void LockStep()
{
_isUnlocked = false;
Debug.Log($"[Puzzles] Step locked: {stepData?.stepId} on {gameObject.name}");
// Optionally, show visual feedback for locked state
}
/// <summary>
/// Returns whether this step is currently unlocked.
/// </summary>
public bool IsStepUnlocked()
{
return _isUnlocked;
}
/// <summary>
/// Handles the start of an interaction (can be used for visual feedback).
/// </summary>
private void OnInteractionStarted(PlayerTouchController playerRef, FollowerController followerRef)
{
// Optionally handle started interaction (e.g. visual feedback)
}
/// <summary>
/// Handles completion of the interaction, notifies PuzzleManager if successful and unlocked.
/// </summary>
/// <param name="success">Whether the interaction was successful.</param>
private void OnInteractionComplete(bool success)
{
if (!_isUnlocked) return;
if (success)
{
Debug.Log($"[Puzzles] Step interacted: {stepData?.stepId} on {gameObject.name}");
PuzzleManager.Instance?.OnStepCompleted(stepData);
}
}
}
}

View File

@@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections.Generic;
using PuzzleS;
/// <summary>
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.