Use typed item keys in behavior item refs
This commit is contained in:
parent
df59a9da62
commit
95bd76ee91
10 changed files with 48 additions and 24 deletions
|
|
@ -38,6 +38,11 @@ public class AetheriaRuntimeItemReference
|
|||
return legacyItemId == Guid.Empty ? "" : $"{LegacyItemDefinitionPrefix}{legacyItemId:D}";
|
||||
}
|
||||
|
||||
public static Guid ToLegacyId(string itemKey)
|
||||
{
|
||||
return TryParseLegacyId(itemKey, out var itemId) ? itemId : Guid.Empty;
|
||||
}
|
||||
|
||||
private static bool TryParseLegacyId(string itemKey, out Guid itemId)
|
||||
{
|
||||
itemId = Guid.Empty;
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior
|
|||
OnStopFiring?.Invoke();
|
||||
return false;
|
||||
}
|
||||
if (_data.AmmoType != Guid.Empty)
|
||||
if (!string.IsNullOrWhiteSpace(_data.AmmoItemKey))
|
||||
{
|
||||
if (_reloading)
|
||||
{
|
||||
|
|
@ -104,10 +104,10 @@ public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior
|
|||
if (_data.MagazineSize > 1 && _ammo > 0) _ammo--;
|
||||
else
|
||||
{
|
||||
var cargo = Entity.FindItemInCargo(_data.AmmoType);
|
||||
var cargo = Entity.FindItemInCargo(_data.AmmoItemKey);
|
||||
if (cargo != null)
|
||||
{
|
||||
var item = cargo.GetFirstItem(_data.AmmoType);
|
||||
var item = cargo.GetFirstItem(_data.AmmoItemKey);
|
||||
if (item is SimpleCommodity simpleCommodity)
|
||||
cargo.Remove(simpleCommodity, 1);
|
||||
|
||||
|
|
|
|||
|
|
@ -127,12 +127,12 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior
|
|||
}
|
||||
|
||||
var hasAmmo = true;
|
||||
if (_data.AmmoType != Guid.Empty)
|
||||
if (!string.IsNullOrWhiteSpace(_data.AmmoItemKey))
|
||||
{
|
||||
var cargo = Entity.FindItemInCargo(_data.AmmoType);
|
||||
var cargo = Entity.FindItemInCargo(_data.AmmoItemKey);
|
||||
if (cargo != null)
|
||||
{
|
||||
var item = cargo.GetFirstItem(_data.AmmoType);
|
||||
var item = cargo.GetFirstItem(_data.AmmoItemKey);
|
||||
if (item is SimpleCommodity simpleCommodity)
|
||||
cargo.Remove(simpleCommodity, 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ using System.Linq;
|
|||
[Inspectable, Order(-5)]
|
||||
public class ItemUsageConfig : RuntimeBehaviorConfig
|
||||
{
|
||||
public Guid Item;
|
||||
public string ItemKey;
|
||||
public Guid Item => AetheriaRuntimeItemReference.ToLegacyId(ItemKey);
|
||||
|
||||
public override Behavior CreateInstance(EquippedItem item)
|
||||
{
|
||||
|
|
@ -36,10 +37,10 @@ public class ItemUsage : Behavior
|
|||
|
||||
public override bool Execute(float dt)
|
||||
{
|
||||
var cargo = Entity.FindItemInCargo(_data.Item);
|
||||
var cargo = Entity.FindItemInCargo(_data.ItemKey);
|
||||
if (cargo == null) return false;
|
||||
|
||||
var item = cargo.GetFirstItem(_data.Item);
|
||||
var item = cargo.GetFirstItem(_data.ItemKey);
|
||||
if (item is SimpleCommodity simpleCommodity)
|
||||
cargo.Remove(simpleCommodity, 1);
|
||||
if (item is CraftedItemInstance craftedItemInstance)
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ public abstract class WeaponConfig : RuntimeBehaviorConfig
|
|||
|
||||
[InspectablePrefab]
|
||||
public PerformanceStat Visibility = new PerformanceStat();
|
||||
public Guid AmmoType;
|
||||
public string AmmoItemKey;
|
||||
public Guid AmmoType => AetheriaRuntimeItemReference.ToLegacyId(AmmoItemKey);
|
||||
|
||||
[InspectablePrefab]
|
||||
public int MagazineSize;
|
||||
|
|
@ -69,9 +70,10 @@ public abstract class Weapon : Behavior, IActivatedBehavior
|
|||
public abstract int Ammo { get; }
|
||||
public DamageType DamageType { get; }
|
||||
public string EffectPrefab { get; }
|
||||
public string AmmoItemKey { get; }
|
||||
public Guid AmmoType { get; }
|
||||
public int MagazineSize { get; }
|
||||
public bool UsesAmmo => AmmoType != Guid.Empty;
|
||||
public bool UsesAmmo => !string.IsNullOrWhiteSpace(AmmoItemKey);
|
||||
public GuidedProjectileTargetMode GuidedProjectileTargeting { get; private set; }
|
||||
public bool HasGuidedProjectileProfile => GuidedProjectileTargeting != GuidedProjectileTargetMode.None;
|
||||
public float4[] GuidedProjectileGuidanceCurve { get; private set; }
|
||||
|
|
@ -102,6 +104,7 @@ public abstract class Weapon : Behavior, IActivatedBehavior
|
|||
_data = data;
|
||||
DamageType = data.DamageType;
|
||||
EffectPrefab = data.EffectPrefab ?? "";
|
||||
AmmoItemKey = data.AmmoItemKey;
|
||||
AmmoType = data.AmmoType;
|
||||
MagazineSize = data.MagazineSize;
|
||||
InitializeGuidedProjectileProfile(data);
|
||||
|
|
@ -112,6 +115,7 @@ public abstract class Weapon : Behavior, IActivatedBehavior
|
|||
_data = data;
|
||||
DamageType = data.DamageType;
|
||||
EffectPrefab = data.EffectPrefab ?? "";
|
||||
AmmoItemKey = data.AmmoItemKey;
|
||||
AmmoType = data.AmmoType;
|
||||
MagazineSize = data.MagazineSize;
|
||||
InitializeGuidedProjectileProfile(data);
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ public class ItemManager
|
|||
|
||||
private static void ApplyItemUsageConfig(ItemUsageConfig config, BehaviorPayloadReader reader)
|
||||
{
|
||||
config.Item = reader.Guid(1, config.Item);
|
||||
config.ItemKey = reader.ItemKey(1, config.ItemKey);
|
||||
}
|
||||
|
||||
private static void ApplyMiningToolConfig(MiningToolConfig config, BehaviorPayloadReader reader)
|
||||
|
|
@ -303,7 +303,7 @@ public class ItemManager
|
|||
config.Energy = reader.PerformanceStat(9, config.Energy);
|
||||
config.Heat = reader.PerformanceStat(10, config.Heat);
|
||||
config.Visibility = reader.PerformanceStat(11, config.Visibility);
|
||||
config.AmmoType = reader.Guid(12, config.AmmoType);
|
||||
config.AmmoItemKey = reader.ItemKey(12, config.AmmoItemKey);
|
||||
config.MagazineSize = reader.Int(13, config.MagazineSize);
|
||||
config.ReloadTime = reader.Float(14, config.ReloadTime);
|
||||
config.Spread = reader.PerformanceStat(15, config.Spread);
|
||||
|
|
@ -424,6 +424,13 @@ public class ItemManager
|
|||
: fallback;
|
||||
}
|
||||
|
||||
public string ItemKey(int key, string fallback = "")
|
||||
{
|
||||
return Guid(key) is var itemId && itemId != System.Guid.Empty
|
||||
? AetheriaRuntimeItemReference.FromLegacyId(itemId)
|
||||
: fallback;
|
||||
}
|
||||
|
||||
public T Enum<T>(int key, T fallback) where T : struct
|
||||
{
|
||||
if (!_fields.TryGetValue(key, out var value))
|
||||
|
|
|
|||
Loading…
Reference in a new issue