Skip to content

Commit

Permalink
feat: debugging improvements + code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Feb 26, 2024
1 parent 83060a4 commit 6d441df
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ private static void RegisterDataComponents(IServiceRegistry serviceRegistry)
private static void RegisterItemComponent(IServiceRegistry serviceRegistry)
{
// TODO: make this accessible globally? So, the name, etc. of an item are reused between classes easily
Dictionary<ItemId, Item> items = new()
{
{ ItemId.Rock, new Item(name: "Rock", isStackable: true, maximumInStack: 16) }
};
// Dictionary<ItemId, Item> items = new()
// {
// { ItemId.Rock, new Item(name: "Rock", isStackable: true, maximumInStack: 16) }
// };

serviceRegistry.RegisterSingleton(_ => new ItemComponent(ItemId.Rock), "Rock");
serviceRegistry.RegisterSingleton(_ => new ItemComponent(ItemId.Rock), ItemsTable.Items[ItemId.Rock].Name);
}

private static void RegisterTagComponents(IServiceRegistry serviceRegistry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void Compose(IServiceRegistry serviceRegistry)

private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new PlayerEntityFactory(
new NameComponent("Player"), // factory.GetInstance<NameComponent>("Player"),
factory.GetInstance<InputMovableComponent>(),
factory.GetInstance<MovableComponent>(),
factory.GetInstance<TransformComponent>("PlayerEntity"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
using Scellecs.Morpeh;
using Services.Movement;
using Systems;
using Systems.Debugging;
using Systems.Debugging.Render;
using Systems.Render;
#if DEBUG
using Features.Debugging;
using Systems.Debugging;
using Systems.Debugging.Render;
using GameDesktop.CompositionRoots.DebugFeatures;
#endif

Expand Down Expand Up @@ -104,7 +105,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
pixel.SetData(new[] { Color.Gold });

return new RootFeature(factory.GetInstance<World>(),
new WorldInitializer(factory.GetInstance<World>(), new WorldEntityFactory(new WorldComponent()),
new WorldInitializer(factory.GetInstance<World>(), new WorldEntityFactory(new WorldMetaComponent()),
factory.GetInstance<PlayerEntityFactory>(),
factory.GetInstance<DummyEntityFactory>(),
factory.GetInstance<RockEntityFactory>()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void Compose(IServiceRegistry serviceRegistry)

private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new DummyEntityFactory(
new NameComponent("Dummy"), // factory.GetInstance<NameComponent>("Dummy")
factory.GetInstance<TransformComponent>("DummyEntity"),
factory.GetInstance<SpriteComponent>(),
factory.GetInstance<RectangleCollisionComponent>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void Compose(IServiceRegistry serviceRegistry)

private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient(factory => new RockEntityFactory(
new NameComponent("Rock"), // factory.GetInstance<NameComponent>("Rock")
factory.GetInstance<ItemComponent>("Rock"),
factory.GetInstance<TransformComponent>("RockEntity"),
factory.GetInstance<SpriteComponent>() //factory.GetInstance<SpriteComponent>("Rock")
Expand Down
22 changes: 12 additions & 10 deletions src/Libs/Components/Data/CameraComponent.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// namespace Entitas.Components.Data;
//
// [Unique]
// public class CameraComponent : IComponent
// {
// public Rectangle Size;
// }
using System.Numerics;
using Microsoft.Xna.Framework.Graphics;
using Scellecs.Morpeh;

// Camera is a system but a component,
namespace Components.Data;

// TODO: Camera is a system and not a component,
// So, the system would have a target as a dependency,
// And exist only in the current world,
// As well, the system has its own behaviour,
// Thus, there's no reason to migrate the component
// As well, the system has its own behaviour
public struct CameraComponent(Viewport viewport) : IComponent
{
public Viewport Viewport = viewport;
public Vector2 Position;
}
30 changes: 27 additions & 3 deletions src/Libs/Components/Data/ItemComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Components.Data;

// TODO: refactor here a lil bit
public enum ItemId
{
Rock = 0,
Expand All @@ -12,9 +13,32 @@ public readonly struct ItemComponent(ItemId itemId) : IComponent
public ItemId ItemId { get; } = itemId;
}

public readonly struct Item(string name, bool isStackable, int? maximumInStack)
public readonly struct Item(string name)
{
public string Name { get; } = name;
public bool IsStackable { get; } = isStackable;
public int MaximumInStack { get; } = maximumInStack ?? 1;
public int MaximumInStack { get; } = 1;
public bool IsStackable { get; } = false;

public Item(string name, int maximumInStack) : this(name)
{
Name = name;
MaximumInStack = maximumInStack;

IsStackable = maximumInStack switch
{
> 1 => true,
< 1 => throw new ArgumentOutOfRangeException(nameof(maximumInStack), maximumInStack,
"Has to be >= 1"),
_ => IsStackable
};
}
}

// TODO: put in data tables namespace (?)
public static class ItemsTable
{
public static readonly Dictionary<ItemId, Item> Items = new()
{
{ ItemId.Rock, new Item(name: "Rock", maximumInStack: 16) }
};
}
8 changes: 8 additions & 0 deletions src/Libs/Components/Data/NameComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Scellecs.Morpeh;

namespace Components.Data;

public readonly struct NameComponent(string name) : IComponent
{
public string Name { get; } = name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Components.Data;

public struct WorldComponent() : IComponent
public struct WorldMetaComponent() : IComponent
{
public float FramesPerSec;
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
// namespace Entitas.Components.Data
// {
// public class MovementAnimationComponent : RenderComponent
// {
// private const Direction DefaultFacing = Direction.Down;
//
// public AnimatedSprite? PlayingAnimation;
// public Dictionary<Direction, AnimatedSprite>? IdleAnimations;
// public Dictionary<Direction, AnimatedSprite>? WalkingAnimations;
//
// public Vector2 FacingDirection = Vector2.UnitY;
// public bool HasStopped;
//
// public MovementAnimationComponent()
// {
// }
//
// public MovementAnimationComponent(Dictionary<Direction, AnimatedSprite> idleAnimations,
// Dictionary<Direction, AnimatedSprite> walkingAnimations)
// {
// IdleAnimations = idleAnimations;
// WalkingAnimations = walkingAnimations;
//
// PlayingAnimation = idleAnimations[DefaultFacing];
// }
// }
// }

using Scellecs.Morpeh;
using Scellecs.Morpeh;
using MonoGame.Aseprite.Sprites;
using Services.Math;

Expand All @@ -40,5 +12,3 @@ public struct MovementAnimationsComponent(
public readonly Dictionary<Direction, AnimatedSprite> IdleAnimations = idleAnimations;
public readonly Dictionary<Direction, AnimatedSprite> WalkingAnimations = walkingAnimations;
}

// CharacterAnimatorComponent
11 changes: 0 additions & 11 deletions src/Libs/Components/Tags/CameraComponent.cs

This file was deleted.

4 changes: 1 addition & 3 deletions src/Libs/Components/Tags/InputMovableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Components.Tags;

public struct InputMovableComponent : IComponent
{
}
public struct InputMovableComponent : IComponent;
4 changes: 1 addition & 3 deletions src/Libs/Components/Tags/MovableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Components.Tags;

public struct MovableComponent : IComponent
{
}
public struct MovableComponent : IComponent;
18 changes: 0 additions & 18 deletions src/Libs/Components/Tags/RenderComponent.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/Libs/Components/Tags/RenderableComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Scellecs.Morpeh;

namespace Components.Tags;

public struct RenderableComponent : IComponent;
26 changes: 12 additions & 14 deletions src/Libs/Entities/Factories/Characters/DummyEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@

namespace Entities.Factories.Characters;

public class DummyEntityFactory : EntityFactory
public class DummyEntityFactory(
NameComponent name,
TransformComponent transform,
RectangleCollisionComponent rectangleCollision)
: EntityFactory
{
private readonly TransformComponent _transform;
private readonly SpriteComponent _sprite;
private readonly RectangleCollisionComponent _rectangleCollision;

public DummyEntityFactory(TransformComponent transform,
RectangleCollisionComponent rectangleCollision)
{
_transform = transform;
_rectangleCollision = rectangleCollision;
}

public DummyEntityFactory(TransformComponent transform,
public DummyEntityFactory(
NameComponent name,
TransformComponent transform,
SpriteComponent sprite,
RectangleCollisionComponent rectangleCollision) : this(transform, rectangleCollision)
RectangleCollisionComponent rectangleCollision) : this(name, transform, rectangleCollision)
{
_sprite = sprite;
}
Expand All @@ -31,8 +28,9 @@ protected override void AddTags(Entity e)

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

protected override void AddRender(Entity e)
Expand Down
8 changes: 6 additions & 2 deletions src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Entities.Factories.Characters;
// 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(
NameComponent nameComponent,
InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
Expand All @@ -23,14 +24,16 @@ public class PlayerEntityFactory(
private readonly MovementAnimationsComponent _movementAnimations;
private readonly CharacterAnimatorComponent _characterAnimator;

public PlayerEntityFactory(InputMovableComponent inputMovable,
public PlayerEntityFactory(
NameComponent nameComponent,
InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleCollisionComponent rectangleCollision,
MovementAnimationsComponent movementAnimations,
CharacterAnimatorComponent characterAnimator,
InventoryComponent inventoryComponent) : this(inputMovable, movable, transform, cameraComponent,
InventoryComponent inventoryComponent) : this(nameComponent, inputMovable, movable, transform, cameraComponent,
rectangleCollision, inventoryComponent)
{
_movementAnimations = movementAnimations;
Expand All @@ -46,6 +49,7 @@ protected override void AddTags(Entity e)

protected override void AddData(Entity e)
{
e.AddComponent(nameComponent);
e.AddComponent(transform);
e.AddComponent(rectangleCollision);
e.AddComponent(inventoryComponent);
Expand Down
7 changes: 5 additions & 2 deletions src/Libs/Entities/Factories/Items/RockEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
namespace Entities.Factories.Items;

public class RockEntityFactory(
NameComponent nameComponent,
ItemComponent itemComponent,
TransformComponent transformComponent) : EntityFactory
{
private readonly SpriteComponent _spriteComponent;

public RockEntityFactory(ItemComponent itemComponent, TransformComponent transformComponent,
SpriteComponent spriteComponent) : this(itemComponent, transformComponent)
public RockEntityFactory(NameComponent nameComponent, ItemComponent itemComponent,
TransformComponent transformComponent,
SpriteComponent spriteComponent) : this(nameComponent, itemComponent, transformComponent)
{
_spriteComponent = spriteComponent;
}
Expand All @@ -23,6 +25,7 @@ protected override void AddTags(Entity e)

protected override void AddData(Entity e)
{
e.AddComponent(nameComponent);
e.AddComponent(itemComponent);
e.AddComponent(transformComponent);
}
Expand Down
11 changes: 2 additions & 9 deletions src/Libs/Entities/Factories/Meta/WorldEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@

namespace Entities.Factories.Meta;

public class WorldEntityFactory : EntityFactory
public class WorldEntityFactory(WorldMetaComponent worldMetaComponent) : EntityFactory
{
private readonly WorldComponent _worldComponent;

public WorldEntityFactory(WorldComponent worldComponent)
{
_worldComponent = worldComponent;
}

protected override void AddTags(Entity e)
{
}

protected override void AddData(Entity e)
{
e.AddComponent(_worldComponent);
e.AddComponent(worldMetaComponent);
}

protected override void AddRender(Entity e)
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/Features.Debugging/DebugFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Systems.Debugging;
using Systems.Debugging.Render;

namespace GameDesktop;
namespace Features.Debugging;

public class DebugFeature : Feature
{
Expand Down
Loading

0 comments on commit 6d441df

Please sign in to comment.