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 class ConsumeAmmoCost(string ammoId, int amount) : ICostEffect
|
||||||
{
|
{
|
||||||
|
public readonly string AmmoId = ammoId;
|
||||||
|
|
||||||
public void Execute(EffectContext context)
|
public void Execute(EffectContext context)
|
||||||
{
|
{
|
||||||
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
||||||
if (inventory == null) return;
|
if (inventory == null) return;
|
||||||
|
|
||||||
inventory.RemoveItem(ammoId, amount);
|
inventory.RemoveItem(AmmoId, amount);
|
||||||
|
|
||||||
var newQuantity = inventory.GetItemCount(ammoId);
|
var newQuantity = inventory.GetItemCount(AmmoId);
|
||||||
context.World.PublishEvent(new InventoryItemChangedEvent(context.Owner, ammoId, newQuantity));
|
context.World.PublishEvent(new InventoryItemChangedEvent(context.Owner, AmmoId, newQuantity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanAfford(EffectContext context)
|
public bool CanAfford(EffectContext context)
|
||||||
{
|
{
|
||||||
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
||||||
return inventory != null && inventory.HasItem(ammoId, amount);
|
return inventory != null && inventory.HasItem(AmmoId, amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,23 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace GameCore.ECS;
|
namespace GameCore.ECS;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a unique object in the game world.
|
/// Represents a unique object in the game world.
|
||||||
/// It's a simple struct containing an ID to keep it lightweight.
|
/// It's a simple struct containing an ID to keep it lightweight.
|
||||||
/// All data associated with an Entity is stored in Components.
|
/// All data associated with an Entity is stored in Components.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly struct Entity(int id)
|
public readonly struct Entity(int id)
|
||||||
{
|
{
|
||||||
public readonly int Id = 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