Cleanup code structure, code smells, rename paramter, break out more granular managers
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using Core;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.StatueDressup.Controllers;
|
||||
using Minigames.StatueDressup.Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using Utils;
|
||||
|
||||
namespace Minigames.StatueDressup.Display
|
||||
@@ -27,10 +26,6 @@ namespace Minigames.StatueDressup.Display
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugInfo = true;
|
||||
|
||||
private Dictionary<string, DecorationData> decorationDataDict;
|
||||
private AsyncOperationHandle<IList<DecorationData>> decorationDataHandle;
|
||||
private AppleHills.Core.Settings.IStatueDressupSettings settings;
|
||||
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
base.OnManagedStart();
|
||||
@@ -44,81 +39,40 @@ namespace Minigames.StatueDressup.Display
|
||||
return;
|
||||
}
|
||||
|
||||
// Get settings
|
||||
settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IStatueDressupSettings>();
|
||||
|
||||
// Start async loading via coroutine wrapper
|
||||
StartCoroutine(LoadAndDisplayDecorationsCoroutine());
|
||||
// DecorationDataManager exists (initialized in OnManagedAwake) but data loads async
|
||||
// Wait for data to finish loading before displaying decorations
|
||||
StartCoroutine(WaitForDataAndDisplay());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine wrapper for async loading and display
|
||||
/// Wait for DecorationDataManager to finish loading data before displaying decorations
|
||||
/// </summary>
|
||||
private System.Collections.IEnumerator LoadAndDisplayDecorationsCoroutine()
|
||||
private System.Collections.IEnumerator WaitForDataAndDisplay()
|
||||
{
|
||||
// Convert async Task to coroutine-compatible operation
|
||||
var loadTask = LoadDecorationDataAsync();
|
||||
|
||||
// Wait for async operation to complete
|
||||
while (!loadTask.IsCompleted)
|
||||
// Wait for data to load (manager is guaranteed to exist)
|
||||
while (!DecorationDataManager.Instance.IsLoaded)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Check for exceptions
|
||||
if (loadTask.IsFaulted)
|
||||
if (showDebugInfo)
|
||||
{
|
||||
Logging.Error($"[StatueDecorationLoader] Failed to load decoration data: {loadTask.Exception?.GetBaseException().Message}");
|
||||
yield break;
|
||||
Logging.Debug("[StatueDecorationLoader] DecorationData loaded, displaying decorations");
|
||||
}
|
||||
|
||||
// Load and display decorations
|
||||
LoadAndDisplayDecorations();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load all DecorationData assets via Addressables and build lookup dictionary
|
||||
/// </summary>
|
||||
private async System.Threading.Tasks.Task LoadDecorationDataAsync()
|
||||
{
|
||||
string label = settings?.DecorationDataLabel;
|
||||
|
||||
if (string.IsNullOrEmpty(label))
|
||||
{
|
||||
Logging.Error("[StatueDecorationLoader] Decoration data label not set in settings!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (showDebugInfo)
|
||||
{
|
||||
Logging.Debug($"[StatueDecorationLoader] Loading DecorationData with label '{label}'...");
|
||||
}
|
||||
|
||||
// Use utility to load all DecorationData and create dictionary by ID
|
||||
var result = await AddressablesUtility.LoadAssetsByLabelAsync<DecorationData, string>(
|
||||
label,
|
||||
data => data.DecorationId, // Key selector: use DecorationId as key
|
||||
progress => { /* Optional: could show loading bar */ }
|
||||
);
|
||||
|
||||
decorationDataDict = result.dictionary;
|
||||
decorationDataHandle = result.handle;
|
||||
|
||||
if (showDebugInfo)
|
||||
{
|
||||
Logging.Debug($"[StatueDecorationLoader] Loaded {decorationDataDict.Count} DecorationData assets");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load decoration metadata and spawn decorations
|
||||
/// </summary>
|
||||
public void LoadAndDisplayDecorations()
|
||||
{
|
||||
// Check if DecorationData is loaded
|
||||
if (decorationDataDict == null || decorationDataDict.Count == 0)
|
||||
// Check if DecorationData is loaded via manager
|
||||
if (DecorationDataManager.Instance == null || !DecorationDataManager.Instance.IsLoaded)
|
||||
{
|
||||
Logging.Warning("[StatueDecorationLoader] DecorationData not loaded yet. Cannot display decorations.");
|
||||
Logging.Warning("[StatueDecorationLoader] DecorationDataManager not ready. Cannot display decorations.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -206,12 +160,12 @@ namespace Minigames.StatueDressup.Display
|
||||
|
||||
/// <summary>
|
||||
/// Spawn a single decoration from placement data
|
||||
/// Looks up DecorationData from pre-loaded dictionary and applies coordinate conversion
|
||||
/// Looks up DecorationData from manager and applies coordinate conversion
|
||||
/// </summary>
|
||||
private bool SpawnDecoration(DecorationPlacement placement, float conversionFactor, Vector2 sourceStatueSize, Vector2 targetStatueWorldSize)
|
||||
{
|
||||
// Look up DecorationData from dictionary
|
||||
if (!decorationDataDict.TryGetValue(placement.decorationId, out DecorationData decorationData))
|
||||
// Look up DecorationData from manager
|
||||
if (!DecorationDataManager.Instance.TryGetData(placement.decorationId, out DecorationData decorationData))
|
||||
{
|
||||
Logging.Warning($"[StatueDecorationLoader] DecorationData not found for ID: {placement.decorationId}");
|
||||
return false;
|
||||
@@ -375,14 +329,7 @@ namespace Minigames.StatueDressup.Display
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup - release Addressables handle
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Release DecorationData handle
|
||||
AddressablesUtility.ReleaseHandle(decorationDataHandle);
|
||||
}
|
||||
// Cleanup handled by DecorationDataManager - no need for OnDestroy here
|
||||
|
||||
/// <summary>
|
||||
/// Reload decorations (useful for testing)
|
||||
|
||||
Reference in New Issue
Block a user