Use typed state for consumable activation
This commit is contained in:
parent
6707dcaba8
commit
5d96355439
162 changed files with 412 additions and 296 deletions
|
|
@ -58,16 +58,13 @@ public abstract class ActionBarBinding
|
|||
public class ActionBarConsumableBinding : ActionBarBinding
|
||||
{
|
||||
public AetheriaRuntimeCatalogItem Target { get; }
|
||||
private readonly Func<ConsumableItemData> _resolveLegacyTarget;
|
||||
|
||||
public ActionBarConsumableBinding(
|
||||
Entity entity,
|
||||
ActionBarSlot slot,
|
||||
AetheriaRuntimeCatalogItem target,
|
||||
Func<ConsumableItemData> resolveLegacyTarget) : base(entity, slot)
|
||||
AetheriaRuntimeCatalogItem target) : base(entity, slot)
|
||||
{
|
||||
Target = target;
|
||||
_resolveLegacyTarget = resolveLegacyTarget;
|
||||
Slot.QuantityRemaining.gameObject.SetActive(true);
|
||||
Slot.Label.gameObject.SetActive(false);
|
||||
Slot.Icon.gameObject.SetActive(false);
|
||||
|
|
@ -75,9 +72,7 @@ public class ActionBarConsumableBinding : ActionBarBinding
|
|||
|
||||
public override void Activate()
|
||||
{
|
||||
var legacyTarget = ResolveLegacyTarget();
|
||||
if (legacyTarget != null)
|
||||
Entity.TryActivateConsumable(legacyTarget);
|
||||
Entity.TryActivateConsumable(Target);
|
||||
}
|
||||
|
||||
public override void Deactivate()
|
||||
|
|
@ -87,25 +82,20 @@ public class ActionBarConsumableBinding : ActionBarBinding
|
|||
public override void Update()
|
||||
{
|
||||
Slot.QuantityRemaining.text = $"{Entity.CountItemsInCargo(TargetLegacyId)}";
|
||||
var legacyTarget = ResolveLegacyTarget();
|
||||
if (legacyTarget == null)
|
||||
if (TargetLegacyId == Guid.Empty)
|
||||
{
|
||||
Slot.Fill.fillAmount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var instance = Entity.FindActiveConsumable(legacyTarget);
|
||||
var instance = Entity.FindActiveConsumable(TargetLegacyId);
|
||||
if (instance == null) Slot.Fill.fillAmount = 0;
|
||||
else Slot.Fill.fillAmount = instance.RemainingDuration / instance.Data.Duration;
|
||||
else Slot.Fill.fillAmount = instance.RemainingDuration / instance.Duration;
|
||||
}
|
||||
|
||||
private Guid TargetLegacyId =>
|
||||
Guid.TryParse(Target.LegacyId, out var legacyId) ? legacyId : Guid.Empty;
|
||||
|
||||
private ConsumableItemData ResolveLegacyTarget()
|
||||
{
|
||||
return _resolveLegacyTarget?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public class ActionBarGearBinding : ActionBarBinding
|
||||
|
|
|
|||
|
|
@ -743,8 +743,7 @@ public class ActionGameManager : MonoBehaviour
|
|||
slot.Binding = new ActionBarConsumableBinding(
|
||||
CurrentEntity,
|
||||
slot,
|
||||
consumable,
|
||||
() => ItemManager.GetData(itemInstanceDragAction.Item) as ConsumableItemData);
|
||||
consumable);
|
||||
return true;
|
||||
case WeaponGroupDragObject weaponGroupDragAction:
|
||||
slot.Binding = new ActionBarWeaponGroupBinding(CurrentEntity, slot, weaponGroupDragAction.Group);
|
||||
|
|
|
|||
|
|
@ -121,6 +121,14 @@ public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemProjectionReader
|
|||
simple.MaxStack = item.MaxStack;
|
||||
}
|
||||
|
||||
if (projected is ConsumableItemData consumable)
|
||||
{
|
||||
consumable.Stackable = item.Stackable;
|
||||
consumable.Duration = (float)item.Duration;
|
||||
consumable.Effectiveness = ProjectCurve(item.EffectivenessCurveKeys);
|
||||
consumable.Behaviors = ProjectBehaviors(item).ToList();
|
||||
}
|
||||
|
||||
if (projected is EquippableItemData equippable)
|
||||
{
|
||||
equippable.Durability = (float)item.Durability;
|
||||
|
|
@ -175,6 +183,20 @@ public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemProjectionReader
|
|||
.ToArray();
|
||||
}
|
||||
|
||||
private static BezierCurve ProjectCurve(IReadOnlyList<AetheriaRuntimeCurveKey> keys)
|
||||
{
|
||||
return new BezierCurve
|
||||
{
|
||||
Keys = keys == null || keys.Count == 0
|
||||
? new[] { new float4(0, 1, 0, 0), new float4(1, 1, 0, 0) }
|
||||
: keys.Select(key => new float4(
|
||||
(float)key.Time,
|
||||
(float)key.Value,
|
||||
(float)key.InTangent,
|
||||
(float)key.OutTangent)).ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
private static IEnumerable<AudioStat> ProjectAudioStats(AetheriaRuntimeCatalogItem item)
|
||||
{
|
||||
return item.AudioStats.Select(audioStat => new AudioStat
|
||||
|
|
|
|||
|
|
@ -316,27 +316,67 @@ public abstract class Entity
|
|||
|
||||
public ConsumableItemEffect FindActiveConsumable(ConsumableItemData data)
|
||||
{
|
||||
return _activeConsumables.FirstOrDefault(ac => ac.Data.ID == data.ID);
|
||||
return data == null ? null : FindActiveConsumable(data.ID);
|
||||
}
|
||||
|
||||
public ConsumableItemEffect FindActiveConsumable(Guid itemId)
|
||||
{
|
||||
return _activeConsumables.FirstOrDefault(ac => ac.Item?.Data?.ItemId == itemId);
|
||||
}
|
||||
|
||||
public bool CanActivateConsumable(ConsumableItemData data)
|
||||
{
|
||||
return data.Stackable || FindActiveConsumable(data) == null;
|
||||
var typedItem = ItemManager.GetRuntimeItem(data?.ID ?? Guid.Empty);
|
||||
if (typedItem == null)
|
||||
return data != null && (data.Stackable || FindActiveConsumable(data) == null);
|
||||
|
||||
return CanActivateConsumable(typedItem);
|
||||
}
|
||||
|
||||
public bool CanActivateConsumable(AetheriaRuntimeCatalogItem item)
|
||||
{
|
||||
var itemId = GetLegacyGuid(item);
|
||||
return item != null && itemId != Guid.Empty && (item.Stackable || FindActiveConsumable(itemId) == null);
|
||||
}
|
||||
|
||||
public bool TryActivateConsumable(ConsumableItemData data)
|
||||
{
|
||||
var typedItem = ItemManager.GetRuntimeItem(data?.ID ?? Guid.Empty);
|
||||
if (typedItem != null)
|
||||
return TryActivateConsumable(typedItem);
|
||||
|
||||
if (!CanActivateConsumable(data)) return false;
|
||||
|
||||
|
||||
var bay = FindItemInCargo(data.ID);
|
||||
if (bay == null) return false;
|
||||
|
||||
|
||||
var item = (ConsumableItem) bay.ItemsOfType[data.ID].First();
|
||||
ActivateConsumable(item);
|
||||
bay.Remove(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryActivateConsumable(AetheriaRuntimeCatalogItem typedItem)
|
||||
{
|
||||
if (!CanActivateConsumable(typedItem)) return false;
|
||||
|
||||
var itemId = GetLegacyGuid(typedItem);
|
||||
var bay = FindItemInCargo(itemId);
|
||||
if (bay == null) return false;
|
||||
|
||||
var item = (ConsumableItem)bay.ItemsOfType[itemId].First();
|
||||
ActivateConsumable(item);
|
||||
bay.Remove(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Guid GetLegacyGuid(AetheriaRuntimeCatalogItem item)
|
||||
{
|
||||
return item != null && Guid.TryParse(item.LegacyId, out var legacyId)
|
||||
? legacyId
|
||||
: Guid.Empty;
|
||||
}
|
||||
|
||||
private void MapEntity()
|
||||
{
|
||||
var typedHull = ItemManager.GetRuntimeItem(Hull);
|
||||
|
|
@ -1209,13 +1249,21 @@ public class ConsumableItemEffect
|
|||
public ConsumableItem Item { get; }
|
||||
public ConsumableItemData Data { get; }
|
||||
public Behavior[] Behaviors { get; }
|
||||
public AetheriaRuntimeCatalogItem RuntimeItem { get; }
|
||||
public float Duration => Math.Max(_duration, float.Epsilon);
|
||||
|
||||
private readonly float _duration;
|
||||
private readonly BezierCurve _effectiveness;
|
||||
|
||||
public ConsumableItemEffect(ConsumableItem item, Entity entity)
|
||||
{
|
||||
Item = item;
|
||||
Entity = entity;
|
||||
RuntimeItem = entity.ItemManager.GetRuntimeItem(item);
|
||||
Data = entity.ItemManager.GetData(item) as ConsumableItemData;
|
||||
RemainingDuration = Data.Duration;
|
||||
_duration = RuntimeItem?.Duration > 0 ? (float)RuntimeItem.Duration : Data?.Duration ?? 0;
|
||||
_effectiveness = CreateEffectivenessCurve(RuntimeItem, Data);
|
||||
RemainingDuration = _duration;
|
||||
|
||||
Behaviors = entity.ItemManager.GetRuntimeBehaviorProjections(item)
|
||||
.Select(bd => bd.CreateInstance(this))
|
||||
|
|
@ -1238,7 +1286,8 @@ public class ConsumableItemEffect
|
|||
|
||||
public float Evaluate(PerformanceStat stat)
|
||||
{
|
||||
var effectiveness = Data.Effectiveness.Evaluate((Data.Duration - RemainingDuration) / Data.Duration);
|
||||
var duration = Math.Max(_duration, float.Epsilon);
|
||||
var effectiveness = _effectiveness.Evaluate((duration - RemainingDuration) / duration);
|
||||
var quality = pow(Item.Quality, stat.QualityExponent);
|
||||
|
||||
var result = lerp(stat.Min, stat.Max, effectiveness * quality);
|
||||
|
|
@ -1247,6 +1296,28 @@ public class ConsumableItemEffect
|
|||
return stat.Min;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static BezierCurve CreateEffectivenessCurve(AetheriaRuntimeCatalogItem item, ConsumableItemData fallback)
|
||||
{
|
||||
if (item?.EffectivenessCurveKeys != null && item.EffectivenessCurveKeys.Count > 0)
|
||||
{
|
||||
return new BezierCurve
|
||||
{
|
||||
Keys = item.EffectivenessCurveKeys
|
||||
.Select(key => new float4(
|
||||
(float)key.Time,
|
||||
(float)key.Value,
|
||||
(float)key.InTangent,
|
||||
(float)key.OutTangent))
|
||||
.ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
return fallback?.Effectiveness ?? new BezierCurve
|
||||
{
|
||||
Keys = new[] { new float4(0, 1, 0, 0), new float4(1, 1, 0, 0) }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class EquippedItem
|
||||
|
|
|
|||
Loading…
Reference in a new issue