1
0

RCDConstructionGhostSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Content.Shared.Hands.Components;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.RCD;
  4. using Content.Shared.RCD.Components;
  5. using Content.Shared.RCD.Systems;
  6. using Robust.Client.Placement;
  7. using Robust.Client.Player;
  8. using Robust.Shared.Enums;
  9. namespace Content.Client.RCD;
  10. public sealed class RCDConstructionGhostSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IPlayerManager _playerManager = default!;
  13. [Dependency] private readonly RCDSystem _rcdSystem = default!;
  14. [Dependency] private readonly IPlacementManager _placementManager = default!;
  15. private string _placementMode = typeof(AlignRCDConstruction).Name;
  16. private Direction _placementDirection = default;
  17. public override void Update(float frameTime)
  18. {
  19. base.Update(frameTime);
  20. // Get current placer data
  21. var placerEntity = _placementManager.CurrentPermission?.MobUid;
  22. var placerProto = _placementManager.CurrentPermission?.EntityType;
  23. var placerIsRCD = HasComp<RCDComponent>(placerEntity);
  24. // Exit if erasing or the current placer is not an RCD (build mode is active)
  25. if (_placementManager.Eraser || (placerEntity != null && !placerIsRCD))
  26. return;
  27. // Determine if player is carrying an RCD in their active hand
  28. var player = _playerManager.LocalSession?.AttachedEntity;
  29. if (!TryComp<HandsComponent>(player, out var hands))
  30. return;
  31. var heldEntity = hands.ActiveHand?.HeldEntity;
  32. if (!TryComp<RCDComponent>(heldEntity, out var rcd))
  33. {
  34. // If the player was holding an RCD, but is no longer, cancel placement
  35. if (placerIsRCD)
  36. _placementManager.Clear();
  37. return;
  38. }
  39. // Update the direction the RCD prototype based on the placer direction
  40. if (_placementDirection != _placementManager.Direction)
  41. {
  42. _placementDirection = _placementManager.Direction;
  43. RaiseNetworkEvent(new RCDConstructionGhostRotationEvent(GetNetEntity(heldEntity.Value), _placementDirection));
  44. }
  45. // If the placer has not changed, exit
  46. _rcdSystem.UpdateCachedPrototype(heldEntity.Value, rcd);
  47. if (heldEntity == placerEntity && rcd.CachedPrototype.Prototype == placerProto)
  48. return;
  49. // Create a new placer
  50. var newObjInfo = new PlacementInformation
  51. {
  52. MobUid = heldEntity.Value,
  53. PlacementOption = _placementMode,
  54. EntityType = rcd.CachedPrototype.Prototype,
  55. Range = (int) Math.Ceiling(SharedInteractionSystem.InteractionRange),
  56. IsTile = (rcd.CachedPrototype.Mode == RcdMode.ConstructTile),
  57. UseEditorContext = false,
  58. };
  59. _placementManager.Clear();
  60. _placementManager.BeginPlacing(newObjInfo);
  61. }
  62. }