VendingMachineSystem.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Linq;
  2. using Content.Shared.VendingMachines;
  3. using Robust.Client.Animations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Shared.GameStates;
  6. namespace Content.Client.VendingMachines;
  7. public sealed class VendingMachineSystem : SharedVendingMachineSystem
  8. {
  9. [Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
  10. [Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<VendingMachineComponent, AppearanceChangeEvent>(OnAppearanceChange);
  15. SubscribeLocalEvent<VendingMachineComponent, AnimationCompletedEvent>(OnAnimationCompleted);
  16. SubscribeLocalEvent<VendingMachineComponent, ComponentHandleState>(OnVendingHandleState);
  17. }
  18. private void OnVendingHandleState(Entity<VendingMachineComponent> entity, ref ComponentHandleState args)
  19. {
  20. if (args.Current is not VendingMachineComponentState state)
  21. return;
  22. var uid = entity.Owner;
  23. var component = entity.Comp;
  24. component.Contraband = state.Contraband;
  25. component.EjectEnd = state.EjectEnd;
  26. component.DenyEnd = state.DenyEnd;
  27. component.DispenseOnHitEnd = state.DispenseOnHitEnd;
  28. // If all we did was update amounts then we can leave BUI buttons in place.
  29. var fullUiUpdate = !component.Inventory.Keys.SequenceEqual(state.Inventory.Keys) ||
  30. !component.EmaggedInventory.Keys.SequenceEqual(state.EmaggedInventory.Keys) ||
  31. !component.ContrabandInventory.Keys.SequenceEqual(state.ContrabandInventory.Keys);
  32. component.Inventory.Clear();
  33. component.EmaggedInventory.Clear();
  34. component.ContrabandInventory.Clear();
  35. foreach (var entry in state.Inventory)
  36. {
  37. component.Inventory.Add(entry.Key, new(entry.Value));
  38. }
  39. foreach (var entry in state.EmaggedInventory)
  40. {
  41. component.EmaggedInventory.Add(entry.Key, new(entry.Value));
  42. }
  43. foreach (var entry in state.ContrabandInventory)
  44. {
  45. component.ContrabandInventory.Add(entry.Key, new(entry.Value));
  46. }
  47. if (UISystem.TryGetOpenUi<VendingMachineBoundUserInterface>(uid, VendingMachineUiKey.Key, out var bui))
  48. {
  49. if (fullUiUpdate)
  50. {
  51. bui.Refresh();
  52. }
  53. else
  54. {
  55. bui.UpdateAmounts();
  56. }
  57. }
  58. }
  59. protected override void UpdateUI(Entity<VendingMachineComponent?> entity)
  60. {
  61. if (!Resolve(entity, ref entity.Comp))
  62. return;
  63. if (UISystem.TryGetOpenUi<VendingMachineBoundUserInterface>(entity.Owner,
  64. VendingMachineUiKey.Key,
  65. out var bui))
  66. {
  67. bui.UpdateAmounts();
  68. }
  69. }
  70. private void OnAnimationCompleted(EntityUid uid, VendingMachineComponent component, AnimationCompletedEvent args)
  71. {
  72. if (!TryComp<SpriteComponent>(uid, out var sprite))
  73. return;
  74. if (!TryComp<AppearanceComponent>(uid, out var appearance) ||
  75. !_appearanceSystem.TryGetData<VendingMachineVisualState>(uid, VendingMachineVisuals.VisualState, out var visualState, appearance))
  76. {
  77. visualState = VendingMachineVisualState.Normal;
  78. }
  79. UpdateAppearance(uid, visualState, component, sprite);
  80. }
  81. private void OnAppearanceChange(EntityUid uid, VendingMachineComponent component, ref AppearanceChangeEvent args)
  82. {
  83. if (args.Sprite == null)
  84. return;
  85. if (!args.AppearanceData.TryGetValue(VendingMachineVisuals.VisualState, out var visualStateObject) ||
  86. visualStateObject is not VendingMachineVisualState visualState)
  87. {
  88. visualState = VendingMachineVisualState.Normal;
  89. }
  90. UpdateAppearance(uid, visualState, component, args.Sprite);
  91. }
  92. private void UpdateAppearance(EntityUid uid, VendingMachineVisualState visualState, VendingMachineComponent component, SpriteComponent sprite)
  93. {
  94. SetLayerState(VendingMachineVisualLayers.Base, component.OffState, sprite);
  95. switch (visualState)
  96. {
  97. case VendingMachineVisualState.Normal:
  98. SetLayerState(VendingMachineVisualLayers.BaseUnshaded, component.NormalState, sprite);
  99. SetLayerState(VendingMachineVisualLayers.Screen, component.ScreenState, sprite);
  100. break;
  101. case VendingMachineVisualState.Deny:
  102. if (component.LoopDenyAnimation)
  103. SetLayerState(VendingMachineVisualLayers.BaseUnshaded, component.DenyState, sprite);
  104. else
  105. PlayAnimation(uid, VendingMachineVisualLayers.BaseUnshaded, component.DenyState, (float)component.DenyDelay.TotalSeconds, sprite);
  106. SetLayerState(VendingMachineVisualLayers.Screen, component.ScreenState, sprite);
  107. break;
  108. case VendingMachineVisualState.Eject:
  109. PlayAnimation(uid, VendingMachineVisualLayers.BaseUnshaded, component.EjectState, (float)component.EjectDelay.TotalSeconds, sprite);
  110. SetLayerState(VendingMachineVisualLayers.Screen, component.ScreenState, sprite);
  111. break;
  112. case VendingMachineVisualState.Broken:
  113. HideLayers(sprite);
  114. SetLayerState(VendingMachineVisualLayers.Base, component.BrokenState, sprite);
  115. break;
  116. case VendingMachineVisualState.Off:
  117. HideLayers(sprite);
  118. break;
  119. }
  120. }
  121. private static void SetLayerState(VendingMachineVisualLayers layer, string? state, SpriteComponent sprite)
  122. {
  123. if (string.IsNullOrEmpty(state))
  124. return;
  125. sprite.LayerSetVisible(layer, true);
  126. sprite.LayerSetAutoAnimated(layer, true);
  127. sprite.LayerSetState(layer, state);
  128. }
  129. private void PlayAnimation(EntityUid uid, VendingMachineVisualLayers layer, string? state, float animationTime, SpriteComponent sprite)
  130. {
  131. if (string.IsNullOrEmpty(state))
  132. return;
  133. if (!_animationPlayer.HasRunningAnimation(uid, state))
  134. {
  135. var animation = GetAnimation(layer, state, animationTime);
  136. sprite.LayerSetVisible(layer, true);
  137. _animationPlayer.Play(uid, animation, state);
  138. }
  139. }
  140. private static Animation GetAnimation(VendingMachineVisualLayers layer, string state, float animationTime)
  141. {
  142. return new Animation
  143. {
  144. Length = TimeSpan.FromSeconds(animationTime),
  145. AnimationTracks =
  146. {
  147. new AnimationTrackSpriteFlick
  148. {
  149. LayerKey = layer,
  150. KeyFrames =
  151. {
  152. new AnimationTrackSpriteFlick.KeyFrame(state, 0f)
  153. }
  154. }
  155. }
  156. };
  157. }
  158. private static void HideLayers(SpriteComponent sprite)
  159. {
  160. HideLayer(VendingMachineVisualLayers.BaseUnshaded, sprite);
  161. HideLayer(VendingMachineVisualLayers.Screen, sprite);
  162. }
  163. private static void HideLayer(VendingMachineVisualLayers layer, SpriteComponent sprite)
  164. {
  165. if (!sprite.LayerMapTryGet(layer, out var actualLayer))
  166. return;
  167. sprite.LayerSetVisible(actualLayer, false);
  168. }
  169. }