WutheringWaves/GameServer/Systems/Entity/EntitySystem.cs

55 lines
1.4 KiB
C#
Raw Normal View History

using GameServer.Systems.Event;
using GameServer.Systems.Notify;
using Protocol;
2024-02-10 16:04:03 +00:00
namespace GameServer.Systems.Entity;
internal class EntitySystem
{
private readonly List<EntityBase> _entities;
private readonly IGameActionListener _listener;
2024-02-10 16:04:03 +00:00
public EntitySystem(IGameActionListener listener)
2024-02-10 16:04:03 +00:00
{
_entities = [];
_listener = listener;
2024-02-10 16:04:03 +00:00
}
public IEnumerable<EntityBase> EnumerateEntities()
{
return _entities;
}
public void Add(IEnumerable<EntityBase> entities)
2024-02-10 16:04:03 +00:00
{
foreach (EntityBase entity in entities)
{
if (_entities.Any(e => e.Id == entity.Id))
throw new InvalidOperationException($"EntitySystem::Create - entity with id {entity.Id} already exists");
2024-02-10 16:04:03 +00:00
_entities.Add(entity);
}
_ = _listener.OnEntitiesAdded(entities);
2024-02-10 16:04:03 +00:00
}
public void Destroy(IEnumerable<EntityBase> entities)
{
foreach (EntityBase entity in entities)
_ = _entities.Remove(entity);
_ = _listener.OnEntitiesRemoved(entities);
}
2024-02-10 16:04:03 +00:00
public void Activate(EntityBase entity)
{
entity.Activate();
}
public TEntity? Get<TEntity>(long id) where TEntity : EntityBase
{
return _entities.SingleOrDefault(e => e.Id == id) as TEntity;
}
public IEnumerable<EntityPb> Pb => _entities.Select(e => e.Pb);
}