From 329a942de7cf744bb831bb0b68694e0489844696 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Mon, 13 Oct 2025 18:29:58 +0200 Subject: [PATCH] Add inventory and pickup components with associated resources and presenters --- Code/Factories/ComponentFactory.cs | 13 ++++++ Code/Presenters/GamePresenter.cs | 3 ++ Code/Presenters/PickupPresenter.cs | 43 ++++++++++++++++++++ Code/Resources/InventoryComponentResource.cs | 9 ++++ Code/Resources/PickupComponentResource.cs | 11 +++++ 5 files changed, 79 insertions(+) create mode 100644 Code/Presenters/PickupPresenter.cs create mode 100644 Code/Resources/InventoryComponentResource.cs create mode 100644 Code/Resources/PickupComponentResource.cs diff --git a/Code/Factories/ComponentFactory.cs b/Code/Factories/ComponentFactory.cs index df4bc22..458b11d 100644 --- a/Code/Factories/ComponentFactory.cs +++ b/Code/Factories/ComponentFactory.cs @@ -6,6 +6,7 @@ using GameCore.Attributes; using GameCore.Combat; using GameCore.ECS.Interfaces; using GameCore.Input; +using GameCore.Inventory; using GameCore.Movement; using GameCore.Physics; using GameCore.Player; @@ -29,6 +30,8 @@ public class ComponentFactory Register(_ => new PlayerComponent()); Register(_ => new RotationComponent()); Register(_ => new CharacterStateComponent()); + Register(_ => new InventoryComponent()); + Register(CreatePickupComponent); } public IComponent Create(Resource resource) @@ -77,4 +80,14 @@ public class ComponentFactory Lifetime = resource.Lifetime, }; } + + private PickupComponent CreatePickupComponent(PickupComponentResource resource) + { + return new PickupComponent + { + ItemId = resource.ItemId, + Quantity = resource.Quantity, + IsInstantUse = resource.IsInstantUse + }; + } } \ No newline at end of file diff --git a/Code/Presenters/GamePresenter.cs b/Code/Presenters/GamePresenter.cs index bfb71ba..2f82626 100644 --- a/Code/Presenters/GamePresenter.cs +++ b/Code/Presenters/GamePresenter.cs @@ -10,6 +10,7 @@ using GameCore.ECS; using GameCore.ECS.Interfaces; using GameCore.Events; using GameCore.Input; +using GameCore.Inventory; using GameCore.Movement; using Godot; @@ -54,6 +55,8 @@ public partial class GamePresenter : Node _world.RegisterSystem(new JumpSystem()); _world.RegisterSystem(new AttributeSystem()); + _world.RegisterSystem(new PickupSystem()); + _world.RegisterSystem(new InventorySystem(_world)); _world.RegisterSystem(new WeaponSystem()); _world.RegisterSystem(new ProjectileSystem()); diff --git a/Code/Presenters/PickupPresenter.cs b/Code/Presenters/PickupPresenter.cs new file mode 100644 index 0000000..a8959bb --- /dev/null +++ b/Code/Presenters/PickupPresenter.cs @@ -0,0 +1,43 @@ +using CryptonymThunder.Code.Autoloads; +using GameCore.ECS; +using GameCore.ECS.Interfaces; +using GameCore.Physics; +using Godot; + +namespace CryptonymThunder.Code.Presenters; + +public partial class PickupPresenter : Area3D, IEntityPresenter, IPresenterComponent +{ + private World _world; + private PresenterRegistry _presenterRegistry; + public Entity CoreEntity { get; set; } + + public override void _Ready() + { + BodyEntered += OnBodyEnter; + } + + private void OnBodyEnter(Node3D body) + { + if (_presenterRegistry.TryGetEntity(body.GetInstanceId(), out var hitEntity)) + { + _world.AddComponent(hitEntity, new CollisionEventComponent(CoreEntity)); + } + } + + public void Initialize(Entity coreEntity, World world) + { + CoreEntity = coreEntity; + _world = world; + _presenterRegistry = GetNode("/root/PresenterRegistry"); + BodyEntered += OnBodyEnter; + } + + public void SyncToPresentation(float delta) + { + } + + public void SyncToCore(float delta) + { + } +} \ No newline at end of file diff --git a/Code/Resources/InventoryComponentResource.cs b/Code/Resources/InventoryComponentResource.cs new file mode 100644 index 0000000..cdcd51d --- /dev/null +++ b/Code/Resources/InventoryComponentResource.cs @@ -0,0 +1,9 @@ +using Godot; + +namespace CryptonymThunder.Code.Resources; + +[GlobalClass] +public partial class InventoryComponentResource : Resource +{ + +} \ No newline at end of file diff --git a/Code/Resources/PickupComponentResource.cs b/Code/Resources/PickupComponentResource.cs new file mode 100644 index 0000000..088b5db --- /dev/null +++ b/Code/Resources/PickupComponentResource.cs @@ -0,0 +1,11 @@ +using Godot; + +namespace CryptonymThunder.Code.Resources; + +[GlobalClass] +public partial class PickupComponentResource : Resource +{ + [Export] public string ItemId { get; set; } + [Export] public int Quantity { get; set; } = 1; + [Export] public bool IsInstantUse { get; set; } = false; +} \ No newline at end of file