Melee refactor (#10897)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee
|
||||
{
|
||||
/// <summary>
|
||||
/// Raised directed on the used entity when a target entity is click attacked by a user.
|
||||
/// </summary>
|
||||
public sealed class ClickAttackEvent : HandledEntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Entity used to attack, for broadcast purposes.
|
||||
/// </summary>
|
||||
public EntityUid Used { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Entity that triggered the attack.
|
||||
/// </summary>
|
||||
public EntityUid User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The original location that was clicked by the user.
|
||||
/// </summary>
|
||||
public EntityCoordinates ClickLocation { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity that was attacked.
|
||||
/// </summary>
|
||||
public EntityUid? Target { get; }
|
||||
|
||||
public ClickAttackEvent(EntityUid used, EntityUid user, EntityCoordinates clickLocation, EntityUid? target = null)
|
||||
{
|
||||
Used = used;
|
||||
User = user;
|
||||
ClickLocation = clickLocation;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised directed on the used entity when a target entity is wide attacked by a user.
|
||||
/// </summary>
|
||||
public sealed class WideAttackEvent : HandledEntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Entity used to attack, for broadcast purposes.
|
||||
/// </summary>
|
||||
public EntityUid Used { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Entity that triggered the attack.
|
||||
/// </summary>
|
||||
public EntityUid User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The original location that was clicked by the user.
|
||||
/// </summary>
|
||||
public EntityCoordinates ClickLocation { get; }
|
||||
|
||||
public WideAttackEvent(EntityUid used, EntityUid user, EntityCoordinates clickLocation)
|
||||
{
|
||||
Used = used;
|
||||
User = user;
|
||||
ClickLocation = clickLocation;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on entities that have been attacked.
|
||||
/// </summary>
|
||||
public sealed class AttackedEvent : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Entity used to attack, for broadcast purposes.
|
||||
/// </summary>
|
||||
public EntityUid Used { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Entity that triggered the attack.
|
||||
/// </summary>
|
||||
public EntityUid User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The original location that was clicked by the user.
|
||||
/// </summary>
|
||||
public EntityCoordinates ClickLocation { get; }
|
||||
|
||||
public AttackedEvent(EntityUid used, EntityUid user, EntityCoordinates clickLocation)
|
||||
{
|
||||
Used = used;
|
||||
User = user;
|
||||
ClickLocation = clickLocation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,16 @@ namespace Content.Shared.Weapons.Melee;
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DamageEffectEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid Entity;
|
||||
/// <summary>
|
||||
/// Color to play for the damage flash.
|
||||
/// </summary>
|
||||
public Color Color;
|
||||
|
||||
public DamageEffectEvent(EntityUid entity)
|
||||
public List<EntityUid> Entities;
|
||||
|
||||
public DamageEffectEvent(Color color, List<EntityUid> entities)
|
||||
{
|
||||
Entity = entity;
|
||||
Color = color;
|
||||
Entities = entities;
|
||||
}
|
||||
}
|
||||
|
||||
47
Content.Shared/Weapons/Melee/Events/AttackEvent.cs
Normal file
47
Content.Shared/Weapons/Melee/Events/AttackEvent.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
public abstract class AttackEvent : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Coordinates being attacked.
|
||||
/// </summary>
|
||||
public readonly EntityCoordinates Coordinates;
|
||||
|
||||
protected AttackEvent(EntityCoordinates coordinates)
|
||||
{
|
||||
Coordinates = coordinates;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on entities that have been attacked.
|
||||
/// </summary>
|
||||
public sealed class AttackedEvent : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Entity used to attack, for broadcast purposes.
|
||||
/// </summary>
|
||||
public EntityUid Used { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Entity that triggered the attack.
|
||||
/// </summary>
|
||||
public EntityUid User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The original location that was clicked by the user.
|
||||
/// </summary>
|
||||
public EntityCoordinates ClickLocation { get; }
|
||||
|
||||
public AttackedEvent(EntityUid used, EntityUid user, EntityCoordinates clickLocation)
|
||||
{
|
||||
Used = used;
|
||||
User = user;
|
||||
ClickLocation = clickLocation;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Content.Shared/Weapons/Melee/Events/DisarmAttackEvent.cs
Normal file
15
Content.Shared/Weapons/Melee/Events/DisarmAttackEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DisarmAttackEvent : AttackEvent
|
||||
{
|
||||
public EntityUid? Target;
|
||||
|
||||
public DisarmAttackEvent(EntityUid? target, EntityCoordinates coordinates) : base(coordinates)
|
||||
{
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
18
Content.Shared/Weapons/Melee/Events/HeavyAttackEvent.cs
Normal file
18
Content.Shared/Weapons/Melee/Events/HeavyAttackEvent.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Raised on the client when it attempts a heavy attack.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class HeavyAttackEvent : AttackEvent
|
||||
{
|
||||
public readonly EntityUid Weapon;
|
||||
|
||||
public HeavyAttackEvent(EntityUid weapon, EntityCoordinates coordinates) : base(coordinates)
|
||||
{
|
||||
Weapon = weapon;
|
||||
}
|
||||
}
|
||||
20
Content.Shared/Weapons/Melee/Events/LightAttackEvent.cs
Normal file
20
Content.Shared/Weapons/Melee/Events/LightAttackEvent.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when a light attack is made.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class LightAttackEvent : AttackEvent
|
||||
{
|
||||
public readonly EntityUid? Target;
|
||||
public readonly EntityUid Weapon;
|
||||
|
||||
public LightAttackEvent(EntityUid? target, EntityUid weapon, EntityCoordinates coordinates) : base(coordinates)
|
||||
{
|
||||
Target = target;
|
||||
Weapon = weapon;
|
||||
}
|
||||
}
|
||||
35
Content.Shared/Weapons/Melee/Events/MeleeLungeEvent.cs
Normal file
35
Content.Shared/Weapons/Melee/Events/MeleeLungeEvent.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Data for melee lunges from attacks.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class MeleeLungeEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid Entity;
|
||||
|
||||
/// <summary>
|
||||
/// Width of the attack angle.
|
||||
/// </summary>
|
||||
public Angle Angle;
|
||||
|
||||
/// <summary>
|
||||
/// The relative local position to the <see cref="Entity"/>
|
||||
/// </summary>
|
||||
public Vector2 LocalPos;
|
||||
|
||||
/// <summary>
|
||||
/// Entity to spawn for the animation
|
||||
/// </summary>
|
||||
public string? Animation;
|
||||
|
||||
public MeleeLungeEvent(EntityUid uid, Angle angle, Vector2 localPos, string? animation)
|
||||
{
|
||||
Entity = uid;
|
||||
Angle = angle;
|
||||
LocalPos = localPos;
|
||||
Animation = animation;
|
||||
}
|
||||
}
|
||||
14
Content.Shared/Weapons/Melee/Events/StartHeavyAttackEvent.cs
Normal file
14
Content.Shared/Weapons/Melee/Events/StartHeavyAttackEvent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class StartHeavyAttackEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid Weapon;
|
||||
|
||||
public StartHeavyAttackEvent(EntityUid weapon)
|
||||
{
|
||||
Weapon = weapon;
|
||||
}
|
||||
}
|
||||
14
Content.Shared/Weapons/Melee/Events/StopAttackEvent.cs
Normal file
14
Content.Shared/Weapons/Melee/Events/StopAttackEvent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class StopAttackEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid Weapon;
|
||||
|
||||
public StopAttackEvent(EntityUid weapon)
|
||||
{
|
||||
Weapon = weapon;
|
||||
}
|
||||
}
|
||||
17
Content.Shared/Weapons/Melee/Events/StopHeavyAttackEvent.cs
Normal file
17
Content.Shared/Weapons/Melee/Events/StopHeavyAttackEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Raised by the client if it pre-emptively stops a heavy attack.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class StopHeavyAttackEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid Weapon;
|
||||
|
||||
public StopHeavyAttackEvent(EntityUid weapon)
|
||||
{
|
||||
Weapon = weapon;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee
|
||||
{
|
||||
[Prototype("MeleeWeaponAnimation")]
|
||||
public sealed class MeleeWeaponAnimationPrototype : IPrototype
|
||||
{
|
||||
[ViewVariables]
|
||||
[IdDataFieldAttribute]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("state")]
|
||||
public string State { get; } = string.Empty;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("prototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string Prototype { get; } = "WeaponArc";
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("length")]
|
||||
public TimeSpan Length { get; } = TimeSpan.FromSeconds(0.5f);
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("speed")]
|
||||
public float Speed { get; } = 1;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("color")]
|
||||
public Vector4 Color { get; } = new(1,1,1,1);
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("colorDelta")]
|
||||
public Vector4 ColorDelta { get; } = Vector4.Zero;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("arcType")]
|
||||
public WeaponArcType ArcType { get; } = WeaponArcType.Slash;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("width")]
|
||||
public float Width { get; } = 90;
|
||||
}
|
||||
|
||||
public enum WeaponArcType
|
||||
{
|
||||
Slash,
|
||||
Poke,
|
||||
}
|
||||
}
|
||||
146
Content.Shared/Weapons/Melee/MeleeWeaponComponent.cs
Normal file
146
Content.Shared/Weapons/Melee/MeleeWeaponComponent.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee;
|
||||
|
||||
/// <summary>
|
||||
/// When given to a mob lets them do unarmed attacks, or when given to an item lets someone wield it to do attacks.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class MeleeWeaponComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Should the melee weapon's damage stats be examinable.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("hidden")]
|
||||
public bool HideFromExamine { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Next time this component is allowed to light attack. Heavy attacks are wound up and never have a cooldown.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("nextAttack")]
|
||||
public TimeSpan NextAttack;
|
||||
|
||||
/*
|
||||
* Melee combat works based around 2 types of attacks:
|
||||
* 1. Click attacks with left-click. This attacks whatever is under your mnouse
|
||||
* 2. Wide attacks with right-click + left-click. This attacks whatever is in the direction of your mouse.
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// How many times we can attack per second.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("attackRate")]
|
||||
public float AttackRate = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// Are we currently holding down the mouse for an attack.
|
||||
/// Used so we can't just hold the mouse button and attack constantly.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool Attacking = false;
|
||||
|
||||
/// <summary>
|
||||
/// When did we start a heavy attack.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("windUpStart")]
|
||||
public TimeSpan? WindUpStart;
|
||||
|
||||
/// <summary>
|
||||
/// How long it takes a heavy attack to windup.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public TimeSpan WindupTime => AttackRate > 0 ? TimeSpan.FromSeconds(1 / AttackRate * HeavyWindupModifier) : TimeSpan.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// Heavy attack windup time gets multiplied by this value and the light attack cooldown.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("heavyWindupModifier")]
|
||||
public float HeavyWindupModifier = 1.5f;
|
||||
|
||||
/// <summary>
|
||||
/// Light attacks get multiplied by this over the base <see cref="Damage"/> value.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("heavyDamageModifier")]
|
||||
public FixedPoint2 HeavyDamageModifier = FixedPoint2.New(2);
|
||||
|
||||
/// <summary>
|
||||
/// Base damage for this weapon. Can be modified via heavy damage or other means.
|
||||
/// </summary>
|
||||
[DataField("damage", required:true)]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public DamageSpecifier Damage = default!;
|
||||
|
||||
[DataField("bluntStaminaDamageFactor")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public FixedPoint2 BluntStaminaDamageFactor { get; set; } = 0.5f;
|
||||
|
||||
/// <summary>
|
||||
/// Nearest edge range to hit an entity.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("range")]
|
||||
public float Range = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// Total width of the angle for wide attacks.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("angle")]
|
||||
public Angle Angle = Angle.FromDegrees(60);
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("animation", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string Animation = "WeaponArcSlash";
|
||||
|
||||
// Sounds
|
||||
|
||||
/// <summary>
|
||||
/// This gets played whenever a melee attack is done. This is predicted by the client.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("soundSwing")]
|
||||
public SoundSpecifier SwingSound { get; set; } = new SoundPathSpecifier("/Audio/Weapons/punchmiss.ogg")
|
||||
{
|
||||
Params = AudioParams.Default.WithVolume(-3f).WithVariation(0.025f),
|
||||
};
|
||||
|
||||
// We do not predict the below sounds in case the client thinks but the server disagrees. If this were the case
|
||||
// then a player may doubt if the target actually took damage or not.
|
||||
// If overwatch and apex do this then we probably should too.
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("soundHit")]
|
||||
public SoundSpecifier? HitSound;
|
||||
|
||||
/// <summary>
|
||||
/// Plays if no damage is done to the target entity.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("soundNoDamage")]
|
||||
public SoundSpecifier NoDamageSound { get; set; } = new SoundPathSpecifier("/Audio/Weapons/tap.ogg");
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class MeleeWeaponComponentState : ComponentState
|
||||
{
|
||||
// None of the other data matters for client as they're not predicted.
|
||||
|
||||
public float AttackRate;
|
||||
public bool Attacking;
|
||||
public TimeSpan NextAttack;
|
||||
public TimeSpan? WindUpStart;
|
||||
|
||||
public MeleeWeaponComponentState(float attackRate, bool attacking, TimeSpan nextAttack, TimeSpan? windupStart)
|
||||
{
|
||||
AttackRate = attackRate;
|
||||
Attacking = attacking;
|
||||
NextAttack = nextAttack;
|
||||
WindUpStart = windupStart;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee
|
||||
{
|
||||
public static class MeleeWeaponSystemMessages
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class PlayMeleeWeaponAnimationMessage : EntityEventArgs
|
||||
{
|
||||
public PlayMeleeWeaponAnimationMessage(string arcPrototype, Angle angle, EntityUid attacker, EntityUid source, List<EntityUid> hits, bool textureEffect = false, bool arcFollowAttacker = true)
|
||||
{
|
||||
ArcPrototype = arcPrototype;
|
||||
Angle = angle;
|
||||
Attacker = attacker;
|
||||
Source = source;
|
||||
Hits = hits;
|
||||
TextureEffect = textureEffect;
|
||||
ArcFollowAttacker = arcFollowAttacker;
|
||||
}
|
||||
|
||||
public string ArcPrototype { get; }
|
||||
public Angle Angle { get; }
|
||||
public EntityUid Attacker { get; }
|
||||
public EntityUid Source { get; }
|
||||
public List<EntityUid> Hits { get; }
|
||||
public bool TextureEffect { get; }
|
||||
public bool ArcFollowAttacker { get; }
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class PlayLungeAnimationMessage : EntityEventArgs
|
||||
{
|
||||
public Angle Angle { get; }
|
||||
public EntityUid Source { get; }
|
||||
|
||||
public PlayLungeAnimationMessage(Angle angle, EntityUid source)
|
||||
{
|
||||
Angle = angle;
|
||||
Source = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
325
Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs
Normal file
325
Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs
Normal file
@@ -0,0 +1,325 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.CombatMode;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Weapons.Melee.Events;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Weapons.Melee;
|
||||
|
||||
public abstract class SharedMeleeWeaponSystem : EntitySystem
|
||||
{
|
||||
[Dependency] protected readonly IGameTiming Timing = default!;
|
||||
[Dependency] protected readonly IMapManager MapManager = default!;
|
||||
[Dependency] protected readonly ActionBlockerSystem Blocker = default!;
|
||||
[Dependency] protected readonly SharedAudioSystem Audio = default!;
|
||||
[Dependency] protected readonly SharedCombatModeSystem CombatMode = default!;
|
||||
[Dependency] protected readonly SharedPopupSystem PopupSystem = default!;
|
||||
|
||||
protected ISawmill Sawmill = default!;
|
||||
|
||||
/// <summary>
|
||||
/// If an attack is released within this buffer it's assumed to be full damage.
|
||||
/// </summary>
|
||||
public const float GracePeriod = 0.05f;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Sawmill = Logger.GetSawmill("melee");
|
||||
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
SubscribeAllEvent<LightAttackEvent>(OnLightAttack);
|
||||
SubscribeAllEvent<StartHeavyAttackEvent>(OnStartHeavyAttack);
|
||||
SubscribeAllEvent<StopHeavyAttackEvent>(OnStopHeavyAttack);
|
||||
SubscribeAllEvent<HeavyAttackEvent>(OnHeavyAttack);
|
||||
SubscribeAllEvent<DisarmAttackEvent>(OnDisarmAttack);
|
||||
SubscribeAllEvent<StopAttackEvent>(OnStopAttack);
|
||||
}
|
||||
|
||||
private void OnStopAttack(StopAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
var user = args.SenderSession.AttachedEntity;
|
||||
|
||||
if (user == null)
|
||||
return;
|
||||
|
||||
var weapon = GetWeapon(user.Value);
|
||||
|
||||
if (weapon?.Owner != msg.Weapon)
|
||||
return;
|
||||
|
||||
if (!weapon.Attacking)
|
||||
return;
|
||||
|
||||
weapon.Attacking = false;
|
||||
Dirty(weapon);
|
||||
}
|
||||
|
||||
private void OnStartHeavyAttack(StartHeavyAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
var user = args.SenderSession.AttachedEntity;
|
||||
|
||||
if (user == null)
|
||||
return;
|
||||
|
||||
var weapon = GetWeapon(user.Value);
|
||||
|
||||
if (weapon?.Owner != msg.Weapon)
|
||||
return;
|
||||
|
||||
DebugTools.Assert(weapon.WindUpStart == null);
|
||||
weapon.WindUpStart = Timing.CurTime;
|
||||
Dirty(weapon);
|
||||
}
|
||||
|
||||
protected abstract void Popup(string message, EntityUid? uid, EntityUid? user);
|
||||
|
||||
private void OnLightAttack(LightAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
var user = args.SenderSession.AttachedEntity;
|
||||
|
||||
if (user == null)
|
||||
return;
|
||||
|
||||
var weapon = GetWeapon(user.Value);
|
||||
|
||||
if (weapon?.Owner != msg.Weapon)
|
||||
return;
|
||||
|
||||
AttemptAttack(args.SenderSession.AttachedEntity!.Value, weapon, msg);
|
||||
}
|
||||
|
||||
private void OnStopHeavyAttack(StopHeavyAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (args.SenderSession.AttachedEntity == null ||
|
||||
!TryComp<MeleeWeaponComponent>(msg.Weapon, out var weapon))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var userWeapon = GetWeapon(args.SenderSession.AttachedEntity.Value);
|
||||
|
||||
if (userWeapon != weapon)
|
||||
return;
|
||||
|
||||
if (weapon.WindUpStart.Equals(null))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
weapon.WindUpStart = null;
|
||||
Dirty(weapon);
|
||||
}
|
||||
|
||||
private void OnHeavyAttack(HeavyAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (args.SenderSession.AttachedEntity == null ||
|
||||
!TryComp<MeleeWeaponComponent>(msg.Weapon, out var weapon))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var userWeapon = GetWeapon(args.SenderSession.AttachedEntity.Value);
|
||||
|
||||
if (userWeapon != weapon)
|
||||
return;
|
||||
|
||||
AttemptAttack(args.SenderSession.AttachedEntity.Value, weapon, msg);
|
||||
}
|
||||
|
||||
private void OnDisarmAttack(DisarmAttackEvent msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (args.SenderSession.AttachedEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var userWeapon = GetWeapon(args.SenderSession.AttachedEntity.Value);
|
||||
|
||||
if (userWeapon == null)
|
||||
return;
|
||||
|
||||
AttemptAttack(args.SenderSession.AttachedEntity.Value, userWeapon, msg);
|
||||
}
|
||||
|
||||
private void OnGetState(EntityUid uid, MeleeWeaponComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new MeleeWeaponComponentState(component.AttackRate, component.Attacking, component.NextAttack,
|
||||
component.WindUpStart);
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, MeleeWeaponComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not MeleeWeaponComponentState state)
|
||||
return;
|
||||
|
||||
component.Attacking = state.Attacking;
|
||||
component.AttackRate = state.AttackRate;
|
||||
component.NextAttack = state.NextAttack;
|
||||
component.WindUpStart = state.WindUpStart;
|
||||
}
|
||||
|
||||
public MeleeWeaponComponent? GetWeapon(EntityUid entity)
|
||||
{
|
||||
MeleeWeaponComponent? melee;
|
||||
|
||||
// Use inhands entity if we got one.
|
||||
if (EntityManager.TryGetComponent(entity, out SharedHandsComponent? hands) &&
|
||||
hands.ActiveHandEntity is { } held)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(held, out melee))
|
||||
{
|
||||
return melee;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (TryComp(entity, out melee))
|
||||
{
|
||||
return melee;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void AttemptLightAttack(EntityUid user, MeleeWeaponComponent weapon, EntityUid target)
|
||||
{
|
||||
if (!TryComp<TransformComponent>(target, out var targetXform))
|
||||
return;
|
||||
|
||||
AttemptAttack(user, weapon, new LightAttackEvent(target, weapon.Owner, targetXform.Coordinates));
|
||||
}
|
||||
|
||||
public void AttemptDisarmAttack(EntityUid user, MeleeWeaponComponent weapon, EntityUid target)
|
||||
{
|
||||
if (!TryComp<TransformComponent>(target, out var targetXform))
|
||||
return;
|
||||
|
||||
AttemptAttack(user, weapon, new DisarmAttackEvent(target, targetXform.Coordinates));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a windup is finished and an attack is tried.
|
||||
/// </summary>
|
||||
private void AttemptAttack(EntityUid user, MeleeWeaponComponent weapon, AttackEvent attack)
|
||||
{
|
||||
var curTime = Timing.CurTime;
|
||||
|
||||
if (weapon.NextAttack > curTime)
|
||||
return;
|
||||
|
||||
if (!Blocker.CanAttack(user))
|
||||
return;
|
||||
|
||||
// Windup time checked elsewhere.
|
||||
|
||||
if (!CombatMode.IsInCombatMode(user))
|
||||
return;
|
||||
|
||||
if (weapon.NextAttack < curTime)
|
||||
weapon.NextAttack = curTime;
|
||||
|
||||
weapon.NextAttack += TimeSpan.FromSeconds(1f / weapon.AttackRate);
|
||||
|
||||
// Attack confirmed
|
||||
// Play a sound to give instant feedback; same with playing the animations
|
||||
Audio.PlayPredicted(weapon.SwingSound, weapon.Owner, user);
|
||||
|
||||
switch (attack)
|
||||
{
|
||||
case LightAttackEvent light:
|
||||
DoLightAttack(user, light, weapon);
|
||||
break;
|
||||
case DisarmAttackEvent disarm:
|
||||
DoDisarm(user, disarm, weapon);
|
||||
break;
|
||||
case HeavyAttackEvent heavy:
|
||||
DoHeavyAttack(user, heavy, weapon);
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
DoLungeAnimation(user, weapon.Angle, attack.Coordinates.ToMap(EntityManager), weapon.Animation);
|
||||
weapon.Attacking = true;
|
||||
Dirty(weapon);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When an attack is released get the actual modifier for damage done.
|
||||
/// </summary>
|
||||
public float GetModifier(MeleeWeaponComponent component, bool lightAttack)
|
||||
{
|
||||
if (lightAttack)
|
||||
return 1f;
|
||||
|
||||
var windup = component.WindUpStart;
|
||||
if (windup == null)
|
||||
return 0f;
|
||||
|
||||
var releaseTime = (Timing.CurTime - windup.Value).TotalSeconds;
|
||||
var windupTime = component.WindupTime.TotalSeconds;
|
||||
|
||||
// Wraps around back to 0
|
||||
releaseTime %= (2 * windupTime);
|
||||
|
||||
var releaseDiff = Math.Abs(releaseTime - windupTime);
|
||||
|
||||
if (releaseDiff < 0)
|
||||
releaseDiff = Math.Min(0, releaseDiff + GracePeriod);
|
||||
else
|
||||
releaseDiff = Math.Max(0, releaseDiff - GracePeriod);
|
||||
|
||||
var fraction = (windupTime - releaseDiff) / windupTime;
|
||||
|
||||
if (fraction < 0.4)
|
||||
fraction = 0;
|
||||
|
||||
DebugTools.Assert(fraction <= 1);
|
||||
return (float) fraction * component.HeavyDamageModifier.Float();
|
||||
}
|
||||
|
||||
protected virtual void DoLightAttack(EntityUid user, LightAttackEvent ev, MeleeWeaponComponent component)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual void DoHeavyAttack(EntityUid user, HeavyAttackEvent ev, MeleeWeaponComponent component)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual bool DoDisarm(EntityUid user, DisarmAttackEvent ev, MeleeWeaponComponent component)
|
||||
{
|
||||
if (Deleted(ev.Target) ||
|
||||
user == ev.Target)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void DoLungeAnimation(EntityUid user, Angle angle, MapCoordinates coordinates, string? animation)
|
||||
{
|
||||
// TODO: Assert that offset eyes are still okay.
|
||||
if (!TryComp<TransformComponent>(user, out var userXform))
|
||||
return;
|
||||
|
||||
var invMatrix = userXform.InvWorldMatrix;
|
||||
var localPos = invMatrix.Transform(coordinates.Position);
|
||||
|
||||
if (localPos.LengthSquared <= 0f)
|
||||
return;
|
||||
|
||||
localPos = userXform.LocalRotation.RotateVec(localPos);
|
||||
DoLunge(user, angle, localPos, animation);
|
||||
}
|
||||
|
||||
public abstract void DoLunge(EntityUid user, Angle angle, Vector2 localPos, string? animation);
|
||||
}
|
||||
Reference in New Issue
Block a user