StaminaActiveSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Content.Shared._Stalker.Stamina;
  2. using Content.Shared.Damage.Components;
  3. using Content.Shared.Damage.Systems;
  4. using Content.Shared.Movement.Components;
  5. using Content.Shared.Movement.Systems;
  6. using Robust.Shared.Physics.Components;
  7. namespace Content.Server._Stalker.Stamina;
  8. public sealed class StaminaActiveSystem : EntitySystem
  9. {
  10. [Dependency] private readonly StaminaSystem _stamina = default!;
  11. [Dependency] private readonly MovementSpeedModifierSystem _speed = default!;
  12. private ISawmill _sawmill = default!;
  13. public override void Initialize()
  14. {
  15. SubscribeLocalEvent<StaminaActiveComponent, RefreshMovementSpeedModifiersEvent>(OnRefresh);
  16. }
  17. public override void Update(float frameTime)
  18. {
  19. base.Update(frameTime);
  20. var query = EntityQueryEnumerator<StaminaComponent, MovementSpeedModifierComponent, StaminaActiveComponent, InputMoverComponent>();
  21. while (query.MoveNext(out var uid, out var stamina, out var modifier, out var active, out var input))
  22. {
  23. // If our entity is slowed, we can't apply new speed/speed modifiers
  24. // Because CurrentSprintSpeed will change
  25. if (!active.Slowed)
  26. {
  27. active.SprintModifier = modifier.BaseWalkSpeed / modifier.BaseSprintSpeed;
  28. }
  29. if (!TryComp<PhysicsComponent>(uid, out var phys))
  30. return;
  31. // If Walk button pressed we will apply stamina damage.
  32. if (input.HeldMoveButtons.HasFlag(MoveButtons.Walk) && !active.Slowed && phys.LinearVelocity.Length() != 0)
  33. {
  34. _stamina.TakeStaminaDamage(uid, active.RunStaminaDamage, stamina, visual: false);
  35. }
  36. // If our entity gets through SlowThreshold, we will apply slowing.
  37. // If our entity is slowed already, we don't need to multiply SprintModifier.
  38. if (stamina.StaminaDamage >= active.SlowThreshold && active.Slowed == false)
  39. {
  40. active.Slowed = true;
  41. active.Change = true;
  42. _speed.RefreshMovementSpeedModifiers(uid);
  43. return;
  44. }
  45. // If our entity revives until ReviveStaminaLevel we will remove same SprintModifier.
  46. // If our entity is already revived, we _don't need to remove SprintModifier.
  47. if (stamina.StaminaDamage <= active.ReviveStaminaLevel && active.Slowed)
  48. {
  49. active.Slowed = false;
  50. active.Change = true;
  51. _speed.RefreshMovementSpeedModifiers(uid);
  52. return;
  53. }
  54. }
  55. }
  56. private void OnRefresh(EntityUid uid, StaminaActiveComponent component, RefreshMovementSpeedModifiersEvent args)
  57. {
  58. if (!component.Change)
  59. return;
  60. var sprint = component.Slowed
  61. ? component.SprintModifier
  62. : args.SprintSpeedModifier;
  63. args.ModifySpeed(args.WalkSpeedModifier, sprint);
  64. }
  65. }