StandingStateSystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Numerics;
  2. using Content.Shared.Hands.Components;
  3. using Content.Shared.Hands.EntitySystems;
  4. using Content.Shared.Standing;
  5. using Content.Shared.Throwing;
  6. using Robust.Shared.Physics.Components;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Standing;
  9. public sealed class StandingStateSystem : EntitySystem
  10. {
  11. [Dependency] private readonly IRobustRandom _random = default!;
  12. [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
  13. [Dependency] private readonly ThrowingSystem _throwingSystem = default!;
  14. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  15. private void FallOver(EntityUid uid, StandingStateComponent component, DropHandItemsEvent args)
  16. {
  17. var direction = EntityManager.TryGetComponent(uid, out PhysicsComponent? comp) ? comp.LinearVelocity / 50 : Vector2.Zero;
  18. var dropAngle = _random.NextFloat(0.8f, 1.2f);
  19. var fellEvent = new FellDownEvent(uid);
  20. RaiseLocalEvent(uid, fellEvent, false);
  21. if (!TryComp(uid, out HandsComponent? handsComp))
  22. return;
  23. var worldRotation = _transformSystem.GetWorldRotation(uid).ToVec();
  24. foreach (var hand in handsComp.Hands.Values)
  25. {
  26. if (hand.HeldEntity is not EntityUid held)
  27. continue;
  28. if (!_handsSystem.TryDrop(uid, hand, null, checkActionBlocker: false, handsComp: handsComp))
  29. continue;
  30. _throwingSystem.TryThrow(held,
  31. _random.NextAngle().RotateVec(direction / dropAngle + worldRotation / 50),
  32. 0.5f * dropAngle * _random.NextFloat(-0.9f, 1.1f),
  33. uid, 0);
  34. }
  35. }
  36. public override void Initialize()
  37. {
  38. base.Initialize();
  39. SubscribeLocalEvent<StandingStateComponent, DropHandItemsEvent>(FallOver);
  40. }
  41. }
  42. /// <summary>
  43. /// Raised after an entity falls down.
  44. /// </summary>
  45. public sealed class FellDownEvent : EntityEventArgs
  46. {
  47. public EntityUid Uid { get; }
  48. public FellDownEvent(EntityUid uid)
  49. {
  50. Uid = uid;
  51. }
  52. }