Skip to content

Commit

Permalink
refactor: factories
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Jun 20, 2024
1 parent 60ec21d commit 49a7345
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ internal class PlayerEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
RegisterEntity(serviceRegistry);
// RegisterEntity(serviceRegistry);
}

private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new PlayerEntityFactory(
new NameComponent("Player"), // factory.GetInstance<NameComponent>("Player"),
factory.GetInstance<InputMovableComponent>(),
factory.GetInstance<MovableComponent>(),
factory.GetInstance<TransformComponent>("PlayerEntity"),
factory.GetInstance<CameraComponent>(),
factory.GetInstance<RectangleColliderComponent>("PlayerEntity"),
factory.GetInstance<MovementAnimationsComponent>(),
factory.GetInstance<CharacterAnimatorComponent>("PlayerEntity"),
factory.GetInstance<InventoryComponent>()));
// private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
// serviceRegistry.RegisterTransient(factory => new PlayerEntityFactory(
// new NameComponent("Player"), // factory.GetInstance<NameComponent>("Player"),
// factory.GetInstance<InputMovableComponent>(),
// factory.GetInstance<MovableComponent>(),
// factory.GetInstance<TransformComponent>("PlayerEntity"),
// factory.GetInstance<CameraComponent>(),
// factory.GetInstance<RectangleColliderComponent>("PlayerEntity"),
// factory.GetInstance<MovementAnimationsComponent>(),
// factory.GetInstance<CharacterAnimatorComponent>("PlayerEntity"),
// factory.GetInstance<InventoryComponent>()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class RockEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
RegisterEntity(serviceRegistry);
// RegisterEntity(serviceRegistry);
}


private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new RockEntityFactory(
new NameComponent("Rock"), // factory.GetInstance<NameComponent>("Rock")
factory.GetInstance<ItemComponent>("Rock"),
factory.GetInstance<TransformComponent>("RockEntity"),
factory.GetInstance<SpriteComponent>() //factory.GetInstance<SpriteComponent>("Rock")
));
// private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
// serviceRegistry.RegisterTransient(factory => new RockEntityFactory(
// new NameComponent("Rock"), // factory.GetInstance<NameComponent>("Rock")
// factory.GetInstance<ItemComponent>("Rock"),
// factory.GetInstance<TransformComponent>("RockEntity"),
// factory.GetInstance<SpriteComponent>() //factory.GetInstance<SpriteComponent>("Rock")
// ));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ internal class StaticEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
RegisterEntity(serviceRegistry);
// RegisterEntity(serviceRegistry);
}


private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new DummyEntityFactory(
new NameComponent("Dummy"), // factory.GetInstance<NameComponent>("Dummy")
factory.GetInstance<TransformComponent>("DummyEntity"),
factory.GetInstance<SpriteComponent>(),
factory.GetInstance<RectangleColliderComponent>("DummyEntity")));
// private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
// serviceRegistry.RegisterTransient(factory => new DummyEntityFactory(
// new NameComponent("Dummy"), // factory.GetInstance<NameComponent>("Dummy")
// factory.GetInstance<TransformComponent>("DummyEntity"),
// factory.GetInstance<SpriteComponent>(),
// factory.GetInstance<RectangleColliderComponent>("DummyEntity")));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Entities.Factories;
using Entities.Factories.Characters;
using Entities.Factories.Items;
using Entities.Factories.Items.Rocks;
using Entities.Factories.Meta;
using Features;
using FontStashSharp.RichText;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void Compose(IServiceRegistry serviceRegistry)
serviceRegistry.RegisterSingleton<PlayerFactory>();

serviceRegistry.RegisterSingleton<PebbleFactory>();
serviceRegistry.RegisterSingleton<RockFactory>();
serviceRegistry.RegisterSingleton<AbstractRockFactory>();

serviceRegistry.RegisterSingleton<AbstractEntityFactory>();

Expand Down
40 changes: 40 additions & 0 deletions src/Libs/Entities/Factories/AbstractEntityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Entities.Factories.Characters;
using Entities.Factories.Items.Rocks;
using LDtk;
using LightInject;
using Scellecs.Morpeh;

namespace Entities.Factories;

