Skip to content

Commit

Permalink
refactor: wip. composition roots
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Jul 6, 2024
1 parent 35e0711 commit f155eb9
Show file tree
Hide file tree
Showing 44 changed files with 532 additions and 350 deletions.
2 changes: 1 addition & 1 deletion src/Libs/Components/Data/CameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Components.Data;

// Camera is a system and not a component, so the system has a target as a dependency,
// The Camera is a system and not a component, so the system has a target as a dependency,
// and exists only in the current world, as well the system has its own behaviour
public struct CameraComponent(Viewport viewport) : IComponent
{
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/Components/Data/TransformComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Numerics;
using Scellecs.Morpeh;
using Services.Math;
using Services.Implementations.Math;

namespace Components.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Components.Data;
using MonoGame.Aseprite.Sprites;
using Scellecs.Morpeh;
using Services.Math;
using Services.Implementations.Math;

namespace Components.Render.Animation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;
using Services.Math;
using Services.Implementations.Math;

namespace Components.Render.Animation;

Expand Down
12 changes: 12 additions & 0 deletions src/Libs/CompositionRoots/Components/Data/ItemModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Components.Data;
using LightInject;

namespace CompositionRoots.Components.Data;

public class ItemModule : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(_ => new ItemComponent(ItemId.Rock), ItemsTable.Items[ItemId.Rock].Name);
}
}
12 changes: 12 additions & 0 deletions src/Libs/CompositionRoots/Components/Data/NameModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Components.Data;
using LightInject;

namespace CompositionRoots.Components.Data;

public class NameModule : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<string, NameComponent>((_, name) => new NameComponent(name));
}
}
15 changes: 15 additions & 0 deletions src/Libs/CompositionRoots/Components/Data/TransformModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Components.Data;
using CompositionRoots.Components.Data;
using LightInject;

[assembly: CompositionRootType(typeof(TransformModule))]

namespace CompositionRoots.Components.Data;

public class TransformModule : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient(_ => new TransformComponent());
}
}
202 changes: 0 additions & 202 deletions src/Libs/CompositionRoots/ComponentsCompositionRoot.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/Libs/CompositionRoots/Entities/CameraModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Components.Data;
using Constants;
using LightInject;
using Microsoft.Xna.Framework.Graphics;

namespace CompositionRoots.Entities;

public class CameraModule : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(_ => new Viewport(0, 0, 801, 480), DINames.Camera);
serviceRegistry.RegisterSingleton(factory =>
new CameraComponent(factory.GetInstance<Viewport>(DINames.Camera)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CompositionRoots.Entities;
using Entities.Factories;
using Entities.Factories.Items.Rocks;
using Entities.Factories.Items.Trees;
using LightInject;

[assembly: CompositionRootType(typeof(EntityFactoriesCompositionRoot))]

namespace CompositionRoots.Entities;

public class EntityFactoriesCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterFrom<WorldModule>();
serviceRegistry.RegisterFrom<PlayerModule>();

serviceRegistry.RegisterSingleton<TreeFactory>();
serviceRegistry.RegisterSingleton<AbstractTreeFactory>();

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

serviceRegistry.RegisterSingleton<EntitiesFactory>();
}
}
Loading

0 comments on commit f155eb9

Please sign in to comment.