forked from Shardstone/trail-into-darkness
52 lines
2.1 KiB
C#
52 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace Jovian.EncounterSystem {
|
|
/// <summary>id → encounter cache. Editor auto-repopulates on asset changes; runtime must call <see cref="PopulateEncounters"/>.</summary>
|
|
[CreateAssetMenu(fileName = "EncounterRegistry", menuName = "Jovian/Encounter System/Encounter Registry")]
|
|
public class EncounterRegistry : ScriptableObject {
|
|
public EncountersCollection[] encounterCollections = Array.Empty<EncountersCollection>();
|
|
|
|
private readonly Dictionary<string, IEncounter> encounters = new();
|
|
|
|
public Dictionary<string, IEncounter> GetEncounters() => encounters;
|
|
|
|
public void RegisterEncounter(IEncounter encounter) {
|
|
encounters?.TryAdd(encounter?.EncounterDefinition?.internalId, encounter);
|
|
}
|
|
|
|
public void UnregisterEncounter(IEncounter encounter) {
|
|
encounters.Remove(encounter.EncounterDefinition.internalId);
|
|
}
|
|
|
|
public void ClearEncounters() {
|
|
encounters.Clear();
|
|
}
|
|
|
|
public void PopulateEncounters() {
|
|
foreach(var collection in encounterCollections) {
|
|
foreach(var encounter in collection.encounterTables) {
|
|
foreach(var encounterInstance in encounter.encounters) {
|
|
RegisterEncounter(encounterInstance);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
/// <summary>Rebuilds the registry (Addressables key "EncounterRegistry") on any asset import/move/delete.</summary>
|
|
public class EncounterRegister : UnityEditor.AssetPostprocessor {
|
|
private static EncounterRegistry registryCache;
|
|
|
|
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
|
|
registryCache ??= Addressables.LoadAssetAsync<EncounterRegistry>("EncounterRegistry").WaitForCompletion();
|
|
registryCache.ClearEncounters();
|
|
registryCache.PopulateEncounters();
|
|
}
|
|
}
|
|
#endif
|
|
}
|