Implement inventory system with item management and pickup functionality
This commit is contained in:
@@ -27,6 +27,8 @@ public class WeaponSystem : ISystem
|
|||||||
|
|
||||||
if (input.IsFiring && weapon.CooldownTimer <= 0f)
|
if (input.IsFiring && weapon.CooldownTimer <= 0f)
|
||||||
{
|
{
|
||||||
|
// Check for ammo if applicable
|
||||||
|
|
||||||
var context = new EffectContext { World = world, Owner = entity };
|
var context = new EffectContext { World = world, Owner = entity };
|
||||||
|
|
||||||
foreach (var effect in weapon.OnFireEffects) effect.Execute(context);
|
foreach (var effect in weapon.OnFireEffects) effect.Execute(context);
|
||||||
|
|||||||
11
GameCore/Events/AddItemToInventoryEvent.cs
Normal file
11
GameCore/Events/AddItemToInventoryEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.Events.Interfaces;
|
||||||
|
using GameCore.Inventory;
|
||||||
|
|
||||||
|
namespace GameCore.Events;
|
||||||
|
|
||||||
|
public readonly struct AddItemToInventoryEvent(Entity target, Item item) : IEvent
|
||||||
|
{
|
||||||
|
public readonly Entity Target = target;
|
||||||
|
public readonly Item Item = item;
|
||||||
|
}
|
||||||
7
GameCore/Events/PlayerJumpEvent.cs
Normal file
7
GameCore/Events/PlayerJumpEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using GameCore.Events.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Events;
|
||||||
|
|
||||||
|
public readonly struct PlayerJumpEvent : IEvent
|
||||||
|
{
|
||||||
|
}
|
||||||
46
GameCore/Inventory/InventoryComponent.cs
Normal file
46
GameCore/Inventory/InventoryComponent.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public class InventoryComponent : IComponent
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, Item> _items = new();
|
||||||
|
|
||||||
|
public bool AddItem(Item itemToAdd)
|
||||||
|
{
|
||||||
|
if (_items.TryGetValue(itemToAdd.ItemId, out var exisitingItem))
|
||||||
|
{
|
||||||
|
exisitingItem.Quantity += itemToAdd.Quantity;
|
||||||
|
_items[itemToAdd.ItemId] = exisitingItem;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_items.Add(itemToAdd.ItemId, itemToAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveItem(string itemId, int quantity)
|
||||||
|
{
|
||||||
|
if (!_items.TryGetValue(itemId, out var existingItem) || existingItem.Quantity < quantity) return false;
|
||||||
|
|
||||||
|
existingItem.Quantity -= quantity;
|
||||||
|
if (existingItem.Quantity <= 0)
|
||||||
|
_items.Remove(itemId);
|
||||||
|
else
|
||||||
|
_items[itemId] = existingItem;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetItemCount(string itemId)
|
||||||
|
{
|
||||||
|
return _items.TryGetValue(itemId, out var existingItem) ? existingItem.Quantity : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasItem(string itemId, int quantity = 1)
|
||||||
|
{
|
||||||
|
return GetItemCount(itemId) >= quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
GameCore/Inventory/InventorySystem.cs
Normal file
26
GameCore/Inventory/InventorySystem.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
using GameCore.Events;
|
||||||
|
|
||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public class InventorySystem : ISystem
|
||||||
|
{
|
||||||
|
private readonly World _world;
|
||||||
|
|
||||||
|
public InventorySystem(World world)
|
||||||
|
{
|
||||||
|
_world = world;
|
||||||
|
_world.Subscribe<AddItemToInventoryEvent>(OnAddItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(World world, float deltaTime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnAddItem(AddItemToInventoryEvent e)
|
||||||
|
{
|
||||||
|
var inventory = _world.GetComponent<InventoryComponent>(e.Target);
|
||||||
|
inventory?.AddItem(e.Item);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
GameCore/Inventory/Item.cs
Normal file
7
GameCore/Inventory/Item.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public struct Item(string itemId, int quantity)
|
||||||
|
{
|
||||||
|
public string ItemId { get; set; } = itemId;
|
||||||
|
public int Quantity { get; set; } = quantity;
|
||||||
|
}
|
||||||
10
GameCore/Inventory/PickupComponent.cs
Normal file
10
GameCore/Inventory/PickupComponent.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public class PickupComponent : IComponent
|
||||||
|
{
|
||||||
|
public string ItemId { get; set; }
|
||||||
|
public int Quantity { get; set; } = 1;
|
||||||
|
public bool IsInstantUse { get; set; } = false;
|
||||||
|
}
|
||||||
36
GameCore/Inventory/PickupSystem.cs
Normal file
36
GameCore/Inventory/PickupSystem.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using GameCore.Combat;
|
||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
using GameCore.Events;
|
||||||
|
using GameCore.Physics;
|
||||||
|
using GameCore.Player;
|
||||||
|
|
||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public class PickupSystem : ISystem
|
||||||
|
{
|
||||||
|
public void Update(World world, float deltaTime)
|
||||||
|
{
|
||||||
|
var entitiesWithCollisions = world.GetEntitiesWith<CollisionEventComponent>();
|
||||||
|
var withCollisions = entitiesWithCollisions.ToList();
|
||||||
|
foreach (var entity in withCollisions)
|
||||||
|
{
|
||||||
|
if (world.GetComponent<PlayerComponent>(entity) == null) return;
|
||||||
|
|
||||||
|
var collision = world.GetComponent<CollisionEventComponent>(entity);
|
||||||
|
if (collision == null) continue;
|
||||||
|
|
||||||
|
var pickup = world.GetComponent<PickupComponent>(entity);
|
||||||
|
var inventory = world.GetComponent<InventoryComponent>(entity);
|
||||||
|
|
||||||
|
if (pickup == null || inventory == null) continue;
|
||||||
|
|
||||||
|
var item = new Item(pickup.ItemId, pickup.Quantity);
|
||||||
|
// In the future, handle IsInstantUse items here
|
||||||
|
world.PublishEvent(new AddItemToInventoryEvent(entity, item));
|
||||||
|
world.AddComponent(collision.OtherEntity, new DeathComponent());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entity in withCollisions) world.RemoveComponent<CollisionEventComponent>(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using GameCore.Attributes;
|
using GameCore.Attributes;
|
||||||
using GameCore.ECS;
|
using GameCore.ECS;
|
||||||
using GameCore.ECS.Interfaces;
|
using GameCore.ECS.Interfaces;
|
||||||
|
using GameCore.Events;
|
||||||
using GameCore.Input;
|
using GameCore.Input;
|
||||||
using GameCore.Physics;
|
using GameCore.Physics;
|
||||||
using Attribute = GameCore.Attributes.Attribute;
|
using Attribute = GameCore.Attributes.Attribute;
|
||||||
@@ -26,6 +27,7 @@ public class JumpSystem : ISystem
|
|||||||
{
|
{
|
||||||
var jumpHeight = attributes.GetValue(Attribute.JumpHeight);
|
var jumpHeight = attributes.GetValue(Attribute.JumpHeight);
|
||||||
velocity.DesiredVelocity.Y = (float)System.Math.Sqrt(2f * world.Config.GravityStrength * jumpHeight);
|
velocity.DesiredVelocity.Y = (float)System.Math.Sqrt(2f * world.Config.GravityStrength * jumpHeight);
|
||||||
|
world.PublishEvent(new PlayerJumpEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
GameCore/Physics/CollisionEventComponent.cs
Normal file
9
GameCore/Physics/CollisionEventComponent.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Physics;
|
||||||
|
|
||||||
|
public class CollisionEventComponent(Entity otherEntity) : IComponent
|
||||||
|
{
|
||||||
|
public readonly Entity OtherEntity = otherEntity;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user