WutheringWaves/GameServer/Systems/Entity/Component/EntityComponentSystem.cs

35 lines
1.2 KiB
C#
Raw Normal View History

2024-02-10 16:04:03 +00:00
using Protocol;
namespace GameServer.Systems.Entity.Component;
internal class EntityComponentSystem
{
private readonly List<EntityComponentBase> _components;
public EntityComponentSystem()
{
_components = [];
}
public TEntityComponent Create<TEntityComponent>() 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<TEntityComponent>() where TEntityComponent : EntityComponentBase
{
return (_components.Single(component => component is TEntityComponent) as TEntityComponent)!;
}
public bool TryGet<TEntityComponent>(out TEntityComponent? component) where TEntityComponent : EntityComponentBase
{
return (component = _components.SingleOrDefault(component => component is TEntityComponent) as TEntityComponent) != null;
}
public IEnumerable<EntityComponentPb> Pb => _components.Select(component => component.Pb);
}