FINALIZE THIS SHIEEEEEET

This commit is contained in:
Michal Pikulski
2025-10-16 19:40:11 +02:00
parent d2c6c5df38
commit e60cca7830
5 changed files with 145 additions and 30 deletions

View File

@@ -229,9 +229,7 @@ MonoBehaviour:
m_EditorClassIdentifier: AppleHillsScripts::UI.LoadingScreenController m_EditorClassIdentifier: AppleHillsScripts::UI.LoadingScreenController
loadingScreenContainer: {fileID: 7270617579256400696} loadingScreenContainer: {fileID: 7270617579256400696}
progressBarImage: {fileID: 1674678211233966532} progressBarImage: {fileID: 1674678211233966532}
minimumDisplayTime: 2 minimumDisplayTime: 1
animateProgressBar: 1
progressBarSmoothTime: 0.1
progressUpdateInterval: 0.1 progressUpdateInterval: 0.1
--- !u!1 &6888795583318782279 --- !u!1 &6888795583318782279
GameObject: GameObject:

View File

@@ -255,7 +255,7 @@ MonoBehaviour:
m_EditorClassIdentifier: AppleHillsScripts::Bootstrap.InitialLoadingScreen m_EditorClassIdentifier: AppleHillsScripts::Bootstrap.InitialLoadingScreen
loadingScreenContainer: {fileID: 180679697} loadingScreenContainer: {fileID: 180679697}
progressBarImage: {fileID: 180679696} progressBarImage: {fileID: 180679696}
minimumDisplayTime: 2 minimumDisplayTime: 1
progressUpdateInterval: 0.1 progressUpdateInterval: 0.1
--- !u!1 &400217123 --- !u!1 &400217123
GameObject: GameObject:

View File

@@ -71,9 +71,16 @@ namespace PuzzleS
void Start() void Start()
{ {
// Register with PuzzleManager, regardless of enabled/disabled state // Simply register with the PuzzleManager
// PuzzleManager will call UnlockStep or LockStep based on current puzzle state // The manager will handle state updates appropriately based on whether data is loaded
PuzzleManager.Instance?.RegisterStepBehaviour(this); if (stepData != null && PuzzleManager.Instance != null)
{
PuzzleManager.Instance.RegisterStepBehaviour(this);
}
else if (stepData == null)
{
Logging.Warning($"[Puzzles] Cannot register step on {gameObject.name}: stepData is null");
}
} }
void OnDestroy() void OnDestroy()
@@ -83,7 +90,11 @@ namespace PuzzleS
_interactable.interactionStarted.RemoveListener(OnInteractionStarted); _interactable.interactionStarted.RemoveListener(OnInteractionStarted);
_interactable.interactionComplete.RemoveListener(OnInteractionComplete); _interactable.interactionComplete.RemoveListener(OnInteractionComplete);
} }
PuzzleManager.Instance?.UnregisterStepBehaviour(this);
if (PuzzleManager.Instance != null && stepData != null)
{
PuzzleManager.Instance.UnregisterStepBehaviour(this);
}
} }
/// <summary> /// <summary>

View File

