2024-02-24 21:54:49 +00:00
|
|
|
|
using GameServer.Systems.Notify;
|
2024-02-24 21:49:57 +00:00
|
|
|
|
using Protocol;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
|
|
|
|
|
namespace GameServer.Systems.Entity;
|
|
|
|
|
internal class EntitySystem
|
|
|
|
|
{
|
|
|
|
|
private readonly List<EntityBase> _entities;
|
2024-02-25 20:29:35 +00:00
|
|
|
|
private readonly List<int> _dynamicEntityIds;
|
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
private readonly IGameActionListener _listener;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
public EntitySystem(IGameActionListener listener)
|
2024-02-10 16:04:03 +00:00
|
|
|
|
{
|
|
|
|
|
_entities = [];
|
2024-02-25 20:29:35 +00:00
|
|
|
|
_dynamicEntityIds = [];
|
2024-02-24 21:49:57 +00:00
|
|
|
|
_listener = listener;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<EntityBase> EnumerateEntities()
|
|
|
|
|
{
|
|
|
|
|
return _entities;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
public void Add(IEnumerable<EntityBase> entities)
|
2024-02-10 16:04:03 +00:00
|
|
|
|
{
|
2024-02-24 21:49:57 +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
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
_entities.Add(entity);
|
2024-02-25 20:29:35 +00:00
|
|
|
|
|
|
|
|
|
if (entity.DynamicId != 0)
|
|
|
|
|
_dynamicEntityIds.Add(entity.DynamicId);
|
2024-02-24 21:49:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ = _listener.OnEntitiesAdded(entities);
|
2024-02-10 16:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 20:29:35 +00:00
|
|
|
|
public bool HasDynamicEntity(int dynamicId)
|
|
|
|
|
{
|
|
|
|
|
return _dynamicEntityIds.Contains(dynamicId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
public void Destroy(IEnumerable<EntityBase> entities)
|
2024-02-11 22:23:13 +00:00
|
|
|
|
{
|
2024-02-24 21:49:57 +00:00
|
|
|
|
foreach (EntityBase entity in entities)
|
2024-02-25 20:29:35 +00:00
|
|
|
|
{
|
2024-02-24 21:49:57 +00:00
|
|
|
|
_ = _entities.Remove(entity);
|
2024-02-25 20:29:35 +00:00
|
|
|
|
_ = _dynamicEntityIds.Remove(entity.DynamicId);
|
|
|
|
|
}
|
2024-02-24 21:49:57 +00:00
|
|
|
|
|
|
|
|
|
_ = _listener.OnEntitiesRemoved(entities);
|
2024-02-11 22:23:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|