91 lines
2.4 KiB
C#
91 lines
2.4 KiB
C#
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<SkillDefinition> skills = new List<SkillDefinition>();
|
|
|
|
Dictionary<string, SkillDefinition> _lookup;
|
|
|
|
public IReadOnlyList<SkillDefinition> 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<string, SkillDefinition>(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<string>(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<SkillDefinition> newSkills)
|
|
{
|
|
skills = newSkills ?? new List<SkillDefinition>();
|
|
_lookup = null;
|
|
}
|
|
#endif
|
|
}
|
|
}
|