@@ -33,10 +33,13 @@ namespace PuzzleS
// Current level puzzle data // Current level puzzle data
private PuzzleLevelDataSO _currentLevelData; private PuzzleLevelDataSO _currentLevelData;
private AsyncOperationHandle<PuzzleLevelDataSO> _levelDataLoadOperation; private AsyncOperationHandle<PuzzleLevelDataSO> _levelDataLoadOperation;
private bool _isLoadingLevelData = false; private bool _isDataLoaded = false;
// Store registered behaviors that are waiting for data to be loaded
private List<ObjectiveStepBehaviour> _registeredBehaviours = new List<ObjectiveStepBehaviour>();
/// <summary> /// <summary>
/// Singleton instance of the PuzzleManager. No longer creates an instance if one doesn't exist. /// Singleton instance of the PuzzleManager.
/// </summary> /// </summary>
public static PuzzleManager Instance => _instance; public static PuzzleManager Instance => _instance;
@@ -66,6 +69,7 @@ namespace PuzzleS
{ {
// Subscribe to SceneManagerService events after boot is complete // Subscribe to SceneManagerService events after boot is complete
SceneManagerService.Instance.SceneLoadCompleted += OnSceneLoadCompleted; SceneManagerService.Instance.SceneLoadCompleted += OnSceneLoadCompleted;
SceneManagerService.Instance.SceneLoadStarted += OnSceneLoadStarted;
// Find player transform // Find player transform
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform; _playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
@@ -74,7 +78,7 @@ namespace PuzzleS
StartProximityChecks(); StartProximityChecks();
// Load puzzle data for the current scene if not already loading // Load puzzle data for the current scene if not already loading
if (_currentLevelData == null && !_isLoadingLevelData) if (_currentLevelData == null && !_isDataLoaded)
{ {
LoadPuzzleDataForCurrentScene(); LoadPuzzleDataForCurrentScene();
} }
@@ -90,6 +94,7 @@ namespace PuzzleS
if (SceneManagerService.Instance != null) if (SceneManagerService.Instance != null)
{ {
SceneManagerService.Instance.SceneLoadCompleted -= OnSceneLoadCompleted; SceneManagerService.Instance.SceneLoadCompleted -= OnSceneLoadCompleted;
SceneManagerService.Instance.SceneLoadStarted -= OnSceneLoadStarted;
} }
// Release addressable handle if needed // Release addressable handle if needed
@@ -99,6 +104,16 @@ namespace PuzzleS
} }
} }
/// <summary>
/// Called when a scene is starting to load
/// </summary>
public void OnSceneLoadStarted(string sceneName)
{
// 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> /// <summary>
/// Called when a scene is loaded /// Called when a scene is loaded
/// </summary> /// </summary>
@@ -132,7 +147,7 @@ namespace PuzzleS
return; return;
} }
_isLoadingLevelData = true; _isDataLoaded = false;
string addressablePath = $"Puzzles/{currentScene}"; string addressablePath = $"Puzzles/{currentScene}";
Logging.Debug($"[Puzzles] Loading puzzle data from addressable: {addressablePath}"); Logging.Debug($"[Puzzles] Loading puzzle data from addressable: {addressablePath}");
@@ -143,9 +158,12 @@ namespace PuzzleS
Addressables.Release(_levelDataLoadOperation); Addressables.Release(_levelDataLoadOperation);
} }
// Check if the addressable exists before trying to load it
if (!AppleHillsUtils.AddressableKeyExists(addressablePath)) if (!AppleHillsUtils.AddressableKeyExists(addressablePath))
{ {
Logging.Warning($"[Puzzles] Puzzle key does not exist in Addressables: {addressablePath}. Returning early!"); Logging.Warning($"[Puzzles] Puzzle data does not exist for scene: {currentScene}");
_isDataLoaded = true; // Mark as loaded but with no data
_currentLevelData = null;
return; return;
} }
@@ -153,8 +171,6 @@ namespace PuzzleS
_levelDataLoadOperation = Addressables.LoadAssetAsync<PuzzleLevelDataSO>(addressablePath); _levelDataLoadOperation = Addressables.LoadAssetAsync<PuzzleLevelDataSO>(addressablePath);
_levelDataLoadOperation.Completed += handle => _levelDataLoadOperation.Completed += handle =>
{ {
_isLoadingLevelData = false;
if (handle.Status == AsyncOperationStatus.Succeeded) if (handle.Status == AsyncOperationStatus.Succeeded)
{ {
_currentLevelData = handle.Result; _currentLevelData = handle.Result;
@@ -167,8 +183,11 @@ namespace PuzzleS
// Unlock initial steps // Unlock initial steps
UnlockInitialSteps(); UnlockInitialSteps();
// Update existing ObjectiveStepBehaviours with the current state // Update all registered behaviors now that data is loaded
UpdateRegisteredStepStates(); UpdateAllRegisteredBehaviors();
// Mark data as loaded
_isDataLoaded = true;
// Notify listeners // Notify listeners
OnLevelDataLoaded?.Invoke(_currentLevelData); OnLevelDataLoaded?.Invoke(_currentLevelData);
@@ -176,6 +195,8 @@ namespace PuzzleS
else else
{ {
Logging.Warning($"[Puzzles] Failed to load puzzle data for {currentScene}: {handle.OperationException?.Message}"); Logging.Warning($"[Puzzles] Failed to load puzzle data for {currentScene}: {handle.OperationException?.Message}");
_isDataLoaded = true; // Mark as loaded but with error
_currentLevelData = null;
} }
}; };
} }
@@ -238,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> /// <summary>
/// Registers a step behaviour with the manager. /// Registers a step behaviour with the manager.
/// </summary> /// </summary>
@@ -246,13 +285,24 @@ namespace PuzzleS
{ {
if (behaviour?.stepData == null) return; 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)) if (!_stepBehaviours.ContainsKey(behaviour.stepData))
{ {
_stepBehaviours.Add(behaviour.stepData, behaviour); _stepBehaviours.Add(behaviour.stepData, behaviour);
Logging.Debug($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}"); Logging.Debug($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
// Immediately set the correct state based on current puzzle state // Only update state if data is already loaded
UpdateStepState(behaviour); if (_isDataLoaded && _currentLevelData != null)
{
UpdateStepState(behaviour);
}
// Otherwise, the state will be updated when data loads in UpdateAllRegisteredBehaviors
} }
} }
@@ -279,17 +329,6 @@ namespace PuzzleS
} }
} }
/// <summary>
/// Updates the states of all registered step behaviours based on current puzzle state.
/// </summary>
private void UpdateRegisteredStepStates()
{
foreach (var kvp in _stepBehaviours)
{
UpdateStepState(kvp.Value);
}
}
/// <summary> /// <summary>
/// Unregisters a step behaviour from the manager. /// Unregisters a step behaviour from the manager.
/// </summary> /// </summary>
@@ -297,7 +336,10 @@ namespace PuzzleS
public void UnregisterStepBehaviour(ObjectiveStepBehaviour behaviour) public void UnregisterStepBehaviour(ObjectiveStepBehaviour behaviour)
{ {
if (behaviour?.stepData == null) return; if (behaviour?.stepData == null) return;
_stepBehaviours.Remove(behaviour.stepData); _stepBehaviours.Remove(behaviour.stepData);
_registeredBehaviours.Remove(behaviour);
Logging.Debug($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}"); Logging.Debug($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
} }
@@ -446,6 +488,15 @@ namespace PuzzleS
return _currentLevelData; 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() void OnApplicationQuit()
{ {
_isQuitting = true; _isQuitting = true;

View File

@@ -1,5 +1,6 @@
using UnityEngine; using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using System;
/// <summary> /// <summary>
/// ScriptableObject representing a single puzzle step, its display info, and which steps it unlocks. /// ScriptableObject representing a single puzzle step, its display info, and which steps it unlocks.
@@ -29,4 +30,58 @@ public class PuzzleStepSO : ScriptableObject
/// </summary> /// </summary>
[Header("Unlocks")] [Header("Unlocks")]
public List<PuzzleStepSO> unlocks = new List<PuzzleStepSO>(); 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);
}
} }