Skip to content

Commit

Permalink
refactor: factories
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Jul 5, 2024
1 parent 53ff31a commit b1ab3c6
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Components.Data;
using Components.Render.Animation;
using Components.Tags;
using Entities.Factories.Characters;
using LightInject;

[assembly: CompositionRootType(typeof(GameDesktop.CompositionRoots.Entities.PlayerEntityCompositionRoot))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Components.Data;
using Components.Render.Static;
using Entities.Factories.Characters;
using LightInject;

[assembly: CompositionRootType(typeof(GameDesktop.CompositionRoots.Entities.StaticEntityCompositionRoot))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Compose(IServiceRegistry serviceRegistry)
serviceRegistry.RegisterSingleton<PebbleFactory>();
serviceRegistry.RegisterSingleton<AbstractRockFactory>();

serviceRegistry.RegisterSingleton<AbstractEntityFactory>();
serviceRegistry.RegisterSingleton<EntitiesFactory>();

RegisterEntryPoint(serviceRegistry);
}
Expand Down Expand Up @@ -157,7 +157,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
// factory.GetInstance<PlayerEntityFactory>(),
// factory.GetInstance<DummyEntityFactory>(),
// factory.GetInstance<RockEntityFactory>(),
factory.GetInstance<AbstractEntityFactory>(),
factory.GetInstance<EntitiesFactory>(),
factory.GetInstance<LDtkFile>())
);
});
Expand Down
41 changes: 0 additions & 41 deletions src/Libs/Entities/Factories/AbstractEntityFactory.cs

This file was deleted.

40 changes: 0 additions & 40 deletions src/Libs/Entities/Factories/Characters/DummyEntityFactory.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Libs/Entities/Factories/Characters/PlayerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Entities.Factories.Characters;

