84 lines
1.8 KiB
C#
84 lines
1.8 KiB
C#
using System;
|
|
using Game.Features.Items.Contracts;
|
|
|
|
namespace Game.Features.Items.Domain;
|
|
|
|
public sealed class HandsInventory
|
|
{
|
|
private ItemInstanceId? _leftHand;
|
|
private ItemInstanceId? _rightHand;
|
|
|
|
public bool IsFree(Hand hand) => Get(hand) is null;
|
|
|
|
public ItemInstanceId? Get(Hand hand) => hand switch
|
|
{
|
|
Hand.Left => _leftHand,
|
|
Hand.Right => _rightHand,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(hand), hand, null)
|
|
};
|
|
|
|
public bool TryHold(ItemInstanceId itemId, HandRequirement requirement, Hand hand)
|
|
{
|
|
if (_leftHand == itemId || _rightHand == itemId)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (requirement == HandRequirement.Both)
|
|
{
|
|
if (_leftHand is not null || _rightHand is not null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_leftHand = itemId;
|
|
_rightHand = itemId;
|
|
return true;
|
|
}
|
|
|
|
if (!IsFree(hand))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Set(hand, itemId);
|
|
return true;
|
|
}
|
|
|
|
public ItemInstanceId? Release(Hand hand)
|
|
{
|
|
ItemInstanceId? itemId = Get(hand);
|
|
if (itemId is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (_leftHand == itemId)
|
|
{
|
|
_leftHand = null;
|
|
}
|
|
|
|
if (_rightHand == itemId)
|
|
{
|
|
_rightHand = null;
|
|
}
|
|
|
|
return itemId;
|
|
}
|
|
|
|
private void Set(Hand hand, ItemInstanceId itemId)
|
|
{
|
|
switch (hand)
|
|
{
|
|
case Hand.Left:
|
|
_leftHand = itemId;
|
|
break;
|
|
case Hand.Right:
|
|
_rightHand = itemId;
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(hand), hand, null);
|
|
}
|
|
}
|
|
}
|