67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using RPG.Network.Proto;
|
|||
|
|
|||
|
namespace RPG.Services.Gameserver.Game.Entity;
|
|||
|
|
|||
|
internal class EntityManager
|
|||
|
{
|
|||
|
private readonly Dictionary<uint, EntityBase> _entities = [];
|
|||
|
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IEntityEventListener _listener;
|
|||
|
|
|||
|
public EntityManager(ILogger<EntityManager> logger, IEntityEventListener listener)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_listener = listener;
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<SceneEntityInfo> EntityInfoList => _entities.Values.Select(entity => entity.SceneEntityInfo);
|
|||
|
|
|||
|
public AvatarEntity? GetAvatar(uint uid, uint avatarId)
|
|||
|
{
|
|||
|
foreach (EntityBase entity in _entities.Values)
|
|||
|
{
|
|||
|
if (entity is AvatarEntity avatar)
|
|||
|
{
|
|||
|
if (avatar.Uid == uid && avatar.AvatarId == avatarId)
|
|||
|
return avatar;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public void NotifyUpdate(EntityBase entity)
|
|||
|
{
|
|||
|
_listener.OnUpdateEntity(entity.SceneEntityInfo);
|
|||
|
}
|
|||
|
|
|||
|
public void RemoveEntities(IEnumerable<uint> ids)
|
|||
|
{
|
|||
|
foreach (uint id in ids)
|
|||
|
{
|
|||
|
_ = _entities.Remove(id);
|
|||
|
}
|
|||
|
|
|||
|
_ = _listener.OnRemoveEntities(ids);
|
|||
|
}
|
|||
|
|
|||
|
public void AddEntity(EntityBase entity)
|
|||
|
{
|
|||
|
if (!_entities.TryAdd(entity.Id, entity))
|
|||
|
_logger.LogError("AddEntity: entity with id {entityId} already added!", entity.Id);
|
|||
|
}
|
|||
|
|
|||
|
public EntityBase? GetEntityById(uint entityId)
|
|||
|
{
|
|||
|
_ = _entities.TryGetValue(entityId, out EntityBase? entity);
|
|||
|
return entity;
|
|||
|
}
|
|||
|
|
|||
|
public void Clear()
|
|||
|
{
|
|||
|
_entities.Clear();
|
|||
|
}
|
|||
|
}
|