Skip to content

Commit

Permalink
refactor: components & debug structure
Browse files Browse the repository at this point in the history
- ignore dirty submodule
  • Loading branch information
cherrynik committed Feb 24, 2024
1 parent 462dba8 commit 9b9ec13
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "src/Libs/External/MonoGame.ImGuiNet"]
path = src/Libs/External/MonoGame.ImGuiNet
url = [email protected]:Mezo-hx/MonoGame.ImGuiNet.git
ignore = dirty
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private static void RegisterDataComponents(IServiceRegistry serviceRegistry)
{
RegisterTransformComponent(serviceRegistry);
RegisterRectangleCollisionComponent(serviceRegistry);
RegisterInventoryComponent(serviceRegistry);
}

private static void RegisterTagComponents(IServiceRegistry serviceRegistry)
Expand Down Expand Up @@ -110,6 +111,12 @@ private static void RegisterRectangleCollisionComponent(IServiceRegistry service
new RectangleCollisionComponent { Size = new(0, 0, 8, 8) });
}

private static void RegisterInventoryComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient(_ =>
new InventoryComponent([]));
}

private static void RegisterCharacterAnimatorComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(factory =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
factory.GetInstance<CameraComponent>(),
factory.GetInstance<RectangleCollisionComponent>(),
factory.GetInstance<MovementAnimationsComponent>(),
factory.GetInstance<CharacterAnimatorComponent>("PlayerEntity")));
factory.GetInstance<CharacterAnimatorComponent>("PlayerEntity"),
factory.GetInstance<InventoryComponent>()));
}
6 changes: 3 additions & 3 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public Game(ILogger logger, IServiceContainer container)
protected override void Initialize()
{
_logger.ForContext<Game>().Verbose($"Initialize(): start; available {GraphicsDevice}");
_logger.ForContext<Game>().Verbose("Circular dependencies initialization...");
_logger.ForContext<Game>().Verbose("Circular dependencies (external) initialization...");
RegisterSpriteBatch();
RegisterMyraUIEnvironment();
#if DEBUG
RegisterImGuiRenderer();
#endif
_logger.ForContext<Game>().Verbose("Circular dependencies initialized");
_logger.ForContext<Game>().Verbose("Circular dependencies (external) initialized");

_logger.ForContext<Game>().Verbose("Game services initialization...");
RegisterRootFeature();
Expand All @@ -73,7 +73,7 @@ protected override void LoadContent()

protected override void BeginRun()
{
_logger.ForContext<Game>().Verbose("Beginning run...");
_logger.ForContext<Game>().Verbose("Beginning to run...");

base.BeginRun();

Expand Down
4 changes: 3 additions & 1 deletion src/Apps/GameDesktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
using ServiceContainer container = new(
new ContainerOptions
{
EnablePropertyInjection = false,
EnableCurrentScope = false,
LogFactory = _ => entry => logger
.ForContext<ServiceContainer>()
.Verbose($"{entry.Message}")
.Verbose($"{entry.Message}"),
});

container.RegisterInstance<IServiceContainer>(container);
Expand Down
24 changes: 24 additions & 0 deletions src/Libs/Components/Data/InventoryComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Scellecs.Morpeh;

namespace Components.Data;

public interface IItem
{
ItemSettings ItemSettings { get; }
}

public struct ItemSettings(string name, bool isStackable)
{
public string Name = name;
public bool IsStackable = isStackable;
}

public struct Slot(IItem item)
{
public IItem Item = item;
}

public struct InventoryComponent(Slot[] slots) : IComponent
{
public Slot[] Slots = slots;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

namespace Components.Render.Animation;

public struct CharacterAnimatorComponent : IComponent
public struct CharacterAnimatorComponent(Direction facing, AnimatedSprite animation) : IComponent
{
public Direction Facing;
public AnimatedSprite Animation;

public CharacterAnimatorComponent(Direction facing, AnimatedSprite animation)
{
Facing = facing;
Animation = animation;
}
public Direction Facing = facing;
public AnimatedSprite Animation = animation;
}
31 changes: 13 additions & 18 deletions src/Libs/Components/Render/Animation/MovementAnimationsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@
// }
// }

namespace Components.Render.Animation
{
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;
using Services.Math;

public struct MovementAnimationsComponent : IComponent
{
public readonly Dictionary<Direction, AnimatedSprite> IdleAnimations;
public readonly Dictionary<Direction, AnimatedSprite> WalkingAnimations;
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;
using Services.Math;

public MovementAnimationsComponent(Dictionary<Direction, AnimatedSprite> idleAnimations,
Dictionary<Direction, AnimatedSprite> walkingAnimations)
{
IdleAnimations = idleAnimations;
WalkingAnimations = walkingAnimations;
}
}
namespace Components.Render.Animation;

// CharacterAnimatorComponent
public struct MovementAnimationsComponent(
Dictionary<Direction, AnimatedSprite> idleAnimations,
Dictionary<Direction, AnimatedSprite> walkingAnimations)
: IComponent
{
public readonly Dictionary<Direction, AnimatedSprite> IdleAnimations = idleAnimations;
public readonly Dictionary<Direction, AnimatedSprite> WalkingAnimations = walkingAnimations;
}

// CharacterAnimatorComponent
18 changes: 6 additions & 12 deletions src/Libs/Components/Render/Static/SpriteComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@
// }


namespace Components.Render.Static
{
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;

public struct SpriteComponent : IComponent
{
public Sprite Sprite { get; private set; }
namespace Components.Render.Static;

public SpriteComponent(Sprite sprite)
{
Sprite = sprite;
}
}
public struct SpriteComponent(Sprite sprite) : IComponent
{
public Sprite Sprite { get; private set; } = sprite;
}
9 changes: 2 additions & 7 deletions src/Libs/Components/Tags/CameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

namespace Components.Tags;

public struct CameraComponent : IComponent
public struct CameraComponent(Viewport viewport) : IComponent
{
public Viewport Viewport;
public Viewport Viewport = viewport;
public Vector2 Position;

public CameraComponent(Viewport viewport)
{
Viewport = viewport;
}
}
44 changes: 17 additions & 27 deletions src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,44 @@ namespace Entities.Factories.Characters;
// to debug entities and their components with ImGui.
// But also, for the factory, ig, the view of the entities class will change.
// For now, Imma keep it as it is.
public class PlayerEntityFactory : EntityFactory
public class PlayerEntityFactory(
InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleCollisionComponent rectangleCollision,
InventoryComponent inventoryComponent)
: EntityFactory
{
private readonly InputMovableComponent _inputMovable;
private readonly MovableComponent _movable;
private readonly TransformComponent _transform;
private readonly CameraComponent _cameraComponent;
private readonly RectangleCollisionComponent _rectangleCollision;
private readonly MovementAnimationsComponent _movementAnimations;
private readonly CharacterAnimatorComponent _characterAnimator;

public PlayerEntityFactory(InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleCollisionComponent rectangleCollision
)
{
_inputMovable = inputMovable;
_movable = movable;
_transform = transform;
_cameraComponent = cameraComponent;
_rectangleCollision = rectangleCollision;
}

public PlayerEntityFactory(InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleCollisionComponent rectangleCollision,
MovementAnimationsComponent movementAnimations,
CharacterAnimatorComponent characterAnimator) : this(inputMovable, movable, transform, cameraComponent,
rectangleCollision)
CharacterAnimatorComponent characterAnimator,
InventoryComponent inventoryComponent) : this(inputMovable, movable, transform, cameraComponent,
rectangleCollision, inventoryComponent)
{
_movementAnimations = movementAnimations;
_characterAnimator = characterAnimator;
}

protected override void AddTags(Entity e)
{
e.AddComponent(_cameraComponent);
e.AddComponent(_inputMovable);
e.AddComponent(_movable);
e.AddComponent(cameraComponent);
e.AddComponent(inputMovable);
e.AddComponent(movable);
}

protected override void AddData(Entity e)
{
e.AddComponent(_transform);
e.AddComponent(_rectangleCollision);
e.AddComponent(transform);
e.AddComponent(rectangleCollision);
e.AddComponent(inventoryComponent);
}

protected override void AddRender(Entity e)
Expand Down
34 changes: 33 additions & 1 deletion src/Libs/Systems.Debugging/EntitiesList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Components.Data;
using Components.Render.Animation;
using Components.Render.Static;
using Components.Tags;
using ImGuiNET;
using Scellecs.Morpeh;
using Scellecs.Morpeh.Extended;
Expand All @@ -8,6 +11,7 @@ namespace Systems.Debugging;
public class EntitiesList : IRenderSystem
{
public World World { set; get; }
private const float Indentation = 16.0f;

public EntitiesList(World world)
{
Expand All @@ -26,7 +30,35 @@ public void OnUpdate(float deltaTime)

if (ImGui.CollapsingHeader("Entities"))
{
foreach (Entity e in filter) ImGui.Text(e.ToString());
ImGui.Indent(Indentation);
foreach (Entity e in filter)
{
// TODO: By flag components I could decide what entity this is and show the proper name
if (!ImGui.CollapsingHeader($"Entity###{e.ID}")) // ### -> for identical values
{
continue;
}

ImGui.Indent(Indentation);

// TODO: collect all the components' names automatically
// TODO: use List<Type> types -> Has(typeof(T)) (with underlying Has<T>());
if (e.Has<InventoryComponent>()) ImGui.TextWrapped(nameof(InventoryComponent));
if (e.Has<TransformComponent>()) ImGui.TextWrapped(nameof(TransformComponent));
if (e.Has<CameraComponent>()) ImGui.TextWrapped(nameof(CameraComponent));
if (e.Has<RectangleCollisionComponent>()) ImGui.TextWrapped(nameof(RectangleCollisionComponent));
if (e.Has<CharacterAnimatorComponent>()) ImGui.TextWrapped(nameof(CharacterAnimatorComponent));
if (e.Has<MovementAnimationsComponent>()) ImGui.TextWrapped(nameof(MovementAnimationsComponent));
if (e.Has<SpriteComponent>()) ImGui.TextWrapped(nameof(SpriteComponent));
if (e.Has<InputMovableComponent>()) ImGui.TextWrapped(nameof(InputMovableComponent));
if (e.Has<MovableComponent>()) ImGui.TextWrapped(nameof(MovableComponent));
if (e.Has<RenderComponent>()) ImGui.TextWrapped(nameof(RenderComponent));
// TODO: menu for each component to edit the values

ImGui.Unindent(Indentation);
}

ImGui.Unindent(Indentation);
}

ImGui.End();
Expand Down
9 changes: 6 additions & 3 deletions src/UnitTests/UnitTests.Entities/PlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void PlayerEntity_IsCreatedInTheWorld()
new CameraComponent(),
new RectangleCollisionComponent(),
new MovementAnimationsComponent(),
new CharacterAnimatorComponent())
new CharacterAnimatorComponent(),
new InventoryComponent())
.CreateEntity(@in: _world);

{
Expand All @@ -58,7 +59,8 @@ public void PlayerEntity_HasComponents()
new CameraComponent(),
new RectangleCollisionComponent(),
new MovementAnimationsComponent(),
new CharacterAnimatorComponent())
new CharacterAnimatorComponent(),
new InventoryComponent())
.CreateEntity(@in: _world);
{
_world.TryGetEntity(playerEntity.ID, out Entity result);
Expand All @@ -80,7 +82,8 @@ public void WorldSystemAndEntityWorkTogether()
var rootFeature = new RootFeature(_world,
new WorldInitializer(_world, new WorldEntityFactory(new WorldComponent()),
new PlayerEntityFactory(new InputMovableComponent(), new MovableComponent(), new TransformComponent(),
new CameraComponent(new Viewport(0, 0, 640, 480)), new RectangleCollisionComponent()),
new CameraComponent(new Viewport(0, 0, 640, 480)), new RectangleCollisionComponent(),
new InventoryComponent()),
new DummyEntityFactory(new TransformComponent(), new RectangleCollisionComponent())),
new MovementFeature(_world, new InputSystem(_world, mockInputScanner.Object),
new MovementSystem(_world, new SimpleMovement())));
Expand Down

0 comments on commit 9b9ec13

Please sign in to comment.