84 lines
3.6 KiB
C#
84 lines
3.6 KiB
C#
using Content.Shared.Damage;
|
|
using Content.Shared.Damage.Prototypes;
|
|
using Robust.Shared.Audio;
|
|
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, DamageChangedEvent>(OnDamageChanged);
|
|
SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
|
|
|
|
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 OnDamageChanged(EntityUid uid, BlockingUserComponent component, DamageChangedEvent args)
|
|
{
|
|
if (args.DamageDelta != null && args.DamageIncreased)
|
|
_damageable.TryChangeDamage(component.BlockingItem, args.DamageDelta, origin: args.Origin);
|
|
}
|
|
|
|
private void OnUserDamageModified(EntityUid uid, BlockingUserComponent component, DamageModifyEvent args)
|
|
{
|
|
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockingComponent))
|
|
{
|
|
if (_proto.TryIndex(blockingComponent.PassiveBlockDamageModifer, out DamageModifierSetPrototype? passiveblockModifier) && !blockingComponent.IsBlocking)
|
|
args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, passiveblockModifier);
|
|
|
|
if (_proto.TryIndex(blockingComponent.ActiveBlockDamageModifier, out DamageModifierSetPrototype? activeBlockModifier) && blockingComponent.IsBlocking)
|
|
{
|
|
args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, activeBlockModifier);
|
|
_audio.PlayPvs(blockingComponent.BlockSound, component.Owner, AudioParams.Default.WithVariation(0.2f));
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|