Melee refactor (#10897)

Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2022-09-29 15:51:59 +10:00
committed by GitHub
parent c583b7b361
commit f51248ecaa
140 changed files with 2440 additions and 1824 deletions

View File

@@ -15,16 +15,5 @@ namespace Content.Shared.CombatMode
public TargetingZone TargetZone { get; }
}
[Serializable, NetSerializable]
public sealed class SetCombatModeActiveMessage : EntityEventArgs
{
public SetCombatModeActiveMessage(bool active)
{
Active = active;
}
public bool Active { get; }
}
}
}

View File

@@ -0,0 +1,7 @@
using Content.Shared.Actions;
namespace Content.Shared.CombatMode;
public sealed class TogglePrecisionModeEvent : InstantActionEvent
{
}

View File

@@ -1,4 +1,3 @@
using Content.Shared.CombatMode;
using Content.Shared.Actions;
namespace Content.Shared.CombatMode.Pacification
@@ -18,11 +17,9 @@ namespace Content.Shared.CombatMode.Pacification
if (!TryComp<SharedCombatModeComponent>(uid, out var combatMode))
return;
if (combatMode.DisarmAction != null)
{
_actionsSystem.SetToggled(combatMode.DisarmAction, false);
_actionsSystem.SetEnabled(combatMode.DisarmAction, false);
}
if (combatMode.CanDisarm != null)
combatMode.CanDisarm = false;
if (combatMode.CombatToggleAction != null)
{
combatMode.IsInCombatMode = false;
@@ -35,8 +32,8 @@ namespace Content.Shared.CombatMode.Pacification
if (!TryComp<SharedCombatModeComponent>(uid, out var combatMode))
return;
if (combatMode.DisarmAction != null)
_actionsSystem.SetEnabled(combatMode.DisarmAction, true);
if (combatMode.CanDisarm != null)
combatMode.CanDisarm = true;
if (combatMode.CombatToggleAction != null)
_actionsSystem.SetEnabled(combatMode.CombatToggleAction, true);
}

View File

@@ -5,17 +5,21 @@ using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Utility;
namespace Content.Shared.CombatMode
{
[NetworkedComponent()]
public abstract class SharedCombatModeComponent : Component
{
private bool _isInCombatMode;
private TargetingZone _activeZone;
#region Disarm
[DataField("disarmFailChance")]
public readonly float BaseDisarmFailChance = 0.75f;
/// <summary>
/// Whether we are able to disarm. This requires our active hand to be free.
/// False if it's toggled off for whatever reason, null if it's not possible.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("disarm")]
public bool? CanDisarm;
[DataField("disarmFailSound")]
public readonly SoundSpecifier DisarmFailSound = new SoundPathSpecifier("/Audio/Weapons/punchmiss.ogg");
@@ -23,14 +27,13 @@ namespace Content.Shared.CombatMode
[DataField("disarmSuccessSound")]
public readonly SoundSpecifier DisarmSuccessSound = new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg");
[DataField("disarmActionId", customTypeSerializer:typeof(PrototypeIdSerializer<EntityTargetActionPrototype>))]
public readonly string DisarmActionId = "Disarm";
[DataField("disarmFailChance")]
public readonly float BaseDisarmFailChance = 0.75f;
[DataField("canDisarm")]
public bool CanDisarm;
#endregion
[DataField("disarmAction")] // must be a data-field to properly save cooldown when saving game state.
public EntityTargetAction? DisarmAction;
private bool _isInCombatMode;
private TargetingZone _activeZone;
[DataField("combatToggleActionId", customTypeSerializer: typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public readonly string CombatToggleActionId = "CombatModeToggle";
@@ -49,19 +52,6 @@ namespace Content.Shared.CombatMode
if (CombatToggleAction != null)
EntitySystem.Get<SharedActionsSystem>().SetToggled(CombatToggleAction, _isInCombatMode);
Dirty();
// Regenerate physics contacts -> Can probably just selectively check
/* Still a bit jank so left disabled for now.
if (Owner.TryGetComponent(out PhysicsComponent? physicsComponent))
{
if (value)
{
physicsComponent.WakeBody();
}
physicsComponent.RegenerateContacts();
}
*/
}
}
@@ -76,35 +66,5 @@ namespace Content.Shared.CombatMode
Dirty();
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not CombatModeComponentState state)
return;
IsInCombatMode = state.IsInCombatMode;
ActiveZone = state.TargetingZone;
}
public override ComponentState GetComponentState()
{
return new CombatModeComponentState(IsInCombatMode, ActiveZone);
}
[Serializable, NetSerializable]
protected sealed class CombatModeComponentState : ComponentState
{
public bool IsInCombatMode { get; }
public TargetingZone TargetingZone { get; }
public CombatModeComponentState(bool isInCombatMode, TargetingZone targetingZone)
{
IsInCombatMode = isInCombatMode;
TargetingZone = targetingZone;
}
}
}
}

