using System.Diagnostics.CodeAnalysis; using Protocol; namespace GameServer.Systems.Entity.Component; internal class EntityComponentSystem { private readonly List _components; public EntityComponentSystem() { _components = []; } public TEntityComponent Create() where TEntityComponent : EntityComponentBase, new() { if (_components.Any(component => component is TEntityComponent)) throw new InvalidOperationException($"Component of type {nameof(TEntityComponent)} already exists"); TEntityComponent component = new(); _components.Add(component); return component; } public TEntityComponent Get() where TEntityComponent : EntityComponentBase { return (_components.Single(component => component is TEntityComponent) as TEntityComponent)!; } public bool TryGet([NotNullWhen(true)] out TEntityComponent? component) where TEntityComponent : EntityComponentBase { return (component = _components.SingleOrDefault(component => component is TEntityComponent) as TEntityComponent) != null; } public IEnumerable Pb => _components.Select(component => component.Pb); }