From bb9bc103425fb5d5585c94af05530313744c0074 Mon Sep 17 00:00:00 2001 From: FunTust <49007663+FunTust@users.noreply.github.com> Date: Wed, 11 Sep 2024 00:46:47 +0300 Subject: [PATCH] add extended melee weapon logic (#1831) * add extended weapon logic * added a division by type of the attack * fixes * check fix --- .../KnockingWeaponOutOfHandsComponent.cs | 16 ++++++ .../Systems/KnockingWeaponOutOfHandsSystem.cs | 52 +++++++++++++++++++ .../Weapons/Melee/Events/AttackEvent.cs | 43 +++++++++++++++ .../Weapons/Melee/SharedMeleeWeaponSystem.cs | 10 ++++ 4 files changed, 121 insertions(+) create mode 100644 Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Components/KnockingWeaponOutOfHandsComponent.cs create mode 100644 Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Systems/KnockingWeaponOutOfHandsSystem.cs diff --git a/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Components/KnockingWeaponOutOfHandsComponent.cs b/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Components/KnockingWeaponOutOfHandsComponent.cs new file mode 100644 index 00000000000000..c7ec55a7134ec5 --- /dev/null +++ b/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Components/KnockingWeaponOutOfHandsComponent.cs @@ -0,0 +1,16 @@ +// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt +namespace Content.Shared.SS220.Weapons.Melee.KnockingWeaponOutOfHands.Components; + +[RegisterComponent] + +public sealed partial class KnockingWeaponOutOfHandsComponent : Component +{ + [DataField] + public bool DropOnHeavyAtack = true; + + [DataField] + public bool DropOnLightAtack = true; + + [DataField("chance", required: true)] + public float Chance; +} diff --git a/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Systems/KnockingWeaponOutOfHandsSystem.cs b/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Systems/KnockingWeaponOutOfHandsSystem.cs new file mode 100644 index 00000000000000..508d31def7cdd0 --- /dev/null +++ b/Content.Shared/SS220/Weapons/Melee/KnockingWeaponOutOfHands/Systems/KnockingWeaponOutOfHandsSystem.cs @@ -0,0 +1,52 @@ +// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt +using Content.Shared.SS220.Weapons.Melee.KnockingWeaponOutOfHands.Components; +using Content.Shared.Hands.EntitySystems; +using Content.Shared.Inventory; +using Content.Shared.Weapons.Melee; +using Content.Shared.Weapons.Melee.Events; +using Content.Shared.Weapons.Ranged.Components; +using Robust.Shared.Random; + +namespace Content.Shared.SS220.Weapons.Melee.KnockingWeaponOutOfHands.Systems; + +public sealed class KnockingWeaponOutOfHandsSystem : EntitySystem +{ + [Dependency] private readonly InventorySystem _inventory = default!; + [Dependency] private readonly SharedHandsSystem _handsSystem = default!; + [Dependency] private readonly IRobustRandom _random = default!; + + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnAttackEvent); + } + + private void OnAttackEvent(Entity entity, ref WeaponAttackEvent args) + { + switch(args.Type) + { + case AttackType.HEAVY: + if(entity.Comp.DropOnHeavyAtack) + DropOnAtack(entity, args.Target); + break; + case AttackType.LIGHT: + if(entity.Comp.DropOnLightAtack) + DropOnAtack(entity, args.Target); + break; + } + } + + private void DropOnAtack(Entity entity, EntityUid target) + { + foreach (var handOrInventoryEntity in _inventory.GetHandOrInventoryEntities(target, SlotFlags.POCKET)) + { + if (!HasComp(handOrInventoryEntity) + || !HasComp(handOrInventoryEntity)) + continue; + if (!_random.Prob(entity.Comp.Chance)) + continue; + _handsSystem.TryDrop(target, handOrInventoryEntity); + } + } +} diff --git a/Content.Shared/Weapons/Melee/Events/AttackEvent.cs b/Content.Shared/Weapons/Melee/Events/AttackEvent.cs index 11720be2ee8cfd..d9a04a73111248 100644 --- a/Content.Shared/Weapons/Melee/Events/AttackEvent.cs +++ b/Content.Shared/Weapons/Melee/Events/AttackEvent.cs @@ -51,4 +51,47 @@ public AttackedEvent(EntityUid used, EntityUid user, EntityCoordinates clickLoca public bool Cancelled = false; // SS220 hook attack event end } + //ss220 extended weapon logic start + public enum AttackType + { + NONE = 0, + LIGHT = 1 << 0, + HEAVY = 1 << 1, + + All = ~NONE + } + + /// + /// SS220 Event raised on user for extended melee weapon logic. + /// + [Serializable] + public sealed class WeaponAttackEvent : EntityEventArgs + { + /// + /// Entity that triggered the attack. + /// + public EntityUid User { get; } + + /// + /// Entity that was attacked. + /// + public EntityUid Target { get; } + + /// + /// Type of the attack that been used. + /// + public AttackType Type { get; } + + /// + /// The original location that was clicked by the user. + /// + + public WeaponAttackEvent(EntityUid user, EntityUid target, AttackType type) + { + User = user; + Target = target; + Type = type; + } + } + //ss220 extended weapon logic end } diff --git a/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs b/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs index 72265038d939d5..852f63b0f9dd17 100644 --- a/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs +++ b/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs @@ -508,6 +508,11 @@ protected virtual void DoLightAttack(EntityUid user, LightAttackEvent ev, Entity var attackedEvent = new AttackedEvent(meleeUid, user, targetXform.Coordinates); RaiseLocalEvent(target.Value, attackedEvent); + //ss220 extended weapon logic start + var weaponAttackEvent = new WeaponAttackEvent(user, target.Value, AttackType.LIGHT); + RaiseLocalEvent(meleeUid, weaponAttackEvent); + //ss220 extended weapon logic end + // SS220 hook attack event start if (attackedEvent.Cancelled) return; @@ -658,6 +663,11 @@ private bool DoHeavyAttack(EntityUid user, HeavyAttackEvent ev, EntityUid meleeU var attackedEvent = new AttackedEvent(meleeUid, user, GetCoordinates(ev.Coordinates)); RaiseLocalEvent(entity, attackedEvent); + //ss220 extended weapon logic start + var weaponAttackEvent = new WeaponAttackEvent(user, entity, AttackType.HEAVY); + RaiseLocalEvent(meleeUid, weaponAttackEvent); + //ss220 extended weapon logic end + // SS220 hook attack event start if (attackedEvent.Cancelled) continue;