80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Game.Scripts.Runtime.Data;
|
|
|
|
namespace MegaKoop.Game.Networking
|
|
{
|
|
internal static class EnemyDefinitionRegistry
|
|
{
|
|
private static readonly Dictionary<string, EnemyDefinition> DefinitionsById = new(StringComparer.OrdinalIgnoreCase);
|
|
private static readonly Dictionary<string, EnemyDefinition> DefinitionsByName = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
internal static void Register(EnemyDefinition definition)
|
|
{
|
|
if (definition == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var id = NormalizeId(definition);
|
|
if (!string.IsNullOrEmpty(id))
|
|
{
|
|
DefinitionsById[id] = definition;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(definition.name))
|
|
{
|
|
DefinitionsByName[definition.name] = definition;
|
|
}
|
|
}
|
|
|
|
internal static bool TryGet(string idOrName, out EnemyDefinition definition)
|
|
{
|
|
definition = null;
|
|
if (string.IsNullOrWhiteSpace(idOrName))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (DefinitionsById.TryGetValue(idOrName, out definition))
|
|
{
|
|
return definition != null;
|
|
}
|
|
|
|
if (DefinitionsByName.TryGetValue(idOrName, out definition))
|
|
{
|
|
return definition != null;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
internal static string ResolveId(EnemyDefinition definition)
|
|
{
|
|
if (definition == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var id = NormalizeId(definition);
|
|
Register(definition);
|
|
return id;
|
|
}
|
|
|
|
private static string NormalizeId(EnemyDefinition definition)
|
|
{
|
|
if (definition == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(definition.Id))
|
|
{
|
|
return definition.Id;
|
|
}
|
|
|
|
return definition.name ?? string.Empty;
|
|
}
|
|
}
|
|
}
|