AI/Guests/Inventory/Item.cs

namespace HC3.Inventory;

#nullable enable

public partial class Item : Component, ISceneMetadata
{
	[Property] public string Title { get; set; } = "Item";
	[Property] public Texture? Icon { get; set; }
	[Property] public string? Category { get; set; }
	[Property] public string? Description { get; set; }

	/// <summary>
	/// An accessor for the inventory
	/// </summary>
	public Inventory Inventory => GetComponentInParent<Inventory>();

	/// <summary>
	/// An accessor for the guest
	/// </summary>
	public Guest Guest => GetComponentInParent<Guest>();

	protected override void OnStart()
	{
		if ( !Inventory.IsValid() ) return;

		IItemEvents.PostToGameObject( Inventory.GameObject, x => x.OnAdded( this ) );
	}

	protected virtual void OnUse()
	{
		// Item used!
	}

	/// <summary>
	/// Consumes an item
	/// </summary>
	/// <param name="remove"></param>
	public void Use( bool remove = true )
	{
		SendUsedRpc();

		if ( !remove )
			return;

		Inventory.Remove( this );
	}

	[Rpc.Broadcast( NetFlags.HostOnly )]
	void SendUsedRpc()
	{
		OnUse();
		IItemEvents.PostToGameObject( Inventory.GameObject, x => x.OnUsed( this ) );
	}

	Dictionary<string, string> ISceneMetadata.GetMetadata()
	{
		return new()
		{
			{ "Type", "Item" }
		};
	}
}