View File

@@ -1,21 +1,20 @@
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Targeting;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.CombatMode
{
public abstract class SharedCombatModeSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<CombatModeSystemMessages.SetCombatModeActiveMessage>(CombatModeActiveHandler);
SubscribeLocalEvent<CombatModeSystemMessages.SetCombatModeActiveMessage>(CombatModeActiveHandler);
SubscribeLocalEvent<SharedCombatModeComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<SharedCombatModeComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<SharedCombatModeComponent, ToggleCombatActionEvent>(OnActionPerform);
@@ -31,30 +30,17 @@ namespace Content.Shared.CombatMode
if (component.CombatToggleAction != null)
_actionsSystem.AddAction(uid, component.CombatToggleAction, null);
if (component.DisarmAction == null
&& component.CanDisarm
&& _protoMan.TryIndex(component.DisarmActionId, out EntityTargetActionPrototype? disarmProto))
{
component.DisarmAction = new(disarmProto);
}
if (component.DisarmAction != null && component.CanDisarm)
_actionsSystem.AddAction(uid, component.DisarmAction, null);
}
private void OnShutdown(EntityUid uid, SharedCombatModeComponent component, ComponentShutdown args)
{
if (component.CombatToggleAction != null)
_actionsSystem.RemoveAction(uid, component.CombatToggleAction);
if (component.DisarmAction != null)
_actionsSystem.RemoveAction(uid, component.DisarmAction);
}
public bool IsInCombatMode(EntityUid entity)
public bool IsInCombatMode(EntityUid? entity, SharedCombatModeComponent? component = null)
{
return TryComp<SharedCombatModeComponent>(entity, out var combatMode) && combatMode.IsInCombatMode;
return entity != null && Resolve(entity.Value, ref component, false) && component.IsInCombatMode;
}
private void OnActionPerform(EntityUid uid, SharedCombatModeComponent component, ToggleCombatActionEvent args)
@@ -66,17 +52,19 @@ namespace Content.Shared.CombatMode
args.Handled = true;
}
private void CombatModeActiveHandler(CombatModeSystemMessages.SetCombatModeActiveMessage ev, EntitySessionEventArgs eventArgs)
[Serializable, NetSerializable]
protected sealed class CombatModeComponentState : ComponentState
{
var entity = eventArgs.SenderSession.AttachedEntity;
public bool IsInCombatMode { get; }
public TargetingZone TargetingZone { get; }
if (entity == null || !EntityManager.TryGetComponent(entity, out SharedCombatModeComponent? combatModeComponent))
return;
combatModeComponent.IsInCombatMode = ev.Active;
public CombatModeComponentState(bool isInCombatMode, TargetingZone targetingZone)
{
IsInCombatMode = isInCombatMode;
TargetingZone = targetingZone;
}
}
}
public sealed class ToggleCombatActionEvent : InstantActionEvent { }
public sealed class DisarmActionEvent : EntityTargetActionEvent { }
}

View File

