ChasmSystem.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Content.Shared.ActionBlocker;
  2. using Content.Shared.Buckle.Components;
  3. using Content.Shared.Movement.Events;
  4. using Content.Shared.StepTrigger.Systems;
  5. using Robust.Shared.Audio;
  6. using Robust.Shared.Audio.Systems;
  7. using Robust.Shared.Network;
  8. using Robust.Shared.Physics.Components;
  9. using Robust.Shared.Timing;
  10. namespace Content.Shared.Chasm;
  11. /// <summary>
  12. /// Handles making entities fall into chasms when stepped on.
  13. /// </summary>
  14. public sealed class ChasmSystem : EntitySystem
  15. {
  16. [Dependency] private readonly IGameTiming _timing = default!;
  17. [Dependency] private readonly ActionBlockerSystem _blocker = default!;
  18. [Dependency] private readonly INetManager _net = default!;
  19. [Dependency] private readonly SharedAudioSystem _audio = default!;
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. SubscribeLocalEvent<ChasmComponent, StepTriggeredOffEvent>(OnStepTriggered);
  24. SubscribeLocalEvent<ChasmComponent, StepTriggerAttemptEvent>(OnStepTriggerAttempt);
  25. SubscribeLocalEvent<ChasmFallingComponent, UpdateCanMoveEvent>(OnUpdateCanMove);
  26. }
  27. public override void Update(float frameTime)
  28. {
  29. base.Update(frameTime);
  30. // don't predict queuedels on client
  31. if (_net.IsClient)
  32. return;
  33. var query = EntityQueryEnumerator<ChasmFallingComponent>();
  34. while (query.MoveNext(out var uid, out var chasm))
  35. {
  36. if (_timing.CurTime < chasm.NextDeletionTime)
  37. continue;
  38. QueueDel(uid);
  39. }
  40. }
  41. private void OnStepTriggered(EntityUid uid, ChasmComponent component, ref StepTriggeredOffEvent args)
  42. {
  43. // already doomed
  44. if (HasComp<ChasmFallingComponent>(args.Tripper))
  45. return;
  46. StartFalling(uid, component, args.Tripper);
  47. }
  48. public void StartFalling(EntityUid chasm, ChasmComponent component, EntityUid tripper, bool playSound = true)
  49. {
  50. var falling = AddComp<ChasmFallingComponent>(tripper);
  51. falling.NextDeletionTime = _timing.CurTime + falling.DeletionTime;
  52. _blocker.UpdateCanMove(tripper);
  53. if (playSound)
  54. _audio.PlayPredicted(component.FallingSound, chasm, tripper);
  55. }
  56. private void OnStepTriggerAttempt(EntityUid uid, ChasmComponent component, ref StepTriggerAttemptEvent args)
  57. {
  58. args.Continue = true;
  59. }
  60. private void OnUpdateCanMove(EntityUid uid, ChasmFallingComponent component, UpdateCanMoveEvent args)
  61. {
  62. args.Cancel();
  63. }
  64. }