diff --git a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs index bba1b86..36b838c 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; +using System.IO; using Components.Data; using Components.Render.Animation; using Components.Render.Static; using Components.Tags; using GameDesktop.Resources.Internal; using LightInject; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Aseprite; using MonoGame.Aseprite.Sprites; using Services.Math; @@ -33,6 +36,31 @@ public void Compose(IServiceRegistry serviceRegistry) private static void RegisterDataComponents(IServiceRegistry serviceRegistry) { + serviceRegistry.Register((factory, path) => + { + var fileName = Path.Join( + Environment.GetEnvironmentVariable(EnvironmentVariable.AppBaseDirectory), + path + ); + return Texture2D.FromFile(factory.GetInstance().GraphicsDevice, fileName); + }); + + // TODO: Automatically get texture & its rect from a tile set (by aseprite?) + serviceRegistry.RegisterSingleton(factory => + { + var texture = factory.GetInstance("Content/SpriteSheets/Main.png"); + var sprite = new Sprite("Pebble", new TextureRegion("Pebble", texture, new(208, 48, 16, 16))); + + return new SpriteComponent(sprite); + }, "Pebble"); + + serviceRegistry.RegisterSingleton(factory => + { + var texture = factory.GetInstance("Content/SpriteSheets/Main.png"); + var sprite = new Sprite("Tree", new TextureRegion("Tree", texture, new(144, 0, 48, 96))); + + return new SpriteComponent(sprite); + }, "Tree"); RegisterTransformComponent(serviceRegistry); RegisterRectangleColliderComponent(serviceRegistry); RegisterItemComponent(serviceRegistry); diff --git a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs index 92d0a83..aca6e8d 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs @@ -5,6 +5,7 @@ using Entities.Factories.Characters; using Entities.Factories.Items; using Entities.Factories.Items.Rocks; +using Entities.Factories.Items.Trees; using Entities.Factories.Meta; using Features; using FontStashSharp.RichText; @@ -57,6 +58,9 @@ public void Compose(IServiceRegistry serviceRegistry) #endif serviceRegistry.RegisterSingleton(); + serviceRegistry.RegisterSingleton(); + serviceRegistry.RegisterSingleton(); + serviceRegistry.RegisterSingleton(); serviceRegistry.RegisterSingleton(); @@ -182,7 +186,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) Left = -30, Top = -20, TextAlign = TextHorizontalAlignment.Right, - Text = "Pre-alpha v0.3.0" + Text = "Pre-alpha v0.3.2" })); serviceRegistry.RegisterSingleton>(factory => factory.GetInstance); diff --git a/src/Apps/GameDesktop/Content/TileMaps/Test.ldtk b/src/Apps/GameDesktop/Content/TileMaps/Test.ldtk index 1821e52..34de78b 100644 --- a/src/Apps/GameDesktop/Content/TileMaps/Test.ldtk +++ b/src/Apps/GameDesktop/Content/TileMaps/Test.ldtk @@ -11,7 +11,7 @@ "iid": "78186310-25d0-11ef-a4b2-f58f8a399958", "jsonVersion": "1.5.3", "appBuildId": 473703, - "nextUid": 168, + "nextUid": 170, "identifierStyle": "Capitalize", "toc": [], "worldLayout": null, diff --git a/src/Libs/Entities/Factories/AbstractEntityFactory.cs b/src/Libs/Entities/Factories/AbstractEntityFactory.cs index 1f8d2aa..59bfcd7 100644 --- a/src/Libs/Entities/Factories/AbstractEntityFactory.cs +++ b/src/Libs/Entities/Factories/AbstractEntityFactory.cs @@ -1,5 +1,6 @@ using Entities.Factories.Characters; using Entities.Factories.Items.Rocks; +using Entities.Factories.Items.Trees; using LDtk; using LightInject; using Scellecs.Morpeh; @@ -10,9 +11,9 @@ public class AbstractEntityFactory(IServiceFactory serviceFactory) : IAbstractEn { private readonly Dictionary _factories = new() { - // { "Tree", null }, { "Player", serviceFactory.GetInstance() }, { "Rock", serviceFactory.GetInstance() }, + { "Tree", serviceFactory.GetInstance() }, // { "Default", null } }; diff --git a/src/Libs/Entities/Factories/Items/Rocks/AbstractRockFactory.cs b/src/Libs/Entities/Factories/Items/Rocks/AbstractRockFactory.cs index 71a3859..0d7063e 100644 --- a/src/Libs/Entities/Factories/Items/Rocks/AbstractRockFactory.cs +++ b/src/Libs/Entities/Factories/Items/Rocks/AbstractRockFactory.cs @@ -6,13 +6,13 @@ namespace Entities.Factories.Items.Rocks; public class AbstractRockFactory(IServiceFactory serviceFactory) : IAbstractEntityFactory { - private readonly Dictionary _createRock = new() + private readonly Dictionary _factories = new() { { "Pebble", serviceFactory.GetInstance() }, }; public Entity? CreateEntity(EntityInstance entity, World @in) => - _createRock.TryGetValue(entity._Identifier, out var factory) + _factories.TryGetValue(entity._Identifier, out var factory) ? factory.CreateEntity(@in) : null; } diff --git a/src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs b/src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs index 3d572e0..6a29f38 100644 --- a/src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs +++ b/src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs @@ -21,7 +21,6 @@ protected override void AddData(Entity e) protected override void AddRender(Entity e) { - e.AddComponent(serviceProvider.GetInstance("Player")); - // TODO: add the sprite of a rock + e.AddComponent(serviceProvider.GetInstance("Pebble")); } } diff --git a/src/Libs/Entities/Factories/Items/Trees/AbstractTreeFactory.cs b/src/Libs/Entities/Factories/Items/Trees/AbstractTreeFactory.cs new file mode 100644 index 0000000..8b3778d --- /dev/null +++ b/src/Libs/Entities/Factories/Items/Trees/AbstractTreeFactory.cs @@ -0,0 +1,19 @@ +using Entities.Factories.Items.Rocks; +using LDtk; +using LightInject; +using Scellecs.Morpeh; + +namespace Entities.Factories.Items.Trees; + +public class AbstractTreeFactory(IServiceFactory serviceFactory) : IAbstractEntityFactory +{ + private readonly Dictionary _factories = new() + { + { "Tree", serviceFactory.GetInstance() }, + }; + + public Entity? CreateEntity(EntityInstance entity, World @in) => + _factories.TryGetValue(entity._Identifier, out var factory) + ? factory.CreateEntity(@in) + : null; +} diff --git a/src/Libs/Entities/Factories/Items/Trees/TreeFactory.cs b/src/Libs/Entities/Factories/Items/Trees/TreeFactory.cs new file mode 100644 index 0000000..d1048f1 --- /dev/null +++ b/src/Libs/Entities/Factories/Items/Trees/TreeFactory.cs @@ -0,0 +1,26 @@ +using Components.Data; +using Components.Render.Static; +using LightInject; +using Scellecs.Morpeh; +using Scellecs.Morpeh.Extended.Extensions; + +namespace Entities.Factories.Items.Trees; + +public class TreeFactory(IServiceFactory serviceProvider) : ConcreteEntityFactory +{ + protected override void AddTags(Entity e) + { + } + + protected override void AddData(Entity e) + { + e.AddComponent(serviceProvider.GetInstance("Tree")); + e.AddComponent(serviceProvider.GetInstance()); + // e.AddComponent(serviceProvider.GetInstance(ItemsTable.Items[ItemId.Rock].Name)); + } + + protected override void AddRender(Entity e) + { + e.AddComponent(serviceProvider.GetInstance("Tree")); + } +}