Files
AppleHillsProduction/Assets/Scripts/PuzzleS/ObjectiveStepBehaviour.cs
tschesky f686f28cb8 Create a simple dialogue authoring system, tied into our items (#10)
- Editor dialogue graph
- Asset importer for processing the graph into runtime data
- DialogueComponent that steers the dialogue interactions
- DialogueCanbas with a scalable speech bubble to display everything
- Brief README overview of the system

Co-authored-by: AlexanderT <alexander@foolhardyhorizons.com>
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #10
2025-09-29 09:34:15 +00:00

103 lines
3.4 KiB
C#

using Input;
using Interactions;
using UnityEngine;
namespace PuzzleS
{
/// <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
{
/// <summary>
/// The data object representing this puzzle step.
/// </summary>
public PuzzleStepSO stepData;
private Interactable _interactable;
private bool _isUnlocked = false;
void Awake()
{
_interactable = GetComponent<Interactable>();
}
void OnEnable()
{
if (_interactable == null)
_interactable = GetComponent<Interactable>();
if (_interactable != null)
{
_interactable.interactionStarted.AddListener(OnInteractionStarted);
_interactable.interactionComplete.AddListener(OnInteractionComplete);
}
PuzzleManager.Instance?.RegisterStepBehaviour(this);
// Check if this step was already unlocked
if (stepData != null && PuzzleManager.Instance != null && PuzzleManager.Instance.IsStepUnlocked(stepData))
{
UnlockStep();
}
}
void OnDestroy()
{
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?.MarkPuzzleStepCompleted(stepData);
}
}
}
}