2023-11-28 02:06:38 -05:00
|
|
|
using Content.Shared.Damage;
|
2024-01-20 02:20:21 +09:00
|
|
|
using Content.Shared.Item.ItemToggle.Components;
|
2024-08-02 11:50:26 +00:00
|
|
|
using Content.Shared.Weapons.Melee.Events;
|
2023-11-27 22:12:34 +11:00
|
|
|
using Robust.Shared.Audio.Systems;
|
2022-07-10 21:50:09 -04:00
|
|
|
using Robust.Shared.Containers;
|
2022-07-04 02:31:12 -04:00
|
|
|
|
|
|
|
|
namespace Content.Shared.Blocking;
|
|
|
|
|
|
2022-12-09 22:42:38 -05:00
|
|
|
public sealed partial class BlockingSystem
|
2022-07-04 02:31:12 -04:00
|
|
|
{
|
2022-08-23 08:00:06 -04:00
|
|
|
[Dependency] private readonly DamageableSystem _damageable = default!;
|
2022-10-04 14:24:19 +11:00
|
|
|
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
2022-07-04 02:31:12 -04:00
|
|
|
|
2022-12-09 22:42:38 -05:00
|
|
|
private void InitializeUser()
|
2022-07-04 02:31:12 -04:00
|
|
|
{
|
|
|
|
|
SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
|
2023-06-05 05:23:54 +03:00
|
|
|
SubscribeLocalEvent<BlockingComponent, DamageModifyEvent>(OnDamageModified);
|
2022-07-04 02:31:12 -04:00
|
|
|
|
2022-07-10 21:50:09 -04:00
|
|
|
SubscribeLocalEvent<BlockingUserComponent, EntParentChangedMessage>(OnParentChanged);
|
|
|
|
|
SubscribeLocalEvent<BlockingUserComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt);
|
2022-07-04 02:31:12 -04:00
|
|
|
SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged);
|
2022-12-09 22:42:38 -05:00
|
|
|
SubscribeLocalEvent<BlockingUserComponent, EntityTerminatingEvent>(OnEntityTerminating);
|
2024-08-02 11:50:26 +00:00
|
|
|
|
|
|
|
|
SubscribeLocalEvent<BlockingUserComponent, MeleeBlockAttemptEvent>(OnMeleeBlockAttempt); // WD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WD START
|
|
|
|
|
private void OnMeleeBlockAttempt(Entity<BlockingUserComponent> ent, ref MeleeBlockAttemptEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Handled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var uid = ent.Comp.BlockingItem;
|
|
|
|
|
if (!TryComp(uid, out BlockingComponent? blocking) || !blocking.IsBlocking)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (TryComp(uid.Value, out ItemToggleComponent? toggle) && !toggle.Activated)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!TryComp(uid.Value, out DamageableComponent? damageable))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_audio.PlayPredicted(blocking.BlockSound, ent, args.Attacker);
|
|
|
|
|
_popupSystem.PopupPredicted(Loc.GetString("melee-block-event-blocked"), ent, args.Attacker);
|
|
|
|
|
_damageable.TryChangeDamage(uid.Value, args.Damage, damageable: damageable);
|
|
|
|
|
args.Handled = true;
|
2022-07-04 02:31:12 -04:00
|
|
|
}
|
2024-08-02 11:50:26 +00:00
|
|
|
// WD END
|
2022-07-04 02:31:12 -04:00
|
|
|
|
2022-07-10 21:50:09 -04:00
|
|
|
private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args)
|
|
|
|
|
{
|
2022-12-09 22:42:38 -05:00
|
|
|
UserStopBlocking(uid, component);
|
2022-07-10 21:50:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args)
|
|
|
|
|
{
|
2022-12-09 22:42:38 -05:00
|
|
|
UserStopBlocking(uid, component);
|
2022-07-10 21:50:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 02:31:12 -04:00
|
|
|
private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args)
|
|
|
|
|
{
|
2022-07-10 21:50:09 -04:00
|
|
|
if (args.Anchored)
|
2022-07-04 02:31:12 -04:00
|
|
|
return;
|
|
|
|
|
|
2022-12-09 22:42:38 -05:00
|
|
|
UserStopBlocking(uid, component);
|
2022-07-04 02:31:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUserDamageModified(EntityUid uid, BlockingUserComponent component, DamageModifyEvent args)
|
|
|
|
|
{
|
2023-06-05 05:23:54 +03:00
|
|
|
if (TryComp<BlockingComponent>(component.BlockingItem, out var blocking))
|
2022-07-04 02:31:12 -04:00
|
|
|
{
|
2023-11-28 02:06:38 -05:00
|
|
|
if (args.Damage.GetTotal() <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-03-11 03:55:19 +02:00
|
|
|
// 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;
|
|
|
|
|
|
2023-06-05 05:23:54 +03:00
|
|
|
var blockFraction = blocking.IsBlocking ? blocking.ActiveBlockFraction : blocking.PassiveBlockFraction;
|
2024-01-20 02:20:21 +09:00
|
|
|
if (TryComp(component.BlockingItem, out ItemToggleComponent? toggle) && !toggle.Activated) // WD
|
|
|
|
|
blockFraction = 0f;
|
2023-06-05 05:23:54 +03:00
|
|
|
blockFraction = Math.Clamp(blockFraction, 0, 1);
|
|
|
|
|
_damageable.TryChangeDamage(component.BlockingItem, blockFraction * args.OriginalDamage);
|
2022-07-04 02:31:12 -04:00
|
|
|
|
2024-03-11 03:55:19 +02:00
|
|
|
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);
|
2023-06-05 05:23:54 +03:00
|
|
|
|
2024-03-11 03:55:19 +02:00
|
|
|
if (blocking.IsBlocking && !args.Damage.Equals(args.OriginalDamage))
|
2022-07-04 02:31:12 -04:00
|
|
|
{
|
2023-11-27 22:12:34 +11:00
|
|
|
_audio.PlayPvs(blocking.BlockSound, uid);
|
2022-07-04 02:31:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-09 22:42:38 -05:00
|
|
|
|
2023-06-05 05:23:54 +03:00
|
|
|
private void OnDamageModified(EntityUid uid, BlockingComponent component, DamageModifyEvent args)
|
|
|
|
|
{
|
2023-06-17 05:07:58 +03:00
|
|
|
var modifier = component.IsBlocking ? component.ActiveBlockDamageModifier : component.PassiveBlockDamageModifer;
|
2023-06-05 05:23:54 +03:00
|
|
|
if (modifier == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, modifier);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 22:42:38 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2022-07-04 02:31:12 -04:00
|
|
|
}
|