| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using Content.Shared.Damage;
- using Content.Shared.Damage.Prototypes;
- using Robust.Shared.Audio;
- using Robust.Shared.Audio.Systems;
- using Robust.Shared.Containers;
- namespace Content.Shared.Blocking;
- public sealed partial class BlockingSystem
- {
- [Dependency] private readonly DamageableSystem _damageable = default!;
- [Dependency] private readonly SharedAudioSystem _audio = default!;
- private void InitializeUser()
- {
- SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
- SubscribeLocalEvent<BlockingComponent, DamageModifyEvent>(OnDamageModified);
- SubscribeLocalEvent<BlockingUserComponent, EntParentChangedMessage>(OnParentChanged);
- SubscribeLocalEvent<BlockingUserComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt);
- SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged);
- SubscribeLocalEvent<BlockingUserComponent, EntityTerminatingEvent>(OnEntityTerminating);
- }
- private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args)
- {
- UserStopBlocking(uid, component);
- }
- private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args)
- {
- UserStopBlocking(uid, component);
- }
- private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args)
- {
- if (args.Anchored)
- return;
- UserStopBlocking(uid, component);
- }
- private void OnUserDamageModified(EntityUid uid, BlockingUserComponent component, DamageModifyEvent args)
- {
- if (TryComp<BlockingComponent>(component.BlockingItem, out var blocking))
- {
- if (args.Damage.GetTotal() <= 0)
- return;
- // A shield should only block damage it can itself absorb. To determine that we need the Damageable component on it.
- if (!TryComp<DamageableComponent>(component.BlockingItem, out var dmgComp))
- return;
- var blockFraction = blocking.IsBlocking ? blocking.ActiveBlockFraction : blocking.PassiveBlockFraction;
- blockFraction = Math.Clamp(blockFraction, 0, 1);
- _damageable.TryChangeDamage(component.BlockingItem, blockFraction * args.OriginalDamage);
- var modify = new DamageModifierSet();
- foreach (var key in dmgComp.Damage.DamageDict.Keys)
- {
- modify.Coefficients.TryAdd(key, 1 - blockFraction);
- }
- args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, modify);
- if (blocking.IsBlocking && !args.Damage.Equals(args.OriginalDamage))
- {
- _audio.PlayPvs(blocking.BlockSound, uid);
- }
- }
- }
- private void OnDamageModified(EntityUid uid, BlockingComponent component, DamageModifyEvent args)
- {
- var modifier = component.IsBlocking ? component.ActiveBlockDamageModifier : component.PassiveBlockDamageModifer;
- if (modifier == null)
- {
- return;
- }
- args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, modifier);
- }
- private void OnEntityTerminating(EntityUid uid, BlockingUserComponent component, ref EntityTerminatingEvent args)
- {
- if (!TryComp<BlockingComponent>(component.BlockingItem, out var blockingComponent))
- return;
- StopBlockingHelper(component.BlockingItem.Value, blockingComponent, uid);
- }
- /// <summary>
- /// Check for the shield and has the user stop blocking
- /// Used where you'd like the user to stop blocking, but also don't want to remove the <see cref="BlockingUserComponent"/>
- /// </summary>
- /// <param name="uid">The user blocking</param>
- /// <param name="component">The <see cref="BlockingUserComponent"/></param>
- private void UserStopBlocking(EntityUid uid, BlockingUserComponent component)
- {
- if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking)
- StopBlocking(component.BlockingItem.Value, blockComp, uid);
- }
- }
|