public class AbstractEntityFactory(IServiceFactory serviceFactory) : IAbstractEntityFactory
{
private readonly Dictionary<string, IAbstractEntityFactory> _factories = new()
{
// { "Tree", null },
{ "Player", serviceFactory.GetInstance<PlayerFactory>() },
{ "Rock", serviceFactory.GetInstance<AbstractRockFactory>() },
// { "Default", null }
};

public Entity? CreateEntity(EntityInstance entity, World @in)
{
{
if (_factories.TryGetValue(entity._Identifier, out var factory))
{
return factory.CreateEntity(entity, @in);
}
}

foreach (var tag in entity._Tags)
{
if (_factories.TryGetValue(tag, out var factory))
{
return factory.CreateEntity(entity, @in);
}
}

return null;
// throw new ArgumentException(
// $"{entity._Identifier} ({entity.Iid}) has unknown tag(-s): {(string.Join(", ", entity._Tags))}");
}
}
66 changes: 33 additions & 33 deletions src/Libs/Entities/Factories/Characters/DummyEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@

namespace Entities.Factories.Characters;

public class DummyEntityFactory(
NameComponent name,
TransformComponent transform,
RectangleColliderComponent rectangleCollider)
: EntityFactory
{
private readonly SpriteComponent _sprite;

public DummyEntityFactory(
NameComponent name,
TransformComponent transform,
SpriteComponent sprite,
RectangleColliderComponent rectangleCollider) : this(name, transform, rectangleCollider)
{
_sprite = sprite;
}

protected override void AddTags(Entity e)
{
}

protected override void AddData(Entity e)
{
e.AddComponent(name);
e.AddComponent(transform);
e.AddComponent(rectangleCollider);
}

protected override void AddRender(Entity e)
{
e.AddComponent(_sprite);
}
}
// public class DummyEntityFactory(
// NameComponent name,
// TransformComponent transform,
// RectangleColliderComponent rectangleCollider)
// : EntityFactory
// {
// private readonly SpriteComponent _sprite;
//
// public DummyEntityFactory(
// NameComponent name,
// TransformComponent transform,
// SpriteComponent sprite,
// RectangleColliderComponent rectangleCollider) : this(name, transform, rectangleCollider)
// {
// _sprite = sprite;
// }
//
// protected override void AddTags(Entity e)
// {
// }
//
// protected override void AddData(Entity e)
// {
// e.AddComponent(name);
// e.AddComponent(transform);
// e.AddComponent(rectangleCollider);
// }
//
// protected override void AddRender(Entity e)
// {
// e.AddComponent(_sprite);
// }
// }
63 changes: 0 additions & 63 deletions src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

This file was deleted.

32 changes: 32 additions & 0 deletions src/Libs/Entities/Factories/Characters/PlayerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Components.Data;
using Components.Render.Animation;
using Components.Tags;
using LightInject;
using Scellecs.Morpeh;
using Scellecs.Morpeh.Extended.Extensions;

namespace Entities.Factories.Characters;

public class PlayerFactory(IServiceFactory serviceFactory) : ConcreteEntityFactory
{
protected override void AddTags(Entity e)
{
e.AddComponent(serviceFactory.GetInstance<CameraComponent>());
e.AddComponent(serviceFactory.GetInstance<InputMovableComponent>());
e.AddComponent(serviceFactory.GetInstance<MovableComponent>());
}

protected override void AddData(Entity e)
{
e.AddComponent(serviceFactory.GetInstance<string, NameComponent>("Player"));
e.AddComponent(serviceFactory.GetInstance<TransformComponent>("PlayerEntity"));
e.AddComponent(serviceFactory.GetInstance<RectangleColliderComponent>("PlayerEntity"));
e.AddComponent(serviceFactory.GetInstance<InventoryComponent>());
}

protected override void AddRender(Entity e)
{
e.AddComponent(serviceFactory.GetInstance<MovementAnimationsComponent>("PlayerEntity"));
e.AddComponent(serviceFactory.GetInstance<CharacterAnimatorComponent>("PlayerEntity"));
}
}
26 changes: 26 additions & 0 deletions src/Libs/Entities/Factories/ConcreteEntityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using LDtk;
using Scellecs.Morpeh;

namespace Entities.Factories;

public abstract class ConcreteEntityFactory : IConcreteEntityFactory
{
public Entity CreateEntity(World @in)
{
Entity e = @in.CreateEntity();

AddTags(e);
AddData(e);
AddRender(e);

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);

protected abstract void AddRender(Entity e);
}
Loading

0 comments on commit 49a7345

Please sign in to comment.