|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using Vintagestory.API.Common; |
| 4 | +using Vintagestory.API.Common.Entities; |
| 5 | +using Vintagestory.GameContent; |
| 6 | + |
| 7 | +namespace Pyrophobia.Behaviors; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// F01 / D08: brandish a lit main-hand torch (universal, main thread). |
| 11 | +/// Prepended ahead of <c>CanIgnite</c>; stance is in-memory, not Attributes. |
| 12 | +/// </summary> |
| 13 | +public class CollectibleBehaviorBrandishTorch : CollectibleBehavior |
| 14 | +{ |
| 15 | + public const string ClassName = "BrandishTorch"; |
| 16 | + private const string TorchBrandishAnimation = "pyrophobia-brandishtorch"; |
| 17 | + |
| 18 | + // Cleared in ModSystem.Dispose. |
| 19 | + private static readonly ConcurrentDictionary<long, byte> BrandishingByEntityId = new(); |
| 20 | + |
| 21 | + public CollectibleBehaviorBrandishTorch(CollectibleObject collObj) |
| 22 | + : base(collObj) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + internal static void ResetStanceState() => BrandishingByEntityId.Clear(); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// PreventDefault marks the use as handled so the caller sets |
| 30 | + /// <c>Controls.HandUse</c>. With a block targeted or Shift held we |
| 31 | + /// PassThrough, which keeps placement, relighting, and CanIgnite vanilla. |
| 32 | + /// </summary> |
| 33 | + public override void OnHeldInteractStart( |
| 34 | + ItemSlot slot, |
| 35 | + EntityAgent byEntity, |
| 36 | + BlockSelection blockSel, |
| 37 | + EntitySelection entitySel, |
| 38 | + bool firstEvent, |
| 39 | + ref EnumHandHandling handHandling, |
| 40 | + ref EnumHandling handling) |
| 41 | + { |
| 42 | + if (!IsBrandishableTorch(slot, byEntity)) |
| 43 | + { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (byEntity.Controls.ShiftKey || blockSel != null) |
| 48 | + { |
| 49 | + SetBrandishing(byEntity, false); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + SetBrandishing(byEntity, true); |
| 54 | + handHandling = EnumHandHandling.PreventDefault; |
| 55 | + handling = EnumHandling.PreventDefault; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Keep HandUse active for the hold duration, even when aim later crosses |
| 60 | + /// a block. PreventSubsequent stops CanIgnite from treating that as ignite. |
| 61 | + /// </summary> |
| 62 | + public override bool OnHeldInteractStep( |
| 63 | + float secondsUsed, |
| 64 | + ItemSlot slot, |
| 65 | + EntityAgent byEntity, |
| 66 | + BlockSelection blockSel, |
| 67 | + EntitySelection entitySel, |
| 68 | + ref EnumHandling handling) |
| 69 | + { |
| 70 | + if (!IsBrandishing(byEntity)) |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + handling = EnumHandling.PreventSubsequent; |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Accept cancellation (release / swap) and ease the stance out. |
| 81 | + /// </summary> |
| 82 | + public override bool OnHeldInteractCancel( |
| 83 | + float secondsUsed, |
| 84 | + ItemSlot slot, |
| 85 | + EntityAgent byEntity, |
| 86 | + BlockSelection blockSel, |
| 87 | + EntitySelection entitySel, |
| 88 | + EnumItemUseCancelReason cancelReason, |
| 89 | + ref EnumHandling handled) |
| 90 | + { |
| 91 | + if (!IsBrandishing(byEntity)) |
| 92 | + { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + SetBrandishing(byEntity, false); |
| 97 | + handled = EnumHandling.PreventSubsequent; |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Brandishing produces no effect on stop. PreventSubsequent keeps |
| 103 | + /// CanIgnite's stop handler from starting fires after a long hold that |
| 104 | + /// began as a brandish and later aimed at a block. |
| 105 | + /// </summary> |
| 106 | + public override void OnHeldInteractStop( |
| 107 | + float secondsUsed, |
| 108 | + ItemSlot slot, |
| 109 | + EntityAgent byEntity, |
| 110 | + BlockSelection blockSel, |
| 111 | + EntitySelection entitySel, |
| 112 | + ref EnumHandling handling) |
| 113 | + { |
| 114 | + if (!IsBrandishing(byEntity)) |
| 115 | + { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + SetBrandishing(byEntity, false); |
| 120 | + handling = EnumHandling.PreventSubsequent; |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Only active brandishes get the stance animation; vanilla uses such as |
| 125 | + /// the ignite gesture keep their own animation. |
| 126 | + /// </summary> |
| 127 | + public override string GetHeldTpUseAnimation( |
| 128 | + ItemSlot activeHotbarSlot, |
| 129 | + Entity forEntity, |
| 130 | + ref EnumHandling bhHandling) |
| 131 | + { |
| 132 | + if (forEntity is EntityAgent agent && |
| 133 | + IsBrandishing(agent) && |
| 134 | + IsBrandishableTorch(activeHotbarSlot, forEntity)) |
| 135 | + { |
| 136 | + bhHandling = EnumHandling.PreventDefault; |
| 137 | + return TorchBrandishAnimation; |
| 138 | + } |
| 139 | + |
| 140 | + return null!; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Lit main-hand torch only. Slot identity keeps mirrored calls for other |
| 145 | + /// slots or entities on their vanilla path. |
| 146 | + /// </summary> |
| 147 | + private bool IsBrandishableTorch(ItemSlot slot, Entity byEntity) |
| 148 | + { |
| 149 | + if (collObj is not BlockTorch torch || |
| 150 | + torch.IsExtinct || |
| 151 | + collObj.Code?.Path.Contains("torch", StringComparison.OrdinalIgnoreCase) != true || |
| 152 | + slot.Itemstack?.Collectible != collObj || |
| 153 | + byEntity is not EntityPlayer entityPlayer || |
| 154 | + !ReferenceEquals(slot, entityPlayer.RightHandItemSlot)) |
| 155 | + { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + private static bool IsBrandishing(EntityAgent byEntity) => |
| 163 | + BrandishingByEntityId.ContainsKey(byEntity.EntityId); |
| 164 | + |
| 165 | + private static void SetBrandishing(EntityAgent byEntity, bool value) |
| 166 | + { |
| 167 | + if (value) |
| 168 | + { |
| 169 | + BrandishingByEntityId[byEntity.EntityId] = 0; |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + BrandishingByEntityId.TryRemove(byEntity.EntityId, out _); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments