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 5f26fd8
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 142 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Aseprite.Sprites;
using Services.Factories;
using Services.Builders;
using Services.Math;

[assembly: CompositionRootType(typeof(GameDesktop.CompositionRoots.FundamentalCompositionRoot))]
Expand All @@ -21,7 +21,6 @@ public void Compose(IServiceRegistry serviceRegistry)
{
RegisterLdtk(serviceRegistry);
RegisterAnimationsFactory(serviceRegistry);
serviceRegistry.RegisterSingleton(typeof(AbstractFactory<>));
}

private static void RegisterLdtk(IServiceRegistry serviceRegistry)
Expand All @@ -39,16 +38,19 @@ private static void RegisterLdtk(IServiceRegistry serviceRegistry)

private static void RegisterAnimationsFactory(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient<AsepriteAnimatedCharactersBuilder>();
// Warning: binding to <string, T> where T is any type, is dangerous and you should have a different
// binding off of implementation overloading, if you wanna pass through a string as an arg.
// So, such resolving won't work either: Func<string, T>, as it'll get it as your string argument is a
// service name. Thus, I use 3 type args here.
serviceRegistry.Register<string, string, Dictionary<Sector, AnimatedSprite>>((factory, path, action) =>
{
GraphicsDevice graphicsDevice = factory.GetInstance<GraphicsDeviceManager>().GraphicsDevice;
SpriteSheet spriteSheet = AnimatedCharactersFactory.LoadSpriteSheet(graphicsDevice, path);

return AnimatedCharactersFactory.CreateAnimations(spriteSheet, action);
return factory.GetInstance<AsepriteAnimatedCharactersBuilder>()
.LoadSpriteSheet(graphicsDevice, path)
.CreateAnimations(action)
.Animations;
}, "Character");
}
}
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) : IAbstractEntityFactory
{
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);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,37 @@
using MonoGame.Aseprite.Sprites;
using Services.Math;

namespace Services.Factories;
namespace Services.Builders;

public static class AnimatedCharactersFactory
public class AsepriteAnimatedCharactersBuilder
{
public Dictionary<Sector, AnimatedSprite>? Animations { get; private set; }

private SpriteSheet? _spriteSheet;

private static readonly IReadOnlyList<Sector> Directions =
new[] { Sector.Right, Sector.Down, Sector.Left, Sector.Up };

public static SpriteSheet LoadSpriteSheet(GraphicsDevice graphicsDevice, string path)
public AsepriteAnimatedCharactersBuilder LoadSpriteSheet(GraphicsDevice graphicsDevice, string path)
{
AsepriteFile asepriteFile = AsepriteFile.Load(path);
return SpriteSheetProcessor.Process(graphicsDevice, asepriteFile);
return new() { _spriteSheet = SpriteSheetProcessor.Process(graphicsDevice, asepriteFile) };
}

public AsepriteAnimatedCharactersBuilder CreateAnimations(string action)
{
if (_spriteSheet is null) throw new ArgumentNullException();

Dictionary<Sector, AnimatedSprite> dictionary =
Directions.ToDictionary(dir => dir, dir => CreateAnimation(_spriteSheet, action, dir));

// Temp hack
dictionary.Add(Sector.DownLeft, dictionary[Sector.Left]);
dictionary.Add(Sector.DownRight, dictionary[Sector.Right]);
dictionary.Add(Sector.UpLeft, dictionary[Sector.Left]);
dictionary.Add(Sector.UpRight, dictionary[Sector.Right]);

return new() { Animations = dictionary };
}

private static string BuildTag(string action, Sector dir) => $"{action}{dir.ToString()}";
Expand All @@ -23,7 +43,7 @@ private static AnimatedSprite CreateAnimation(SpriteSheet spriteSheet, string ac
{
AnimatedSprite animatedSprite;

if (direction == Sector.Left)
if (direction is Sector.Left)
{
string rightAnimationTag = BuildTag(action, Sector.Right);

Expand All @@ -40,18 +60,4 @@ private static AnimatedSprite CreateAnimation(SpriteSheet spriteSheet, string ac

return animatedSprite;
}

public static Dictionary<Sector, AnimatedSprite> CreateAnimations(SpriteSheet spriteSheet, string action)
{
Dictionary<Sector, AnimatedSprite> dictionary =
Directions.ToDictionary(dir => dir, dir => CreateAnimation(spriteSheet, action, dir));

// Temp hack
dictionary.Add(Sector.DownLeft, dictionary[Sector.Left]);
dictionary.Add(Sector.DownRight, dictionary[Sector.Right]);
dictionary.Add(Sector.UpLeft, dictionary[Sector.Left]);
dictionary.Add(Sector.UpRight, dictionary[Sector.Right]);

return dictionary;
}
}
6 changes: 0 additions & 6 deletions src/Libs/Services/Factories/AbstractFactory.cs

This file was deleted.

2 changes: 2 additions & 0 deletions src/Libs/Services/Movement/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Services.Movement;

public class SimpleMovement : IMovement
{
/// <param name="by">You won't see difference by passing computed values,
/// as the parameter is only for a unit vector (e.g. velocity)</param>
public Vector2 Move(Vector2 from, Vector2 by, float speed = 1)
{
if (by.Equals(Vector2.Zero)) return from;
Expand Down
Loading

0 comments on commit 5f26fd8

Please sign in to comment.