Kick mines (real) (#8056)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Pieter-Jan Briers
2022-05-18 06:07:35 +02:00
committed by GitHub
parent 2f604ce05c
commit ebfe5e888f
29 changed files with 635 additions and 220 deletions

View File

@@ -1,13 +1,11 @@
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Inventory;
using Content.Shared.StatusEffect;
using Content.Shared.StepTrigger;
using Content.Shared.Stunnable;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;
namespace Content.Shared.Slippery
{
@@ -17,144 +15,68 @@ namespace Content.Shared.Slippery
[Dependency] private readonly SharedAdminLogSystem _adminLog = default!;
[Dependency] private readonly SharedStunSystem _stunSystem = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;
private readonly List<SlipperyComponent> _slipped = new();
[Dependency] private readonly SharedContainerSystem _container = default!;
public override void Initialize()
{
base.Initialize();
UpdatesOutsidePrediction = true;
SubscribeLocalEvent<SlipperyComponent, StartCollideEvent>(HandleCollide);
SubscribeLocalEvent<SlipperyComponent, StepTriggerAttemptEvent>(HandleAttemptCollide);
SubscribeLocalEvent<SlipperyComponent, StepTriggeredEvent>(HandleStepTrigger);
SubscribeLocalEvent<NoSlipComponent, SlipAttemptEvent>(OnNoSlipAttempt);
}
private void HandleCollide(EntityUid uid, SlipperyComponent component, StartCollideEvent args)
private void HandleStepTrigger(EntityUid uid, SlipperyComponent component, ref StepTriggeredEvent args)
{
var otherUid = args.OtherFixture.Body.Owner;
if (!CanSlip(component, otherUid)) return;
if (!_slipped.Contains(component))
_slipped.Add(component);
component.Colliding.Add(otherUid);
TrySlip(component, args.Tripper);
}
private void OnNoSlipAttempt(EntityUid uid, NoSlipComponent component, SlipAttemptEvent args)
private void HandleAttemptCollide(
EntityUid uid,
SlipperyComponent component,
ref StepTriggerAttemptEvent args)
{
args.Continue |= CanSlip(uid, args.Tripper);
}
private static void OnNoSlipAttempt(EntityUid uid, NoSlipComponent component, SlipAttemptEvent args)
{
args.Cancel();
}
/// <inheritdoc />
public override void Update(float frameTime)
private bool CanSlip(EntityUid uid, EntityUid toSlip)
{
for (var i = _slipped.Count - 1; i >= 0; i--)
{
var slipperyComp = _slipped[i];
if (!Update(slipperyComp)) continue;
_slipped.RemoveAt(i);
}
return !_container.IsEntityInContainer(uid)
&& _statusEffectsSystem.CanApplyEffect(toSlip, "Stun"); //Should be KnockedDown instead?
}
public bool CanSlip(SlipperyComponent component, EntityUid uid)
private void TrySlip(SlipperyComponent component, EntityUid other)
{
if (!component.Slippery
|| component.Owner.IsInContainer()
|| component.Slipped.Contains(uid)
|| !_statusEffectsSystem.CanApplyEffect(uid, "Stun")) //Should be KnockedDown instead?
{
return false;
}
return true;
}
private bool TrySlip(SlipperyComponent component, IPhysBody ourBody, IPhysBody otherBody)
{
if (!CanSlip(component, otherBody.Owner)) return false;
if (otherBody.LinearVelocity.Length < component.RequiredSlipSpeed)
{
return false;
}
var percentage = otherBody.GetWorldAABB().IntersectPercentage(ourBody.GetWorldAABB());
if (percentage < component.IntersectPercentage)
{
return false;
}
if (EntityManager.HasComponent<KnockedDownComponent>(otherBody.Owner))
{
return false;
}
if (HasComp<KnockedDownComponent>(other))
return;
var ev = new SlipAttemptEvent();
RaiseLocalEvent(otherBody.Owner, ev, false);
RaiseLocalEvent(other, ev, false);
if (ev.Cancelled)
return false;
return;
otherBody.LinearVelocity *= component.LaunchForwardsMultiplier;
if (TryComp(other, out PhysicsComponent? physics))
physics.LinearVelocity *= component.LaunchForwardsMultiplier;
bool playSound = !_statusEffectsSystem.HasStatusEffect(otherBody.Owner, "KnockedDown");
var playSound = !_statusEffectsSystem.HasStatusEffect(other, "KnockedDown");
_stunSystem.TryParalyze(otherBody.Owner, TimeSpan.FromSeconds(component.ParalyzeTime), true);
component.Slipped.Add(otherBody.Owner);
component.Dirty();
_stunSystem.TryParalyze(other, TimeSpan.FromSeconds(component.ParalyzeTime), true);
//Preventing from playing the slip sound when you are already knocked down.
if(playSound)
{
// Preventing from playing the slip sound when you are already knocked down.
if (playSound)
PlaySound(component);
}
_adminLog.Add(LogType.Slip, LogImpact.Low, $"{ToPrettyString(otherBody.Owner):mob} slipped on collision with {ToPrettyString(component.Owner):entity}");
return true;
_adminLog.Add(LogType.Slip, LogImpact.Low,
$"{ToPrettyString(other):mob} slipped on collision with {ToPrettyString(component.Owner):entity}");
}
// Until we get predicted slip sounds TM?
protected abstract void PlaySound(SlipperyComponent component);
private bool Update(SlipperyComponent component)
{
if (component.Deleted || !component.Slippery || component.Colliding.Count == 0)
return true;
if (!EntityManager.TryGetComponent(component.Owner, out PhysicsComponent? body))
{
component.Colliding.Clear();
return true;
}
foreach (var uid in component.Colliding.ToArray())
{
if (!uid.IsValid())
{
component.Colliding.Remove(uid);
component.Slipped.Remove(uid);
component.Dirty();
continue;
}
if (!EntityManager.TryGetComponent(uid, out PhysicsComponent? otherPhysics) ||
!body.GetWorldAABB().Intersects(otherPhysics.GetWorldAABB()))
{
component.Colliding.Remove(uid);
component.Slipped.Remove(uid);
component.Dirty();
continue;
}
if (!component.Slipped.Contains(uid))
TrySlip(component, body, otherPhysics);
}
return false;
}
}
/// <summary>

View File

@@ -1,31 +1,26 @@
using System.Linq;
using Content.Shared.Sound;
using Content.Shared.Sound;
using Content.Shared.StepTrigger;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Slippery
{
/// <summary>
/// Causes somebody to slip when they walk over this entity.
/// </summary>
/// <remarks>
/// Requires <see cref="StepTriggerComponent"/>, see that component for some additional properties.
/// </remarks>
[RegisterComponent]
[NetworkedComponent()]
[NetworkedComponent]
public sealed class SlipperyComponent : Component
{
private float _paralyzeTime = 3f;
private float _intersectPercentage = 0.3f;
private float _requiredSlipSpeed = 3.5f;
private float _launchForwardsMultiplier = 1f;
private bool _slippery = true;
private SoundSpecifier _slipSound = new SoundPathSpecifier("/Audio/Effects/slip.ogg");
/// <summary>
/// List of entities that are currently colliding with the entity.
/// </summary>
public readonly HashSet<EntityUid> Colliding = new();
/// <summary>
/// The list of entities that have been slipped by this component, which shouldn't be slipped again.
/// </summary>
public readonly HashSet<EntityUid> Slipped = new();
/// <summary>
/// Path to the sound to be played when a mob slips.
/// </summary>
@@ -61,40 +56,6 @@ namespace Content.Shared.Slippery
}
}
/// <summary>
/// Percentage of shape intersection for a slip to occur.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("intersectPercentage")]
public float IntersectPercentage
{
get => _intersectPercentage;
set
{
if (MathHelper.CloseToPercent(_intersectPercentage, value)) return;
_intersectPercentage = value;
Dirty();
}
}
/// <summary>
/// Entities will only be slipped if their speed exceeds this limit.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("requiredSlipSpeed")]
public float RequiredSlipSpeed
{
get => _requiredSlipSpeed;
set
{
if (MathHelper.CloseToPercent(_requiredSlipSpeed, value)) return;
_requiredSlipSpeed = value;
Dirty();
}
}
/// <summary>
/// The entity's speed will be multiplied by this to slip it forwards.
/// </summary>
@@ -112,44 +73,18 @@ namespace Content.Shared.Slippery
}
}
/// <summary>
/// Whether or not this component will try to slip entities.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("slippery")]
public bool Slippery
{
get => _slippery;
set
{
if (_slippery == value) return;
_slippery = value;
Dirty();
}
}
public override ComponentState GetComponentState()
{
return new SlipperyComponentState(ParalyzeTime, IntersectPercentage, RequiredSlipSpeed, LaunchForwardsMultiplier, Slippery, SlipSound.GetSound(), Slipped.ToArray());
return new SlipperyComponentState(ParalyzeTime, LaunchForwardsMultiplier, SlipSound.GetSound());
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not SlipperyComponentState state) return;
_slippery = state.Slippery;
_intersectPercentage = state.IntersectPercentage;
_paralyzeTime = state.ParalyzeTime;
_requiredSlipSpeed = state.RequiredSlipSpeed;
_launchForwardsMultiplier = state.LaunchForwardsMultiplier;
_slipSound = new SoundPathSpecifier(state.SlipSound);
Slipped.Clear();
foreach (var slipped in state.Slipped)
{
Slipped.Add(slipped);
}
}
}
@@ -157,22 +92,14 @@ namespace Content.Shared.Slippery
public sealed class SlipperyComponentState : ComponentState
{
public float ParalyzeTime { get; }
public float IntersectPercentage { get; }
public float RequiredSlipSpeed { get; }
public float LaunchForwardsMultiplier { get; }
public bool Slippery { get; }
public string SlipSound { get; }
public readonly EntityUid[] Slipped;
public SlipperyComponentState(float paralyzeTime, float intersectPercentage, float requiredSlipSpeed, float launchForwardsMultiplier, bool slippery, string slipSound, EntityUid[] slipped)
public SlipperyComponentState(float paralyzeTime, float launchForwardsMultiplier, string slipSound)
{
ParalyzeTime = paralyzeTime;
IntersectPercentage = intersectPercentage;
RequiredSlipSpeed = requiredSlipSpeed;
LaunchForwardsMultiplier = launchForwardsMultiplier;
Slippery = slippery;
SlipSound = slipSound;
Slipped = slipped;
}
}
}