Restructured for new direction.
This commit is contained in:
@@ -1,42 +1,44 @@
|
||||
// ==============================
|
||||
// PuzzleBase.cs (updated)
|
||||
// ==============================
|
||||
|
||||
using System.Collections.Generic;
|
||||
using BriarQueen.Framework.Assets;
|
||||
using BriarQueen.Framework.Coordinators.Events;
|
||||
using BriarQueen.Framework.Managers.Assets;
|
||||
using BriarQueen.Framework.Managers.Audio;
|
||||
using BriarQueen.Framework.Managers.Hints.Data;
|
||||
using BriarQueen.Framework.Managers.IO;
|
||||
using BriarQueen.Framework.Managers.Levels.Data;
|
||||
using BriarQueen.Framework.Registries;
|
||||
using BriarQueen.Framework.Services.Destruction;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
namespace BriarQueen.Framework.Services.Puzzles.Base
|
||||
{
|
||||
public abstract class BasePuzzle : BaseLevel
|
||||
public abstract class BasePuzzle : MonoBehaviour
|
||||
{
|
||||
protected AddressableManager AddressableManager;
|
||||
protected AssetRegistry AssetRegistry;
|
||||
protected AudioManager AudioManager;
|
||||
protected DestructionService DestructionService;
|
||||
protected EventCoordinator EventCoordinator;
|
||||
protected ItemRegistry ItemRegistry;
|
||||
protected PuzzleService PuzzleService;
|
||||
protected SaveManager SaveManager;
|
||||
|
||||
public abstract string PuzzleID { get; }
|
||||
|
||||
public override bool IsPuzzleLevel => true;
|
||||
|
||||
// BaseLevel still requires these.
|
||||
public abstract override string LevelName { get; }
|
||||
public abstract override Dictionary<int, BaseHint> Hints { get; }
|
||||
|
||||
public abstract UniTask CompletePuzzle();
|
||||
|
||||
public virtual UniTask PostLoad()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual UniTask PreUnload()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
[Inject]
|
||||
public void Construct(EventCoordinator eventCoordinator, AudioManager audioManager,
|
||||
SaveManager saveManager, ItemRegistry itemRegistry, AddressableManager addressableManager,
|
||||
AssetRegistry assetRegistry, PuzzleService puzzleService)
|
||||
AssetRegistry assetRegistry, PuzzleService puzzleService, DestructionService destructionService)
|
||||
{
|
||||
EventCoordinator = eventCoordinator;
|
||||
AudioManager = audioManager;
|
||||
@@ -45,6 +47,7 @@ namespace BriarQueen.Framework.Services.Puzzles.Base
|
||||
AddressableManager = addressableManager;
|
||||
AssetRegistry = assetRegistry;
|
||||
PuzzleService = puzzleService;
|
||||
DestructionService = destructionService;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
{
|
||||
private readonly SaveManager _saveManager;
|
||||
|
||||
private BasePuzzle _currentBasePuzzle;
|
||||
private readonly Dictionary<string, BasePuzzle> _activePuzzles = new();
|
||||
private bool _isWritingState;
|
||||
|
||||
[Inject]
|
||||
@@ -31,40 +31,98 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
|
||||
public async UniTask LoadPuzzle(BasePuzzle basePuzzle)
|
||||
{
|
||||
_currentBasePuzzle = basePuzzle;
|
||||
if (_currentBasePuzzle == null)
|
||||
if (basePuzzle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await TryRestorePuzzleState(_currentBasePuzzle);
|
||||
if (string.IsNullOrWhiteSpace(basePuzzle.PuzzleID))
|
||||
{
|
||||
Debug.LogWarning($"[PuzzleService] Cannot load puzzle '{basePuzzle.name}' with null/empty PuzzleID.");
|
||||
return;
|
||||
}
|
||||
|
||||
_activePuzzles[basePuzzle.PuzzleID] = basePuzzle;
|
||||
|
||||
await basePuzzle.PostLoad();
|
||||
await TryRestorePuzzleState(basePuzzle);
|
||||
}
|
||||
|
||||
public async UniTask LoadPuzzles(IEnumerable<BasePuzzle> basePuzzles)
|
||||
{
|
||||
_activePuzzles.Clear();
|
||||
|
||||
if (basePuzzles == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var basePuzzle in basePuzzles)
|
||||
{
|
||||
await LoadPuzzle(basePuzzle);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask SavePuzzle(BasePuzzle basePuzzle)
|
||||
{
|
||||
if (basePuzzle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_currentBasePuzzle != null && basePuzzle != _currentBasePuzzle)
|
||||
if (!string.IsNullOrWhiteSpace(basePuzzle.PuzzleID) &&
|
||||
_activePuzzles.TryGetValue(basePuzzle.PuzzleID, out var activePuzzle) &&
|
||||
activePuzzle != basePuzzle)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await SavePuzzleState(basePuzzle, flushToDisk: true);
|
||||
_currentBasePuzzle = null;
|
||||
await basePuzzle.PreUnload();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(basePuzzle.PuzzleID))
|
||||
{
|
||||
_activePuzzles.Remove(basePuzzle.PuzzleID);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask SavePuzzles(IEnumerable<BasePuzzle> basePuzzles)
|
||||
{
|
||||
if (basePuzzles != null)
|
||||
{
|
||||
foreach (var basePuzzle in basePuzzles)
|
||||
{
|
||||
await SavePuzzle(basePuzzle);
|
||||
}
|
||||
}
|
||||
|
||||
_activePuzzles.Clear();
|
||||
}
|
||||
|
||||
private async UniTask OnBeforeSaveRequestedAsync()
|
||||
{
|
||||
if (_currentBasePuzzle == null)
|
||||
if (_activePuzzles.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await SavePuzzleState(_currentBasePuzzle, flushToDisk: false);
|
||||
foreach (var basePuzzle in _activePuzzles.Values.ToList())
|
||||
{
|
||||
await SavePuzzleState(basePuzzle, flushToDisk: false);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask TryRestorePuzzleState(BasePuzzle basePuzzle)
|
||||
{
|
||||
if (basePuzzle == null || _saveManager.CurrentSave == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (basePuzzle is not IPuzzleStateful stateful)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var save = _saveManager.CurrentSave;
|
||||
var entry = save.PuzzleStates?.FirstOrDefault(x => x != null && x.PuzzleID == basePuzzle.PuzzleID);
|
||||
@@ -83,20 +141,28 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
private async UniTask SavePuzzleState(BasePuzzle basePuzzle, bool flushToDisk)
|
||||
{
|
||||
if (basePuzzle == null || _saveManager.CurrentSave == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (basePuzzle is not IPuzzleStateful stateful)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isWritingState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isWritingState = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (basePuzzle is IPuzzleWorldStateSync worldStateSync)
|
||||
{
|
||||
worldStateSync.SyncWorldStateToSave();
|
||||
}
|
||||
|
||||
var save = _saveManager.CurrentSave;
|
||||
save.PuzzleStates ??= new List<PuzzleStateSaveData>();
|
||||
@@ -123,7 +189,9 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
existing.Completed = stateful.IsCompleted;
|
||||
|
||||
if (flushToDisk)
|
||||
{
|
||||
await _saveManager.SaveGameDataLatest();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -131,4 +199,4 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user