WutheringWaves/GameServer/Systems/Entity/EntityFactory.cs

41 lines
1.3 KiB
C#
Raw Permalink Normal View History

using Microsoft.Extensions.DependencyInjection;
namespace GameServer.Systems.Entity;
2024-02-10 16:04:03 +00:00
internal class EntityFactory
{
private static readonly ObjectFactory<PlayerEntity> s_createPlayerEntity;
private static readonly ObjectFactory<MonsterEntity> s_createMonsterEntity;
2024-02-10 16:04:03 +00:00
private long _entityIdCounter;
private readonly IServiceProvider _serviceProvider;
static EntityFactory()
{
s_createPlayerEntity = ActivatorUtilities.CreateFactory<PlayerEntity>([typeof(long), typeof(int), typeof(int)]);
s_createMonsterEntity = ActivatorUtilities.CreateFactory<MonsterEntity>([typeof(long), typeof(int)]);
}
public EntityFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
2024-02-10 16:04:03 +00:00
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();
2024-02-10 16:04:03 +00:00
return monsterEntity;
}
2024-02-10 16:04:03 +00:00
private long NextId() => Interlocked.Increment(ref _entityIdCounter);
}