@@ -22,7 +22,6 @@ namespace Content.Shared.Input
public static readonly BoundKeyFunction CycleChatChannelForward = "CycleChatChannelForward";
public static readonly BoundKeyFunction CycleChatChannelBackward = "CycleChatChannelBackward";
public static readonly BoundKeyFunction OpenCharacterMenu = "OpenCharacterMenu";
public static readonly BoundKeyFunction OpenContextMenu = "OpenContextMenu";
public static readonly BoundKeyFunction OpenCraftingMenu = "OpenCraftingMenu";
public static readonly BoundKeyFunction OpenInventoryMenu = "OpenInventoryMenu";
public static readonly BoundKeyFunction SmartEquipBackpack = "SmartEquipBackpack";

View File

@@ -198,12 +198,9 @@ namespace Content.Shared.Interaction
if (target != null && Deleted(target.Value))
return;
// TODO COMBAT Consider using alt-interact for advanced combat? maybe alt-interact disarms?
if (!altInteract && TryComp(user, out SharedCombatModeComponent? combatMode) && combatMode.IsInCombatMode)
if (TryComp(user, out SharedCombatModeComponent? combatMode) && combatMode.IsInCombatMode)
{
// Wide attack if there isn't a target or the target is out of range, click attack otherwise.
var shouldWideAttack = target == null || !InRangeUnobstructed(user, target.Value);
DoAttack(user, coordinates, shouldWideAttack, target);
// Eat the input
return;
}
@@ -300,12 +297,6 @@ namespace Content.Shared.Interaction
checkAccess: false);
}
public virtual void DoAttack(EntityUid user, EntityCoordinates coordinates, bool wideAttack,
EntityUid? targetUid = null)
{
// TODO PREDICTION move server-side interaction logic into the shared system for interaction prediction.
}
public void InteractUsingRanged(EntityUid user, EntityUid used, EntityUid? target,
EntityCoordinates clickLocation, bool inRangeUnobstructed)
{

View File

@@ -1,3 +1,4 @@
using Content.Shared.CombatMode;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
@@ -9,8 +10,9 @@ namespace Content.Shared.Item;
public abstract class SharedItemSystem : EntitySystem
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedCombatModeSystem _combatMode = default!;
[Dependency] protected readonly SharedContainerSystem Container = default!;
public override void Initialize()
{
@@ -69,7 +71,7 @@ public abstract class SharedItemSystem : EntitySystem
private void OnHandInteract(EntityUid uid, ItemComponent component, InteractHandEvent args)
{
if (args.Handled)
if (args.Handled || _combatMode.IsInCombatMode(args.User))
return;
args.Handled = _handsSystem.TryPickup(args.User, uid, animateUser: false);
@@ -121,8 +123,8 @@ public abstract class SharedItemSystem : EntitySystem
// if the item already in a container (that is not the same as the user's), then change the text.
// this occurs when the item is in their inventory or in an open backpack
_container.TryGetContainingContainer(args.User, out var userContainer);
if (_container.TryGetContainingContainer(args.Target, out var container) && container != userContainer)
Container.TryGetContainingContainer(args.User, out var userContainer);
if (Container.TryGetContainingContainer(args.Target, out var container) && container != userContainer)
verb.Text = Loc.GetString("pick-up-verb-get-data-text-inventory");
else
verb.Text = Loc.GetString("pick-up-verb-get-data-text");

View File

@@ -29,7 +29,6 @@ public sealed class ThrowingSystem : EntitySystem
/// <param name="uid">The entity being thrown.</param>
/// <param name="direction">A vector pointing from the entity to its destination.</param>
/// <param name="strength">How much the direction vector should be multiplied for velocity.</param>
/// <param name="user"></param>
/// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param>
public void TryThrow(
EntityUid uid,

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}

View 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;
}
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -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,
}
}

View 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;
}
}

View File

@@ -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;
}
}
}
}

View 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);
}

View File

@@ -1,6 +1,6 @@
using Content.Shared.Roles;
using Content.Shared.Weapons.Melee;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Zombies
@@ -52,8 +52,8 @@ namespace Content.Shared.Zombies
/// <summary>
/// The attack arc of the zombie
/// </summary>
[DataField("attackArc", customTypeSerializer: typeof(PrototypeIdSerializer<MeleeWeaponAnimationPrototype>))]
public string AttackArc = "claw";
[DataField("attackArc", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string AttackAnimation = "WeaponArcClaw";
/// <summary>
/// The role prototype of the zombie antag role