using Microsoft.Extensions.DependencyInjection; namespace GameServer.Systems.Entity; internal class EntityFactory { private static readonly ObjectFactory s_createPlayerEntity; private static readonly ObjectFactory s_createMonsterEntity; private long _entityIdCounter; private readonly IServiceProvider _serviceProvider; static EntityFactory() { s_createPlayerEntity = ActivatorUtilities.CreateFactory([typeof(long), typeof(int), typeof(int)]); s_createMonsterEntity = ActivatorUtilities.CreateFactory([typeof(long), typeof(int)]); } public EntityFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public PlayerEntity CreatePlayer(int characterConfigId, int playerId) { PlayerEntity entity = s_createPlayerEntity(_serviceProvider, [NextId(), characterConfigId, playerId]); entity.OnCreate(); return entity; } public MonsterEntity CreateMonster(int levelEntityId) { MonsterEntity monsterEntity = s_createMonsterEntity(_serviceProvider, [NextId(), levelEntityId]); monsterEntity.OnCreate(); return monsterEntity; } private long NextId() => Interlocked.Increment(ref _entityIdCounter); }