2024-02-24 21:49:57 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace GameServer.Systems.Entity;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
internal class EntityFactory
|
|
|
|
|
{
|
2024-02-24 21:49:57 +00:00
|
|
|
|
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;
|
2024-02-24 21:49:57 +00:00
|
|
|
|
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)
|
2024-02-24 21:49:57 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2024-02-24 21:49:57 +00:00
|
|
|
|
return monsterEntity;
|
|
|
|
|
}
|
2024-02-12 20:15:14 +00:00
|
|
|
|
|
2024-02-10 16:04:03 +00:00
|
|
|
|
private long NextId() => Interlocked.Increment(ref _entityIdCounter);
|
|
|
|
|
}
|