Bind consumables with typed action bar rows
This commit is contained in:
parent
9cbd9eaa2a
commit
da27eb39b6
4 changed files with 51 additions and 21 deletions
|
|
@ -57,25 +57,27 @@ public abstract class ActionBarBinding
|
|||
|
||||
public class ActionBarConsumableBinding : ActionBarBinding
|
||||
{
|
||||
public ConsumableItemData Target;
|
||||
public AetheriaRuntimeCatalogItem Target { get; }
|
||||
private readonly Func<ConsumableItemData> _resolveLegacyTarget;
|
||||
|
||||
public ActionBarConsumableBinding(Entity entity, ActionBarSlot slot, ConsumableItemData target) : base(entity, slot)
|
||||
public ActionBarConsumableBinding(
|
||||
Entity entity,
|
||||
ActionBarSlot slot,
|
||||
AetheriaRuntimeCatalogItem target,
|
||||
Func<ConsumableItemData> resolveLegacyTarget) : base(entity, slot)
|
||||
{
|
||||
Target = target;
|
||||
_resolveLegacyTarget = resolveLegacyTarget;
|
||||
Slot.QuantityRemaining.gameObject.SetActive(true);
|
||||
var data = Target;
|
||||
if(!string.IsNullOrEmpty(data.Icon))
|
||||
{
|
||||
Slot.Label.gameObject.SetActive(false);
|
||||
Slot.Icon.gameObject.SetActive(true);
|
||||
Slot.Icon.texture = Resources.Load<Texture2D>(data.Icon.Substring("Assets/Resources/".Length).Split('.').First());
|
||||
}
|
||||
else Slot.Icon.gameObject.SetActive(false);
|
||||
Slot.Label.gameObject.SetActive(false);
|
||||
Slot.Icon.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void Activate()
|
||||
{
|
||||
Entity.TryActivateConsumable(Target);
|
||||
var legacyTarget = ResolveLegacyTarget();
|
||||
if (legacyTarget != null)
|
||||
Entity.TryActivateConsumable(legacyTarget);
|
||||
}
|
||||
|
||||
public override void Deactivate()
|
||||
|
|
@ -84,11 +86,26 @@ public class ActionBarConsumableBinding : ActionBarBinding
|
|||
|
||||
public override void Update()
|
||||
{
|
||||
Slot.QuantityRemaining.text = $"{Entity.CountItemsInCargo(Target.ID)}";
|
||||
var instance = Entity.FindActiveConsumable(Target);
|
||||
Slot.QuantityRemaining.text = $"{Entity.CountItemsInCargo(TargetLegacyId)}";
|
||||
var legacyTarget = ResolveLegacyTarget();
|
||||
if (legacyTarget == null)
|
||||
{
|
||||
Slot.Fill.fillAmount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var instance = Entity.FindActiveConsumable(legacyTarget);
|
||||
if (instance == null) Slot.Fill.fillAmount = 0;
|
||||
else Slot.Fill.fillAmount = instance.RemainingDuration / instance.Data.Duration;
|
||||
}
|
||||
|
||||
private Guid TargetLegacyId =>
|
||||
Guid.TryParse(Target.LegacyId, out var legacyId) ? legacyId : Guid.Empty;
|
||||
|
||||
private ConsumableItemData ResolveLegacyTarget()
|
||||
{
|
||||
return _resolveLegacyTarget?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public class ActionBarGearBinding : ActionBarBinding
|
||||
|
|
|
|||
|
|
@ -729,9 +729,13 @@ public class ActionGameManager : MonoBehaviour
|
|||
slot.Binding = new ActionBarGearBinding(CurrentEntity, slot, equippedItemDragAction.EquippedItem, trigger);
|
||||
return true;
|
||||
case ItemInstanceDragObject itemInstanceDragAction:
|
||||
if (!IsRuntimeConsumableItem(itemInstanceDragAction.Item)) return false;
|
||||
if (!(ItemManager.GetData(itemInstanceDragAction.Item) is ConsumableItemData consumable)) return false;
|
||||
slot.Binding = new ActionBarConsumableBinding(CurrentEntity, slot, consumable);
|
||||
var consumable = FindTypedActionBarConsumable(itemInstanceDragAction.Item);
|
||||
if (consumable == null) return false;
|
||||
slot.Binding = new ActionBarConsumableBinding(
|
||||
CurrentEntity,
|
||||
slot,
|
||||
consumable,
|
||||
() => ItemManager.GetData(itemInstanceDragAction.Item) as ConsumableItemData);
|
||||
return true;
|
||||
case WeaponGroupDragObject weaponGroupDragAction:
|
||||
slot.Binding = new ActionBarWeaponGroupBinding(CurrentEntity, slot, weaponGroupDragAction.Group);
|
||||
|
|
@ -749,11 +753,13 @@ public class ActionGameManager : MonoBehaviour
|
|||
return slot;
|
||||
}
|
||||
|
||||
bool IsRuntimeConsumableItem(ItemInstance item)
|
||||
AetheriaRuntimeCatalogItem FindTypedActionBarConsumable(ItemInstance item)
|
||||
{
|
||||
var typedItem = FindTypedActionBarItem(item);
|
||||
return typedItem != null &&
|
||||
string.Equals(typedItem.Category, nameof(ConsumableItemData), StringComparison.Ordinal);
|
||||
string.Equals(typedItem.Category, nameof(ConsumableItemData), StringComparison.Ordinal)
|
||||
? typedItem
|
||||
: null;
|
||||
}
|
||||
|
||||
AetheriaRuntimeCatalogItem FindTypedActionBarItem(ItemInstance item)
|
||||
|
|
|
|||
Loading…
Reference in a new issue