using System; using System.Collections.Generic; using UnityEngine; namespace NightclubArcadia.Skills { [CreateAssetMenu(menuName = "Nightclub Arcadia/Skill Database", fileName = "SkillDatabase")] public sealed class SkillDatabase : ScriptableObject { [SerializeField] List skills = new List(); Dictionary _lookup; public IReadOnlyList All => skills; public bool TryGet(string skillId, out SkillDefinition def) { EnsureLookup(); if (string.IsNullOrEmpty(skillId)) { def = null; return false; } return _lookup.TryGetValue(skillId, out def); } void EnsureLookup() { if (_lookup != null) { return; } _lookup = new Dictionary(StringComparer.OrdinalIgnoreCase); if (skills == null) { return; } foreach (var skill in skills) { if (skill == null || string.IsNullOrEmpty(skill.Id)) { continue; } _lookup[skill.Id] = skill; } } void OnValidate() { _lookup = null; if (skills == null) { return; } var seen = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (var skill in skills) { if (skill == null) { Debug.LogError($"[{nameof(SkillDatabase)}] Null skill entry in {name}.", this); continue; } if (string.IsNullOrEmpty(skill.Id)) { Debug.LogError($"[{nameof(SkillDatabase)}] Skill '{skill.name}' has an empty Id.", this); continue; } if (!seen.Add(skill.Id)) { Debug.LogError($"[{nameof(SkillDatabase)}] Duplicate skill Id '{skill.Id}'.", this); } } } #if UNITY_EDITOR public void EditorSetSkills(List newSkills) { skills = newSkills ?? new List(); _lookup = null; } #endif } }