public class PlayerFactory(IServiceFactory serviceFactory) : ConcreteEntityFactory
public class PlayerFactory(IServiceFactory serviceFactory) : EntityFactory
{
protected override void AddTags(Entity e)
{
Expand Down
39 changes: 39 additions & 0 deletions src/Libs/Entities/Factories/EntitiesFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Entities.Factories.Characters;
using Entities.Factories.Items.Rocks;
using Entities.Factories.Items.Trees;
using LDtk;
using LightInject;
using Scellecs.Morpeh;

namespace Entities.Factories;

public class EntitiesFactory(IServiceFactory serviceFactory) : IEntitiesFactory
{
private readonly Dictionary<string, IAbstractEntityFactory> _abstractFactories = new()
{
{ "Rock", serviceFactory.GetInstance<AbstractRockFactory>() },
{ "Tree", serviceFactory.GetInstance<AbstractTreeFactory>() },
// { "Default", null }
};

private readonly Dictionary<string, EntityFactory> _concreteFactories = new()
{
{ "Player", serviceFactory.GetInstance<PlayerFactory>() },
// { "Default", null }
};

public Entity? CreateEntity(EntityInstance entity, World @in) => CreateEntity(entity._Identifier, @in) ??
entity._Tags
.Select(tag =>
CreateEntity(entity._Identifier, @in))
.OfType<Entity>().FirstOrDefault();

public Entity? CreateEntity(string tag, World @in)
{
if (_abstractFactories.TryGetValue(tag, out var @abstract)) return @abstract.CreateEntity(tag, @in);

if (_concreteFactories.TryGetValue(tag, out var concrete)) return concrete.CreateEntity(@in);

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Entities.Factories;

public abstract class ConcreteEntityFactory : IConcreteEntityFactory
public abstract class EntityFactory : IEntityFactory
{
public Entity CreateEntity(World @in)
{
Expand All @@ -16,8 +16,6 @@ public Entity CreateEntity(World @in)
return e;
}

public virtual Entity CreateEntity(EntityInstance entity, World @in) => this.CreateEntity(@in);

protected abstract void AddTags(Entity e);

protected abstract void AddData(Entity e);
Expand Down
3 changes: 1 addition & 2 deletions src/Libs/Entities/Factories/IAbstractEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace Entities.Factories;

public interface IAbstractEntityFactory
{
// Entity? CreateEntity(string tag, World @in);
Entity? CreateEntity(EntityInstance entity, World @in);
Entity? CreateEntity(string tag, World @in);
}
9 changes: 9 additions & 0 deletions src/Libs/Entities/Factories/IEntitiesFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LDtk;
using Scellecs.Morpeh;

namespace Entities.Factories;

public interface IEntitiesFactory : IAbstractEntityFactory
{
Entity? CreateEntity(EntityInstance entity, World @in);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Entities.Factories;

public interface IConcreteEntityFactory : IAbstractEntityFactory
public interface IEntityFactory
{
Entity CreateEntity(World @in);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Entities.Factories.Items.Rocks;

public class AbstractRockFactory(IServiceFactory serviceFactory) : IAbstractEntityFactory
{
private readonly Dictionary<string, ConcreteEntityFactory> _factories = new()
private readonly Dictionary<string, EntityFactory> _factories = new()
{
{ "Pebble", serviceFactory.GetInstance<PebbleFactory>() },
};

public Entity? CreateEntity(EntityInstance entity, World @in) =>
_factories.TryGetValue(entity._Identifier, out var factory)
public Entity? CreateEntity(string tag, World @in) =>
_factories.TryGetValue(tag, out var factory)
? factory.CreateEntity(@in)
: null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Entities.Factories.Items.Rocks;

public class PebbleFactory(IServiceFactory serviceProvider) : ConcreteEntityFactory
public class PebbleFactory(IServiceFactory serviceProvider) : EntityFactory
{
protected override void AddTags(Entity e)
{
Expand Down
10 changes: 4 additions & 6 deletions src/Libs/Entities/Factories/Items/Trees/AbstractTreeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using Entities.Factories.Items.Rocks;
using LDtk;
using LightInject;
using LightInject;
using Scellecs.Morpeh;

namespace Entities.Factories.Items.Trees;

public class AbstractTreeFactory(IServiceFactory serviceFactory) : IAbstractEntityFactory
{
private readonly Dictionary<string, ConcreteEntityFactory> _factories = new()
private readonly Dictionary<string, EntityFactory> _factories = new()
{
{ "Tree", serviceFactory.GetInstance<TreeFactory>() },
};

public Entity? CreateEntity(EntityInstance entity, World @in) =>
_factories.TryGetValue(entity._Identifier, out var factory)
public Entity? CreateEntity(string tag, World @in) =>
_factories.TryGetValue(tag, out var factory)
? factory.CreateEntity(@in)
: null;
}
2 changes: 1 addition & 1 deletion src/Libs/Entities/Factories/Items/Trees/TreeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Entities.Factories.Items.Trees;

public class TreeFactory(IServiceFactory serviceProvider) : ConcreteEntityFactory
public class TreeFactory(IServiceFactory serviceProvider) : EntityFactory
{
protected override void AddTags(Entity e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/Entities/Factories/Meta/WorldEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Entities.Factories.Meta;

public class WorldEntityFactory(WorldMetaComponent worldMetaComponent) : ConcreteEntityFactory
public class WorldEntityFactory(WorldMetaComponent worldMetaComponent) : EntityFactory
{
protected override void AddTags(Entity e)
{
Expand Down
7 changes: 1 addition & 6 deletions src/Libs/Systems.Debugging/Systems.Debugging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
<ProjectReference Include="..\Components\Components.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\External\MonoGame.ImGuiNet\MonoGame.ImGuiNet\Monogame.ImGuiNet.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="LightInject">
<HintPath>..\..\..\..\..\.nuget\packages\lightinject\6.6.4\lib\net6.0\LightInject.dll</HintPath>
</Reference>
<PackageReference Include="LightInject" Version="6.6.4" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Libs/Systems/WorldInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Systems;

public class WorldInitializer(
World world,
AbstractEntityFactory abstractEntityFactory,
EntitiesFactory entitiesFactory,
LDtkFile ldtkFile)
: IInitializer
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public void OnAwake()

foreach (var entity in layer.EntityInstances)
{
var e = abstractEntityFactory.CreateEntity(entity, world);
var e = entitiesFactory.CreateEntity(entity, world);

if (e is null) continue;

Expand Down
2 changes: 1 addition & 1 deletion src/UnitTests/UnitTests.Entities/PlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Tests()

_serviceContainer.RegisterInstance((IServiceFactory)_serviceContainer);

_serviceContainer.RegisterSingleton<AbstractEntityFactory>();
_serviceContainer.RegisterSingleton<EntitiesFactory>();
_serviceContainer.RegisterSingleton<AbstractRockFactory>();
}

Expand Down

0 comments on commit b1ab3c6

Please sign in to comment.