50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System.Collections.ObjectModel;
|
|
|
|
namespace Sand.Core;
|
|
|
|
public sealed class ParticleLibrary : IParticleLibrary
|
|
{
|
|
private readonly IReadOnlyList<ParticleDef> _definitions;
|
|
private readonly Dictionary<string, ushort> _typeIds;
|
|
|
|
public ParticleLibrary(IReadOnlyList<ParticleDef> definitions)
|
|
{
|
|
if (definitions.Count == 0)
|
|
{
|
|
throw new ArgumentException("At least one particle definition is required.", nameof(definitions));
|
|
}
|
|
|
|
_definitions = new ReadOnlyCollection<ParticleDef>(definitions.ToArray());
|
|
_typeIds = new Dictionary<string, ushort>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
for (var index = 0; index < definitions.Count; index++)
|
|
{
|
|
_typeIds[definitions[index].Id] = checked((ushort)(index + 1));
|
|
}
|
|
}
|
|
|
|
public IReadOnlyList<ParticleDef> Definitions => _definitions;
|
|
|
|
public ushort GetTypeId(string id)
|
|
{
|
|
if (_typeIds.TryGetValue(id, out var typeId))
|
|
{
|
|
return typeId;
|
|
}
|
|
|
|
throw new KeyNotFoundException($"Unknown particle id '{id}'.");
|
|
}
|
|
|
|
public bool TryGetTypeId(string id, out ushort typeId) => _typeIds.TryGetValue(id, out typeId);
|
|
|
|
public ParticleDef GetDefinition(ushort typeId)
|
|
{
|
|
if (typeId == 0 || typeId > _definitions.Count)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(typeId), $"Unknown type id '{typeId}'.");
|
|
}
|
|
|
|
return _definitions[typeId - 1];
|
|
}
|
|
}
|