From 5ee7945bfc1a5ae3fc6475efedb979cdb85f3131 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Wed, 29 Oct 2025 02:58:49 +0100 Subject: [PATCH] Refactor ConsumeAmmoCost to use readonly field for ammoId and improve Entity struct equality methods --- GameCore/Combat/Effects/ConsumeAmmoCost.cs | 10 ++++++---- GameCore/ECS/Entity.cs | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/GameCore/Combat/Effects/ConsumeAmmoCost.cs b/GameCore/Combat/Effects/ConsumeAmmoCost.cs index d81e494..16c5fea 100644 --- a/GameCore/Combat/Effects/ConsumeAmmoCost.cs +++ b/GameCore/Combat/Effects/ConsumeAmmoCost.cs @@ -6,20 +6,22 @@ namespace GameCore.Combat.Effects; public class ConsumeAmmoCost(string ammoId, int amount) : ICostEffect { + public readonly string AmmoId = ammoId; + public void Execute(EffectContext context) { var inventory = context.World.GetComponent(context.Owner); if (inventory == null) return; - inventory.RemoveItem(ammoId, amount); + inventory.RemoveItem(AmmoId, amount); - var newQuantity = inventory.GetItemCount(ammoId); - context.World.PublishEvent(new InventoryItemChangedEvent(context.Owner, ammoId, newQuantity)); + var newQuantity = inventory.GetItemCount(AmmoId); + context.World.PublishEvent(new InventoryItemChangedEvent(context.Owner, AmmoId, newQuantity)); } public bool CanAfford(EffectContext context) { var inventory = context.World.GetComponent(context.Owner); - return inventory != null && inventory.HasItem(ammoId, amount); + return inventory != null && inventory.HasItem(AmmoId, amount); } } \ No newline at end of file diff --git a/GameCore/ECS/Entity.cs b/GameCore/ECS/Entity.cs index 2e8aad5..fd9349d 100644 --- a/GameCore/ECS/Entity.cs +++ b/GameCore/ECS/Entity.cs @@ -1,11 +1,23 @@ +using System.Diagnostics.CodeAnalysis; + namespace GameCore.ECS; /// -/// Represents a unique object in the game world. -/// It's a simple struct containing an ID to keep it lightweight. -/// All data associated with an Entity is stored in Components. +/// Represents a unique object in the game world. +/// It's a simple struct containing an ID to keep it lightweight. +/// All data associated with an Entity is stored in Components. /// public readonly struct Entity(int id) { public readonly int Id = id; + + public override bool Equals([NotNullWhen(true)] object? obj) + { + return base.Equals(obj); + } + + public override int GetHashCode() + { + return Id.GetHashCode(); + } } \ No newline at end of file