68 lines
2.7 KiB
C#
68 lines
2.7 KiB
C#
// Assets/_Project/Scripts/Gameplay/BuilderSpells/BuilderSpellPool.cs
|
|
using System;
|
|
using UnityEngine;
|
|
using TD.Core;
|
|
|
|
namespace TD.Gameplay.BuilderSpells
|
|
{
|
|
/// <summary>
|
|
/// Scene singleton holding every <see cref="BuilderSpellDefinition"/> available this match,
|
|
/// authored as a flat array in the inspector for convenience. Internally it builds a
|
|
/// fixed-size table indexed by <see cref="BuilderSpellKind"/> so lookups are a single array
|
|
/// access, not a scan.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Plain MonoBehaviour: identical on every peer (same assets), so there is nothing to sync.
|
|
/// The server reads it to resolve casts; clients read it to resolve <see
|
|
/// cref="BuilderSpellDefinition.ClientPlayVfx"/> and to render the cast hotbar. Mirrors
|
|
/// <see cref="BuilderEffects.BuilderEffectPool"/> exactly.
|
|
/// </remarks>
|
|
public class BuilderSpellPool : MonoBehaviour
|
|
{
|
|
public static BuilderSpellPool Instance { get; private set; }
|
|
|
|
[Tooltip("Every BuilderSpellDefinition asset available this match. One entry per " +
|
|
"BuilderSpellKind — order doesn't matter, Kind on the asset itself decides " +
|
|
"its slot.")]
|
|
[SerializeField] private BuilderSpellDefinition[] spells;
|
|
|
|
// Fixed-size, enum-indexed lookup built once in Awake. Sized to the enum's entry count,
|
|
// not authored count, so an out-of-range Kind is a compile-time impossibility rather
|
|
// than a bounds check we'd otherwise need on every Get().
|
|
private BuilderSpellDefinition[] byKind;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Debug.LogError("[BuilderSpellPool] Multiple instances detected. Only one per scene.");
|
|
return;
|
|
}
|
|
Instance = this;
|
|
|
|
int kindCount = Enum.GetValues(typeof(BuilderSpellKind)).Length;
|
|
byKind = new BuilderSpellDefinition[kindCount];
|
|
if (spells == null) return;
|
|
|
|
for (int i = 0; i < spells.Length; i++)
|
|
{
|
|
var def = spells[i];
|
|
if (def == null) continue;
|
|
byKind[(int)def.Kind] = def;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this) Instance = null;
|
|
}
|
|
|
|
/// <summary>Returns the spell asset for <paramref name="kind"/>, or null if none is
|
|
/// authored in this pool.</summary>
|
|
public BuilderSpellDefinition Get(BuilderSpellKind kind)
|
|
{
|
|
int i = (int)kind;
|
|
return (byKind != null && i >= 0 && i < byKind.Length) ? byKind[i] : null;
|
|
}
|
|
}
|
|
}
|