Skip to content

Commit

Permalink
feat: load up textures & its regions for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Jun 20, 2024
1 parent 36e069f commit f91ea94
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -33,6 +36,31 @@ public void Compose(IServiceRegistry serviceRegistry)

private static void RegisterDataComponents(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<string, Texture2D>((factory, path) =>
{
var fileName = Path.Join(
Environment.GetEnvironmentVariable(EnvironmentVariable.AppBaseDirectory),
path
);
return Texture2D.FromFile(factory.GetInstance<GraphicsDeviceManager>().GraphicsDevice, fileName);
});

// TODO: Automatically get texture & its rect from a tile set (by aseprite?)
serviceRegistry.RegisterSingleton(factory =>
{
var texture = factory.GetInstance<string, Texture2D>("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<string, Texture2D>("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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +58,9 @@ public void Compose(IServiceRegistry serviceRegistry)
#endif
serviceRegistry.RegisterSingleton<PlayerFactory>();

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

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

Expand Down Expand Up @@ -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<Func<GameVersion>>(factory => factory.GetInstance<GameVersion>);

Expand Down
2 changes: 1 addition & 1 deletion src/Apps/GameDesktop/Content/TileMaps/Test.ldtk
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Libs/Entities/Factories/AbstractEntityFactory.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,9 +11,9 @@ public class AbstractEntityFactory(IServiceFactory serviceFactory) : IAbstractEn
{
private readonly Dictionary<string, IAbstractEntityFactory> _factories = new()
{
// { "Tree", null },
{ "Player", serviceFactory.GetInstance<PlayerFactory>() },
{ "Rock", serviceFactory.GetInstance<AbstractRockFactory>() },
{ "Tree", serviceFactory.GetInstance<AbstractTreeFactory>() },
// { "Default", null }
};

Expand Down
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> _createRock = new()
private readonly Dictionary<string, ConcreteEntityFactory> _factories = new()
{
{ "Pebble", serviceFactory.GetInstance<PebbleFactory>() },
};

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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Libs/Entities/Factories/Items/Rocks/PebbleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ protected override void AddData(Entity e)

protected override void AddRender(Entity e)
{
e.AddComponent(serviceProvider.GetInstance<SpriteComponent>("Player"));
// TODO: add the sprite of a rock
e.AddComponent(serviceProvider.GetInstance<SpriteComponent>("Pebble"));
}
}
19 changes: 19 additions & 0 deletions src/Libs/Entities/Factories/Items/Trees/AbstractTreeFactory.cs
Original file line number Diff line number Diff line change
@@ -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<string, ConcreteEntityFactory> _factories = new()
{
{ "Tree", serviceFactory.GetInstance<TreeFactory>() },
};

public Entity? CreateEntity(EntityInstance entity, World @in) =>
_factories.TryGetValue(entity._Identifier, out var factory)
? factory.CreateEntity(@in)
: null;
}
26 changes: 26 additions & 0 deletions src/Libs/Entities/Factories/Items/Trees/TreeFactory.cs
Original file line number Diff line number Diff line change
@@ -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<string, NameComponent>("Tree"));
e.AddComponent(serviceProvider.GetInstance<TransformComponent>());
// e.AddComponent(serviceProvider.GetInstance<ItemComponent>(ItemsTable.Items[ItemId.Rock].Name));
}

protected override void AddRender(Entity e)
{
e.AddComponent(serviceProvider.GetInstance<SpriteComponent>("Tree"));
}
}

0 comments on commit f91ea94

Please sign in to comment.