AI/ActionSystem/Actions/Guest/ConsumeItemAction.cs
using HC3.Inventory;
namespace HC3;
public sealed class ConsumeItemAction : BehaviorTreeAction
{
private Item _item;
private Guest Guest => Agent as Guest;
private float _duration;
public override float Score()
{
if ( !Guest.IsValid() ) return 0f;
_item = null;
var items = Guest.Inventory.Items;
for ( int i = 0; i < items.Count; i++ )
{
if ( IsConsumable( items[i] ) )
{
_item = items[i];
break;
}
}
return _item.IsValid() ? 500f : 0f;
}
protected override void OnTreeStart()
{
_item = null;
var items = Guest.Inventory.Items;
for ( int i = 0; i < items.Count; i++ )
{
if ( IsConsumable( items[i] ) )
{
_item = items[i];
break;
}
}
_duration = Game.Random.Float( 3f, 6f );
}
protected override Node BuildTree()
{
return new SequenceNode( new()
{
new AgentSequenceNode( Agent, "Action_Eat", _duration ),
new ConsumeItemNode( Guest, _item )
} );
}
private bool IsConsumable( Item item )
{
var c = item.GetComponent<Consumable>();
if ( c == null ) return false;
var need = Guest.Needs.GetNeed( c.Need );
return need != null && need.IsActionable();
}
public override ActionDisplayInfo? GetDisplay()
{
var display = base.GetDisplay();
return new ActionDisplayInfo(
"ramen_dining",
$"Consuming {_item?.Title ?? "Item"}",
display?.Progress ?? 0f
);
}
}