Refactor ConsumeAmmoCost to use readonly field for ammoId and improve Entity struct equality methods
This commit is contained in:
@@ -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<InventoryComponent>(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<InventoryComponent>(context.Owner);
|
||||
return inventory != null && inventory.HasItem(ammoId, amount);
|
||||
return inventory != null && inventory.HasItem(AmmoId, amount);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace GameCore.ECS;
|
||||
|
||||
/// <summary>
|
||||
@@ -8,4 +10,14 @@ namespace GameCore.ECS;
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user