Skip to content

Commit

Permalink
refactor: basic di for root feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Nov 19, 2023
1 parent b7240c1 commit b9d065d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
using LightInject;
using Components.Data;
using Entities.Factories.Characters;
using Entities.Factories.Meta;
using Features;
using LightInject;
using GameDesktop.CompositionRoots.Components;
using GameDesktop.CompositionRoots.Entities;
using Implementations;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Scellecs.Morpeh;
using Services.Movement;
using Systems;
using Systems.Debugging;
using Systems.Debugging.Render;
using Systems.Render;
#if DEBUG
using GameDesktop.CompositionRoots.DebugFeatures;
#endif
Expand Down Expand Up @@ -29,7 +42,7 @@ public void Compose(IServiceRegistry serviceRegistry)
serviceRegistry.RegisterFrom<DebugRootFeatureCompositionRoot>();
#endif

// RegisterEntryPoint(serviceRegistry);
RegisterEntryPoint(serviceRegistry);
}

private static void RegisterFundamental(IServiceRegistry serviceRegistry) =>
Expand All @@ -52,6 +65,37 @@ private static void RegisterFeatures(IServiceRegistry serviceRegistry)
// serviceRegistry.RegisterFrom<MovementFeatureCompositionRoot>();
}

// private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) =>
private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(_ => World.Create());

serviceRegistry.RegisterSingleton(factory =>
{
Texture2D pixel = new(factory.GetInstance<SpriteBatch>().GraphicsDevice, 1, 1);
pixel.SetData(new[] { Color.Gold });

return new RootFeature(factory.GetInstance<World>(),
new WorldInitializer(factory.GetInstance<World>(), new WorldEntityFactory(new WorldComponent()),
factory.GetInstance<PlayerEntityFactory>(),
factory.GetInstance<DummyEntityFactory>()),
new MovementFeature(factory.GetInstance<World>(),
new InputSystem(factory.GetInstance<World>(), new KeyboardInput()),
new MovementSystem(factory.GetInstance<World>(), new SimpleMovement())),
new PreRenderFeature(factory.GetInstance<World>(),
new CharacterMovementAnimationSystem(factory.GetInstance<World>()),
new CameraFollowingSystem(factory.GetInstance<World>())),
new RenderFeature(factory.GetInstance<World>(),
new RenderCharacterMovementAnimationSystem(factory.GetInstance<World>(),
factory.GetInstance<SpriteBatch>()))
#if DEBUG
,
new DebugFeature(factory.GetInstance<World>(), new EntitiesList(factory.GetInstance<World>()),
new FrameCounter(factory.GetInstance<World>()),
new RenderFramesPerSec(factory.GetInstance<World>()),
new PivotRenderSystem(factory.GetInstance<World>(), factory.GetInstance<SpriteBatch>(), pixel))
#endif
);
});
}
// serviceRegistry.RegisterSingleton<RootFeature>();
}
21 changes: 1 addition & 20 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,7 @@ protected override void LoadContent()
// _desktop.Root = grid;
// ------

World world = World.Create();

_rootFeature = new RootFeature(world,
new WorldInitializer(world, new WorldEntityFactory(new WorldComponent()),
_container.GetInstance<PlayerEntityFactory>(),
_container.GetInstance<DummyEntityFactory>()),
new MovementFeature(world,
new InputSystem(world, new KeyboardInput()),
new MovementSystem(world, new SimpleMovement())),
new PreRenderFeature(world,
new CharacterMovementAnimationSystem(world),
new CameraFollowingSystem(world)),
new RenderFeature(world,
new RenderCharacterMovementAnimationSystem(world, _spriteBatch))
#if DEBUG
,
new DebugFeature(world, new EntitiesList(world), new FrameCounter(world), new RenderFramesPerSec(world),
new PivotRenderSystem(world, _spriteBatch, pixel))
#endif
);
_rootFeature = _container.GetInstance<RootFeature>();

_rootFeature.OnAwake();

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 @@ -95,7 +95,7 @@ public void WorldSystemAndEntityWorkTogether()
Assert.That(transform.Velocity.X, Is.EqualTo(0));
Assert.That(transform.Position.Y, Is.EqualTo(0));

rootFeature.OnUpdate(16.6f);
rootFeature.OnUpdate(.0f);

Assert.That(transform.Velocity.X, Is.EqualTo(1));
// Assert.That(transform.Position.Y, Is.EqualTo(0));
Expand Down

0 comments on commit b9d065d

Please sign in to comment.