Revamp the puzzle registration system
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
using Input;
|
||||
using Interactions;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using AppleHills.Core.Settings;
|
||||
using Core;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace PuzzleS
|
||||
{
|
||||
@@ -37,23 +34,46 @@ namespace PuzzleS
|
||||
void Awake()
|
||||
{
|
||||
_interactable = GetComponent<Interactable>();
|
||||
|
||||
// Initialize the indicator if it exists, but ensure it's hidden initially
|
||||
if (puzzleIndicator != null)
|
||||
{
|
||||
// The indicator should start inactive until we determine its proper state
|
||||
puzzleIndicator.SetActive(false);
|
||||
|
||||
// Get the IPuzzlePrompt component
|
||||
_indicator = puzzleIndicator.GetComponent<IPuzzlePrompt>();
|
||||
|
||||
if (_indicator == null)
|
||||
{
|
||||
// Try to find it in children if not on the root
|
||||
_indicator = puzzleIndicator.GetComponentInChildren<IPuzzlePrompt>();
|
||||
}
|
||||
|
||||
if (_indicator == null)
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Indicator prefab for {stepData?.stepId} does not implement IPuzzlePrompt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (_interactable == null)
|
||||
_interactable = GetComponent<Interactable>();
|
||||
|
||||
if (_interactable != null)
|
||||
{
|
||||
_interactable.interactionStarted.AddListener(OnInteractionStarted);
|
||||
_interactable.interactionComplete.AddListener(OnInteractionComplete);
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Register with PuzzleManager, regardless of enabled/disabled state
|
||||
// PuzzleManager will call UnlockStep or LockStep based on current puzzle state
|
||||
PuzzleManager.Instance?.RegisterStepBehaviour(this);
|
||||
// Check if this step was already unlocked
|
||||
if (stepData != null && PuzzleManager.Instance != null && PuzzleManager.Instance.IsStepUnlocked(stepData))
|
||||
{
|
||||
UnlockStep();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
@@ -73,7 +93,7 @@ namespace PuzzleS
|
||||
public void UpdateProximityState(ProximityState newState)
|
||||
{
|
||||
if (_currentProximityState == newState) return;
|
||||
if (_indicator == null) return;
|
||||
if (!_isUnlocked) return; // Don't process state changes if locked
|
||||
|
||||
// Determine state changes and call appropriate methods
|
||||
if (newState == ProximityState.Close)
|
||||
@@ -97,6 +117,9 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public virtual void OnShow()
|
||||
{
|
||||
if (puzzleIndicator != null)
|
||||
puzzleIndicator.SetActive(true);
|
||||
|
||||
// Delegate to indicator if available
|
||||
if (IsIndicatorValid())
|
||||
{
|
||||
@@ -104,7 +127,6 @@ namespace PuzzleS
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Prompt shown for {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
@@ -113,14 +135,15 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public virtual void OnHide()
|
||||
{
|
||||
if (puzzleIndicator != null)
|
||||
puzzleIndicator.SetActive(false);
|
||||
|
||||
// Delegate to indicator if available
|
||||
if (IsIndicatorValid())
|
||||
{
|
||||
_indicator.OnHide();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Prompt hidden for {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
@@ -138,9 +161,6 @@ namespace PuzzleS
|
||||
_indicator.ShowFar();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Player entered far range of {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -157,9 +177,6 @@ namespace PuzzleS
|
||||
_indicator.ShowClose();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Player entered close range of {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -176,9 +193,6 @@ namespace PuzzleS
|
||||
_indicator.HideClose();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Player exited close range of {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -195,9 +209,6 @@ namespace PuzzleS
|
||||
_indicator.HideFar();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Player exited far range of {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -205,57 +216,42 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public void UnlockStep()
|
||||
{
|
||||
if (_isUnlocked) return;
|
||||
|
||||
_isUnlocked = true;
|
||||
Logging.Debug($"[Puzzles] Step unlocked: {stepData?.stepId} on {gameObject.name}");
|
||||
|
||||
// Show indicator if enabled in settings
|
||||
if (puzzleIndicator != null)
|
||||
// Make the indicator visible since this step is now unlocked
|
||||
OnShow();
|
||||
|
||||
if (IsIndicatorValid())
|
||||
{
|
||||
// Try to get the IPuzzlePrompt component from the spawned indicator
|
||||
_indicator = puzzleIndicator.GetComponent<IPuzzlePrompt>();
|
||||
|
||||
if (_indicator == null)
|
||||
// Set the correct state based on current player distance
|
||||
Transform playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
||||
if (playerTransform != null)
|
||||
{
|
||||
// Try to find it in children if not on the root
|
||||
_indicator = puzzleIndicator.GetComponentInChildren<IPuzzlePrompt>();
|
||||
}
|
||||
|
||||
if (_indicator == null)
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Indicator prefab for {stepData?.stepId} does not implement IPuzzlePrompt");
|
||||
}
|
||||
else
|
||||
{
|
||||
// First show the indicator
|
||||
_indicator.OnShow();
|
||||
float distance = Vector3.Distance(transform.position, playerTransform.position);
|
||||
float promptRange = AppleHills.SettingsAccess.GetPuzzlePromptRange();
|
||||
|
||||
// Then set the correct state based on current player distance
|
||||
Transform playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
||||
if (playerTransform != null)
|
||||
if (distance <= promptRange)
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, playerTransform.position);
|
||||
float promptRange = AppleHills.SettingsAccess.GetPuzzlePromptRange();
|
||||
|
||||
if (distance <= promptRange)
|
||||
{
|
||||
// Player is in close range
|
||||
_currentProximityState = ProximityState.Close;
|
||||
_indicator.ShowClose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Player is in far range
|
||||
_currentProximityState = ProximityState.Far;
|
||||
_indicator.ShowFar();
|
||||
}
|
||||
// Player is in close range
|
||||
_currentProximityState = ProximityState.Close;
|
||||
_indicator.ShowClose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to far if player not found
|
||||
// Player is in far range
|
||||
_currentProximityState = ProximityState.Far;
|
||||
_indicator.ShowFar();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to far if player not found
|
||||
_currentProximityState = ProximityState.Far;
|
||||
_indicator.ShowFar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,14 +260,18 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public void LockStep()
|
||||
{
|
||||
if (!_isUnlocked && puzzleIndicator != null)
|
||||
{
|
||||
// Make sure indicator is hidden if we're already locked
|
||||
puzzleIndicator.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
_isUnlocked = false;
|
||||
Logging.Debug($"[Puzzles] Step locked: {stepData?.stepId} on {gameObject.name}");
|
||||
|
||||
// Hide indicator
|
||||
if (IsIndicatorValid())
|
||||
{
|
||||
_indicator.OnHide();
|
||||
}
|
||||
// Hide the indicator
|
||||
OnHide();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -287,7 +287,7 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
private void OnInteractionStarted(PlayerTouchController playerRef, FollowerController followerRef)
|
||||
{
|
||||
// Optionally handle started interaction (e.g. visual feedback)
|
||||
// Empty - handled by Interactable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -297,12 +297,17 @@ namespace PuzzleS
|
||||
private void OnInteractionComplete(bool success)
|
||||
{
|
||||
if (!_isUnlocked) return;
|
||||
if (success)
|
||||
if (success && !_isCompleted)
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Step interacted: {stepData?.stepId} on {gameObject.name}");
|
||||
_isCompleted = true;
|
||||
PuzzleManager.Instance?.MarkPuzzleStepCompleted(stepData);
|
||||
Destroy(puzzleIndicator);
|
||||
|
||||
if (puzzleIndicator != null)
|
||||
{
|
||||
Destroy(puzzleIndicator);
|
||||
_indicator = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,7 +328,7 @@ namespace PuzzleS
|
||||
|
||||
// Draw threshold circle
|
||||
Gizmos.color = Color.cyan;
|
||||
Gizmos.DrawWireSphere(transform.position, promptRange / 2f);
|
||||
Gizmos.DrawWireSphere(transform.position, promptRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user