Revamp the prompt system, the bootstrapper system, the starting cinematic
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,22 +34,52 @@ 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);
|
||||
}
|
||||
PuzzleManager.Instance?.RegisterStepBehaviour(this);
|
||||
// Check if this step was already unlocked
|
||||
if (stepData != null && PuzzleManager.Instance != null && PuzzleManager.Instance.IsStepUnlocked(stepData))
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Simply register with the PuzzleManager
|
||||
// The manager will handle state updates appropriately based on whether data is loaded
|
||||
if (stepData != null && PuzzleManager.Instance != null)
|
||||
{
|
||||
UnlockStep();
|
||||
PuzzleManager.Instance.RegisterStepBehaviour(this);
|
||||
}
|
||||
else if (stepData == null)
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Cannot register step on {gameObject.name}: stepData is null");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +90,11 @@ namespace PuzzleS
|
||||
_interactable.interactionStarted.RemoveListener(OnInteractionStarted);
|
||||
_interactable.interactionComplete.RemoveListener(OnInteractionComplete);
|
||||
}
|
||||
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
|
||||
|
||||
if (PuzzleManager.Instance != null && stepData != null)
|
||||
{
|
||||
PuzzleManager.Instance.UnregisterStepBehaviour(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -73,7 +104,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 +128,9 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public virtual void OnShow()
|
||||
{
|
||||
if (puzzleIndicator != null)
|
||||
puzzleIndicator.SetActive(true);
|
||||
|
||||
// Delegate to indicator if available
|
||||
if (IsIndicatorValid())
|
||||
{
|
||||
@@ -104,7 +138,6 @@ namespace PuzzleS
|
||||
return;
|
||||
}
|
||||
|
||||
// Default fallback behavior
|
||||
Logging.Debug($"[Puzzles] Prompt shown for {stepData?.stepId} on {gameObject.name}");
|
||||
}
|
||||
|
||||
@@ -113,14 +146,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 +172,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 +188,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 +204,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 +220,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 +227,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 +271,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 +298,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 +308,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 +339,7 @@ namespace PuzzleS
|
||||
|
||||
// Draw threshold circle
|
||||
Gizmos.color = Color.cyan;
|
||||
Gizmos.DrawWireSphere(transform.position, promptRange / 2f);
|
||||
Gizmos.DrawWireSphere(transform.position, promptRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
103
Assets/Scripts/PuzzleS/PuzzleChainSO.cs
Normal file
103
Assets/Scripts/PuzzleS/PuzzleChainSO.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace PuzzleS
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a complete chain of puzzle steps that form a logical sequence.
|
||||
/// This is automatically generated from folder structure during asset import.
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "PuzzleChain", menuName = "AppleHills/Items & Puzzles/PuzzleChain")]
|
||||
public class PuzzleChainSO : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Unique identifier for this puzzle chain, automatically set to match folder name
|
||||
/// </summary>
|
||||
public string chainId;
|
||||
|
||||
/// <summary>
|
||||
/// Display name for this chain
|
||||
/// </summary>
|
||||
public string displayName;
|
||||
|
||||
/// <summary>
|
||||
/// Description of this puzzle chain
|
||||
/// </summary>
|
||||
[TextArea]
|
||||
public string description;
|
||||
|
||||
/// <summary>
|
||||
/// All steps that belong to this puzzle chain
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> allSteps = new List<PuzzleStepSO>();
|
||||
|
||||
/// <summary>
|
||||
/// Initial steps that should be unlocked when the puzzle chain starts
|
||||
/// (steps with no dependencies)
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> initialSteps = new List<PuzzleStepSO>();
|
||||
|
||||
/// <summary>
|
||||
/// Optional requirement for this entire chain to be activated
|
||||
/// If not null, this chain requires the specified chain to be completed first
|
||||
/// </summary>
|
||||
public PuzzleChainSO requiredChain;
|
||||
|
||||
/// <summary>
|
||||
/// Pre-processed dependency data built at edit time.
|
||||
/// Maps step IDs to arrays of dependency step IDs
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public Dictionary<string, string[]> stepDependencies = new Dictionary<string, string[]>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets all steps that will be unlocked by completing the given step
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> GetUnlockedSteps(string stepId)
|
||||
{
|
||||
var result = new List<PuzzleStepSO>();
|
||||
foreach (var step in allSteps)
|
||||
{
|
||||
if (step.stepId == stepId && step != null)
|
||||
{
|
||||
return step.unlocks;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all steps that will be unlocked by completing the given step
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> GetUnlockedSteps(PuzzleStepSO completedStep)
|
||||
{
|
||||
return completedStep != null ? completedStep.unlocks : new List<PuzzleStepSO>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if this step is an initial step (no dependencies)
|
||||
/// </summary>
|
||||
public bool IsInitialStep(PuzzleStepSO step)
|
||||
{
|
||||
return step != null && initialSteps.Contains(step);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if all steps in this chain are completed
|
||||
/// </summary>
|
||||
public bool IsChainComplete(HashSet<PuzzleStepSO> completedSteps)
|
||||
{
|
||||
if (completedSteps == null) return false;
|
||||
|
||||
foreach (var step in allSteps)
|
||||
{
|
||||
if (step != null && !completedSteps.Contains(step))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/PuzzleS/PuzzleChainSO.cs.meta
Normal file
3
Assets/Scripts/PuzzleS/PuzzleChainSO.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58109a40325e47f2a8a3b9264d8938dd
|
||||
timeCreated: 1760532067
|
||||
73
Assets/Scripts/PuzzleS/PuzzleLevelDataSO.cs
Normal file
73
Assets/Scripts/PuzzleS/PuzzleLevelDataSO.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace PuzzleS
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents all puzzle steps in a level.
|
||||
/// This is automatically generated from folder structure during asset import.
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "LevelPuzzleData", menuName = "AppleHills/Items & Puzzles/LevelPuzzleData")]
|
||||
public class PuzzleLevelDataSO : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Unique identifier for this level, automatically set to match folder name
|
||||
/// </summary>
|
||||
public string levelId;
|
||||
|
||||
/// <summary>
|
||||
/// Display name for this level
|
||||
/// </summary>
|
||||
public string displayName;
|
||||
|
||||
/// <summary>
|
||||
/// All puzzle steps in this level
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> allSteps = new List<PuzzleStepSO>();
|
||||
|
||||
/// <summary>
|
||||
/// Steps that should be unlocked at level start (no dependencies)
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> initialSteps = new List<PuzzleStepSO>();
|
||||
|
||||
/// <summary>
|
||||
/// Pre-processed dependency data built at edit time.
|
||||
/// Maps step IDs to arrays of dependency step IDs (which steps are required by each step)
|
||||
/// </summary>
|
||||
public Dictionary<string, string[]> stepDependencies = new Dictionary<string, string[]>();
|
||||
|
||||
/// <summary>
|
||||
/// Check if all steps in the level are complete
|
||||
/// </summary>
|
||||
public bool IsLevelComplete(HashSet<PuzzleStepSO> completedSteps)
|
||||
{
|
||||
if (completedSteps == null) return false;
|
||||
|
||||
foreach (var step in allSteps)
|
||||
{
|
||||
if (step != null && !completedSteps.Contains(step))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all steps that will be unlocked by completing the given step
|
||||
/// </summary>
|
||||
public List<PuzzleStepSO> GetUnlockedSteps(PuzzleStepSO completedStep)
|
||||
{
|
||||
return completedStep != null ? completedStep.unlocks : new List<PuzzleStepSO>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if this step is an initial step (no dependencies)
|
||||
/// </summary>
|
||||
public bool IsInitialStep(PuzzleStepSO step)
|
||||
{
|
||||
return step != null && initialSteps.Contains(step);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/PuzzleS/PuzzleLevelDataSO.cs.meta
Normal file
3
Assets/Scripts/PuzzleS/PuzzleLevelDataSO.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a79780a5a0d498084afd737d4515e3b
|
||||
timeCreated: 1760532084
|
||||
@@ -5,7 +5,11 @@ using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using AppleHills.Core.Settings;
|
||||
using Core; // Added for IInteractionSettings
|
||||
using Bootstrap;
|
||||
using Core;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using Utils;
|
||||
|
||||
namespace PuzzleS
|
||||
{
|
||||
@@ -26,71 +30,103 @@ namespace PuzzleS
|
||||
// Settings reference
|
||||
private IInteractionSettings _interactionSettings;
|
||||
|
||||
// Current level puzzle data
|
||||
private PuzzleLevelDataSO _currentLevelData;
|
||||
private AsyncOperationHandle<PuzzleLevelDataSO> _levelDataLoadOperation;
|
||||
private bool _isDataLoaded = false;
|
||||
|
||||
// Store registered behaviors that are waiting for data to be loaded
|
||||
private List<ObjectiveStepBehaviour> _registeredBehaviours = new List<ObjectiveStepBehaviour>();
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the PuzzleManager.
|
||||
/// </summary>
|
||||
public static PuzzleManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null && Application.isPlaying && !_isQuitting)
|
||||
{
|
||||
_instance = FindAnyObjectByType<PuzzleManager>();
|
||||
if (_instance == null)
|
||||
{
|
||||
var go = new GameObject("PuzzleManager");
|
||||
_instance = go.AddComponent<PuzzleManager>();
|
||||
// DontDestroyOnLoad(go);
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
public static PuzzleManager Instance => _instance;
|
||||
|
||||
// Events to notify about step lifecycle
|
||||
public event Action<PuzzleStepSO> OnStepCompleted;
|
||||
public event Action<PuzzleStepSO> OnStepUnlocked;
|
||||
public event Action<PuzzleLevelDataSO> OnLevelDataLoaded;
|
||||
public event Action<PuzzleLevelDataSO> OnAllPuzzlesComplete;
|
||||
|
||||
private HashSet<PuzzleStepSO> _completedSteps = new HashSet<PuzzleStepSO>();
|
||||
private HashSet<PuzzleStepSO> _unlockedSteps = new HashSet<PuzzleStepSO>();
|
||||
// Registration for ObjectiveStepBehaviour
|
||||
private Dictionary<PuzzleStepSO, ObjectiveStepBehaviour> _stepBehaviours = new Dictionary<PuzzleStepSO, ObjectiveStepBehaviour>();
|
||||
// Runtime dependency graph
|
||||
private Dictionary<PuzzleStepSO, List<PuzzleStepSO>> _runtimeDependencies = new Dictionary<PuzzleStepSO, List<PuzzleStepSO>>();
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
// DontDestroyOnLoad(gameObject);
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
|
||||
// Initialize settings reference
|
||||
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
||||
|
||||
// Register for post-boot initialization
|
||||
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
||||
}
|
||||
|
||||
void Start()
|
||||
|
||||
private void InitializePostBoot()
|
||||
{
|
||||
// Subscribe to SceneManagerService events after boot is complete
|
||||
SceneManagerService.Instance.SceneLoadCompleted += OnSceneLoadCompleted;
|
||||
SceneManagerService.Instance.SceneLoadStarted += OnSceneLoadStarted;
|
||||
|
||||
// Find player transform
|
||||
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
||||
|
||||
// Start proximity check coroutine
|
||||
StartProximityChecks();
|
||||
|
||||
// Load puzzle data for the current scene if not already loading
|
||||
if (_currentLevelData == null && !_isDataLoaded)
|
||||
{
|
||||
LoadPuzzleDataForCurrentScene();
|
||||
}
|
||||
|
||||
Logging.Debug("[PuzzleManager] Subscribed to SceneManagerService events");
|
||||
}
|
||||
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
StopProximityChecks();
|
||||
|
||||
// Unsubscribe from scene manager events
|
||||
if (SceneManagerService.Instance != null)
|
||||
{
|
||||
SceneManagerService.Instance.SceneLoadCompleted -= OnSceneLoadCompleted;
|
||||
SceneManagerService.Instance.SceneLoadStarted -= OnSceneLoadStarted;
|
||||
}
|
||||
|
||||
// Release addressable handle if needed
|
||||
if (_levelDataLoadOperation.IsValid())
|
||||
{
|
||||
Addressables.Release(_levelDataLoadOperation);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
/// <summary>
|
||||
/// Called when a scene is starting to load
|
||||
/// </summary>
|
||||
public void OnSceneLoadStarted(string sceneName)
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
// Reset data loaded state when changing scenes to avoid using stale data
|
||||
_isDataLoaded = false;
|
||||
Logging.Debug($"[Puzzles] Scene load started: {sceneName}, marked puzzle data as not loaded");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a scene is loaded
|
||||
/// </summary>
|
||||
public void OnSceneLoadCompleted(string sceneName)
|
||||
{
|
||||
// Skip for non-gameplay scenes
|
||||
if (sceneName == "BootstrapScene" || string.IsNullOrEmpty(sceneName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Logging.Debug("[MDPI] OnSceneLoaded");
|
||||
_runtimeDependencies.Clear();
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
Logging.Debug($"[Puzzles] Scene loaded: {sceneName}, loading puzzle data");
|
||||
LoadPuzzleDataForCurrentScene(sceneName);
|
||||
|
||||
// Find player transform again in case it changed with scene load
|
||||
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
||||
@@ -99,6 +135,72 @@ namespace PuzzleS
|
||||
StartProximityChecks();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load puzzle data for the current scene
|
||||
/// </summary>
|
||||
private void LoadPuzzleDataForCurrentScene(string sceneName = null)
|
||||
{
|
||||
string currentScene = sceneName ?? SceneManagerService.Instance.CurrentGameplayScene;
|
||||
if (string.IsNullOrEmpty(currentScene))
|
||||
{
|
||||
Logging.Warning("[Puzzles] Cannot load puzzle data: Current scene name is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
_isDataLoaded = false;
|
||||
string addressablePath = $"Puzzles/{currentScene}";
|
||||
|
||||
Logging.Debug($"[Puzzles] Loading puzzle data from addressable: {addressablePath}");
|
||||
|
||||
// Release previous handle if needed
|
||||
if (_levelDataLoadOperation.IsValid())
|
||||
{
|
||||
Addressables.Release(_levelDataLoadOperation);
|
||||
}
|
||||
|
||||
// Check if the addressable exists before trying to load it
|
||||
if (!AppleHillsUtils.AddressableKeyExists(addressablePath))
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Puzzle data does not exist for scene: {currentScene}");
|
||||
_isDataLoaded = true; // Mark as loaded but with no data
|
||||
_currentLevelData = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Load the level data asset
|
||||
_levelDataLoadOperation = Addressables.LoadAssetAsync<PuzzleLevelDataSO>(addressablePath);
|
||||
_levelDataLoadOperation.Completed += handle =>
|
||||
{
|
||||
if (handle.Status == AsyncOperationStatus.Succeeded)
|
||||
{
|
||||
_currentLevelData = handle.Result;
|
||||
Logging.Debug($"[Puzzles] Loaded level data: {_currentLevelData.levelId} with {_currentLevelData.allSteps.Count} steps");
|
||||
|
||||
// Reset state
|
||||
_completedSteps.Clear();
|
||||
_unlockedSteps.Clear();
|
||||
|
||||
// Unlock initial steps
|
||||
UnlockInitialSteps();
|
||||
|
||||
// Update all registered behaviors now that data is loaded
|
||||
UpdateAllRegisteredBehaviors();
|
||||
|
||||
// Mark data as loaded
|
||||
_isDataLoaded = true;
|
||||
|
||||
// Notify listeners
|
||||
OnLevelDataLoaded?.Invoke(_currentLevelData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Failed to load puzzle data for {currentScene}: {handle.OperationException?.Message}");
|
||||
_isDataLoaded = true; // Mark as loaded but with error
|
||||
_currentLevelData = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start the proximity check coroutine.
|
||||
/// </summary>
|
||||
@@ -138,7 +240,7 @@ namespace PuzzleS
|
||||
foreach (var kvp in _stepBehaviours)
|
||||
{
|
||||
if (kvp.Value == null) continue;
|
||||
if (IsPuzzleStepCompleted(kvp.Value.stepData.stepId)) continue;
|
||||
if (IsPuzzleStepCompleted(kvp.Key.stepId)) continue;
|
||||
|
||||
float distance = Vector3.Distance(_playerTransform.position, kvp.Value.transform.position);
|
||||
|
||||
@@ -157,6 +259,24 @@ namespace PuzzleS
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update all registered behaviors with their current state
|
||||
/// </summary>
|
||||
private void UpdateAllRegisteredBehaviors()
|
||||
{
|
||||
foreach (var behaviour in _registeredBehaviours)
|
||||
{
|
||||
if (behaviour == null) continue;
|
||||
|
||||
// Only update if the step is in our dictionary
|
||||
bool stepUnlocked = IsStepUnlocked(behaviour.stepData);
|
||||
if (stepUnlocked)
|
||||
{
|
||||
UpdateStepState(behaviour);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a step behaviour with the manager.
|
||||
/// </summary>
|
||||
@@ -164,21 +284,51 @@ namespace PuzzleS
|
||||
public void RegisterStepBehaviour(ObjectiveStepBehaviour behaviour)
|
||||
{
|
||||
if (behaviour?.stepData == null) return;
|
||||
|
||||
// Always add to our registered behaviors list
|
||||
if (!_registeredBehaviours.Contains(behaviour))
|
||||
{
|
||||
_registeredBehaviours.Add(behaviour);
|
||||
}
|
||||
|
||||
// Add to the step behaviours dictionary if not already there
|
||||
if (!_stepBehaviours.ContainsKey(behaviour.stepData))
|
||||
{
|
||||
_stepBehaviours.Add(behaviour.stepData, behaviour);
|
||||
_runtimeDependencies.Clear();
|
||||
foreach (var step in _stepBehaviours.Values)
|
||||
{
|
||||
step.LockStep();
|
||||
}
|
||||
_unlockedSteps.Clear();
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
Logging.Debug($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
||||
|
||||
// Only update state if data is already loaded
|
||||
if (_isDataLoaded && _currentLevelData != null)
|
||||
{
|
||||
UpdateStepState(behaviour);
|
||||
}
|
||||
// Otherwise, the state will be updated when data loads in UpdateAllRegisteredBehaviors
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates a step's state based on the current puzzle state.
|
||||
/// </summary>
|
||||
private void UpdateStepState(ObjectiveStepBehaviour behaviour)
|
||||
{
|
||||
if (behaviour?.stepData == null) return;
|
||||
|
||||
// If step is already completed, ignore
|
||||
if (_completedSteps.Contains(behaviour.stepData))
|
||||
return;
|
||||
|
||||
// If step is already unlocked, update the behaviour
|
||||
if (_unlockedSteps.Contains(behaviour.stepData))
|
||||
{
|
||||
behaviour.UnlockStep();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make sure it's locked
|
||||
behaviour.LockStep();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a step behaviour from the manager.
|
||||
/// </summary>
|
||||
@@ -186,57 +336,27 @@ namespace PuzzleS
|
||||
public void UnregisterStepBehaviour(ObjectiveStepBehaviour behaviour)
|
||||
{
|
||||
if (behaviour?.stepData == null) return;
|
||||
|
||||
_stepBehaviours.Remove(behaviour.stepData);
|
||||
_registeredBehaviours.Remove(behaviour);
|
||||
|
||||
Logging.Debug($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the runtime dependency graph for all registered steps.
|
||||
/// </summary>
|
||||
private void BuildRuntimeDependencies()
|
||||
{
|
||||
_runtimeDependencies = PuzzleGraphUtility.BuildDependencyGraph(_stepBehaviours.Keys);
|
||||
foreach (var step in _runtimeDependencies.Keys)
|
||||
{
|
||||
foreach (var dep in _runtimeDependencies[step])
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Step {step.stepId} depends on {dep.stepId}");
|
||||
}
|
||||
}
|
||||
Logging.Debug($"[Puzzles] Runtime dependencies built. Total steps: {_stepBehaviours.Count}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unlocks all initial steps (those with no dependencies) and any steps whose dependencies are already met.
|
||||
/// Unlocks all initial steps (those with no dependencies)
|
||||
/// </summary>
|
||||
private void UnlockInitialSteps()
|
||||
{
|
||||
// First, unlock all steps with no dependencies (initial steps)
|
||||
var initialSteps = PuzzleGraphUtility.FindInitialSteps(_runtimeDependencies);
|
||||
foreach (var step in initialSteps)
|
||||
if (_currentLevelData == null) return;
|
||||
|
||||
// Unlock initial steps
|
||||
foreach (var step in _currentLevelData.initialSteps)
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Initial step unlocked: {step.stepId}");
|
||||
UnlockStep(step);
|
||||
}
|
||||
|
||||
// Keep trying to unlock steps as long as we're making progress
|
||||
bool madeProgress;
|
||||
do
|
||||
{
|
||||
madeProgress = false;
|
||||
|
||||
// Check all steps that haven't been unlocked yet
|
||||
foreach (var step in _runtimeDependencies.Keys.Where(s => !_unlockedSteps.Contains(s)))
|
||||
{
|
||||
// Check if all dependencies have been completed
|
||||
if (AreRuntimeDependenciesMet(step))
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Chain step unlocked: {step.stepId}");
|
||||
UnlockStep(step);
|
||||
madeProgress = true;
|
||||
}
|
||||
}
|
||||
} while (madeProgress);
|
||||
Logging.Debug($"[Puzzles] Unlocked {_unlockedSteps.Count} initial steps");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -246,24 +366,29 @@ namespace PuzzleS
|
||||
public void MarkPuzzleStepCompleted(PuzzleStepSO step)
|
||||
{
|
||||
if (_completedSteps.Contains(step)) return;
|
||||
if (_currentLevelData == null) return;
|
||||
|
||||
_completedSteps.Add(step);
|
||||
Logging.Debug($"[Puzzles] Step completed: {step.stepId}");
|
||||
|
||||
// Broadcast completion
|
||||
OnStepCompleted?.Invoke(step);
|
||||
|
||||
foreach (var unlock in step.unlocks)
|
||||
// Unlock steps that are unlocked by this step
|
||||
foreach (var unlockStep in _currentLevelData.GetUnlockedSteps(step))
|
||||
{
|
||||
if (AreRuntimeDependenciesMet(unlock))
|
||||
if (AreStepDependenciesMet(unlockStep))
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Unlocking step {unlock.stepId} after completing {step.stepId}");
|
||||
UnlockStep(unlock);
|
||||
Logging.Debug($"[Puzzles] Unlocking step {unlockStep.stepId} after completing {step.stepId}");
|
||||
UnlockStep(unlockStep);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Debug($"[Puzzles] Step {unlock.stepId} not unlocked yet, waiting for other dependencies");
|
||||
Logging.Debug($"[Puzzles] Step {unlockStep.stepId} not unlocked yet, waiting for other dependencies");
|
||||
}
|
||||
}
|
||||
|
||||
// Check if all puzzle steps are now complete
|
||||
CheckPuzzleCompletion();
|
||||
}
|
||||
|
||||
@@ -272,13 +397,33 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
/// <param name="step">The step to check.</param>
|
||||
/// <returns>True if all dependencies are met, false otherwise.</returns>
|
||||
private bool AreRuntimeDependenciesMet(PuzzleStepSO step)
|
||||
private bool AreStepDependenciesMet(PuzzleStepSO step)
|
||||
{
|
||||
if (!_runtimeDependencies.ContainsKey(step) || _runtimeDependencies[step].Count == 0) return true;
|
||||
foreach (var dep in _runtimeDependencies[step])
|
||||
if (_currentLevelData == null || step == null) return false;
|
||||
|
||||
// If it's an initial step, it has no dependencies
|
||||
if (_currentLevelData.IsInitialStep(step)) return true;
|
||||
|
||||
// Check if dependencies are met using pre-processed data
|
||||
if (_currentLevelData.stepDependencies.TryGetValue(step.stepId, out string[] dependencies))
|
||||
{
|
||||
if (!_completedSteps.Contains(dep)) return false;
|
||||
foreach (var depId in dependencies)
|
||||
{
|
||||
// Find the dependency step
|
||||
bool dependencyMet = false;
|
||||
foreach (var completedStep in _completedSteps)
|
||||
{
|
||||
if (completedStep.stepId == depId)
|
||||
{
|
||||
dependencyMet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dependencyMet) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -290,6 +435,7 @@ namespace PuzzleS
|
||||
{
|
||||
if (_unlockedSteps.Contains(step)) return;
|
||||
_unlockedSteps.Add(step);
|
||||
|
||||
if (_stepBehaviours.TryGetValue(step, out var behaviour))
|
||||
{
|
||||
behaviour.UnlockStep();
|
||||
@@ -301,14 +447,18 @@ namespace PuzzleS
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the puzzle is complete (all steps finished).
|
||||
/// Checks if the puzzle is complete (all steps in level finished).
|
||||
/// </summary>
|
||||
private void CheckPuzzleCompletion()
|
||||
{
|
||||
if (_completedSteps.Count == _stepBehaviours.Count)
|
||||
if (_currentLevelData == null) return;
|
||||
|
||||
if (_currentLevelData.IsLevelComplete(_completedSteps))
|
||||
{
|
||||
Logging.Debug("[Puzzles] Puzzle complete! All steps finished.");
|
||||
// TODO: Fire puzzle complete event or trigger outcome logic
|
||||
Logging.Debug("[Puzzles] All puzzles complete! Level finished.");
|
||||
|
||||
// Fire level complete event
|
||||
OnAllPuzzlesComplete?.Invoke(_currentLevelData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,9 +467,6 @@ namespace PuzzleS
|
||||
/// </summary>
|
||||
public bool IsStepUnlocked(PuzzleStepSO step)
|
||||
{
|
||||
// _runtimeDependencies.Clear();
|
||||
// BuildRuntimeDependencies();
|
||||
// UnlockInitialSteps();
|
||||
return _unlockedSteps.Contains(step);
|
||||
}
|
||||
|
||||
@@ -332,6 +479,23 @@ namespace PuzzleS
|
||||
{
|
||||
return _completedSteps.Any(step => step.stepId == stepId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current level puzzle data
|
||||
/// </summary>
|
||||
public PuzzleLevelDataSO GetCurrentLevelData()
|
||||
{
|
||||
return _currentLevelData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if puzzle data is loaded
|
||||
/// </summary>
|
||||
/// <returns>True if data loading has completed (whether successful or not)</returns>
|
||||
public bool IsDataLoaded()
|
||||
{
|
||||
return _isDataLoaded;
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// ScriptableObject representing a single puzzle step, its display info, and which steps it unlocks.
|
||||
@@ -29,4 +30,58 @@ public class PuzzleStepSO : ScriptableObject
|
||||
/// </summary>
|
||||
[Header("Unlocks")]
|
||||
public List<PuzzleStepSO> unlocks = new List<PuzzleStepSO>();
|
||||
|
||||
/// <summary>
|
||||
/// Override Equals to compare by stepId rather than reference equality.
|
||||
/// This ensures consistent behavior across different platforms (Editor vs Mobile).
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to compare to</param>
|
||||
/// <returns>True if the objects represent the same puzzle step</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null) return false;
|
||||
|
||||
// Check if the object is actually a PuzzleStepSO
|
||||
PuzzleStepSO other = obj as PuzzleStepSO;
|
||||
if (other == null) return false;
|
||||
|
||||
// Compare by stepId instead of reference
|
||||
return string.Equals(stepId, other.stepId, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override GetHashCode to be consistent with the Equals method.
|
||||
/// This is crucial for HashSet and Dictionary to work properly.
|
||||
/// </summary>
|
||||
/// <returns>Hash code based on stepId</returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
// Generate hash code from stepId to ensure consistent hashing
|
||||
return stepId != null ? stepId.GetHashCode() : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override == operator to use our custom equality logic
|
||||
/// </summary>
|
||||
public static bool operator ==(PuzzleStepSO a, PuzzleStepSO b)
|
||||
{
|
||||
// Check if both are null or if they're the same instance
|
||||
if (ReferenceEquals(a, b))
|
||||
return true;
|
||||
|
||||
// Check if either is null (but not both, as that's handled above)
|
||||
if (((object)a == null) || ((object)b == null))
|
||||
return false;
|
||||
|
||||
// Use our custom Equals method
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override != operator to be consistent with == operator
|
||||
/// </summary>
|
||||
public static bool operator !=(PuzzleStepSO a, PuzzleStepSO b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user