Skip to content

Commit

Permalink
add extended melee weapon logic (#1831)
Browse files Browse the repository at this point in the history
* add extended weapon logic

* added a division by type of the attack

* fixes

* check fix
  • Loading branch information
FunTust authored Sep 10, 2024
1 parent 42b8387 commit bb9bc10
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<KnockingWeaponOutOfHandsComponent, WeaponAttackEvent>(OnAttackEvent);
}

private void OnAttackEvent(Entity<KnockingWeaponOutOfHandsComponent> 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<KnockingWeaponOutOfHandsComponent> entity, EntityUid target)
{
foreach (var handOrInventoryEntity in _inventory.GetHandOrInventoryEntities(target, SlotFlags.POCKET))
{
if (!HasComp<MeleeWeaponComponent>(handOrInventoryEntity)
|| !HasComp<GunComponent>(handOrInventoryEntity))
continue;
if (!_random.Prob(entity.Comp.Chance))
continue;
_handsSystem.TryDrop(target, handOrInventoryEntity);
}
}
}
43 changes: 43 additions & 0 deletions Content.Shared/Weapons/Melee/Events/AttackEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
/// SS220 Event raised on user for extended melee weapon logic.
/// </summary>
[Serializable]
public sealed class WeaponAttackEvent : EntityEventArgs
{
/// <summary>
/// Entity that triggered the attack.
/// </summary>
public EntityUid User { get; }

/// <summary>
/// Entity that was attacked.
/// </summary>
public EntityUid Target { get; }

/// <summary>
/// Type of the attack that been used.
/// </summary>
public AttackType Type { get; }

/// <summary>
/// The original location that was clicked by the user.
/// </summary>

public WeaponAttackEvent(EntityUid user, EntityUid target, AttackType type)
{
User = user;
Target = target;
Type = type;
}
}
//ss220 extended weapon logic end
}
10 changes: 10 additions & 0 deletions Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bb9bc10

Please sign in to comment.