Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Aug 21, 2024
1 parent afa709b commit 984a93c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public sealed partial class LeashComponent : Component
public List<LeashData> Leashed = new();

[DataDefinition, Serializable, NetSerializable]
public partial class LeashData
public sealed partial class LeashData
{
[DataField]
public string JointId = string.Empty;
Expand Down
47 changes: 31 additions & 16 deletions Content.Shared/Floofstation/Leash/LeashSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics.Joints;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using Robust.Shared.Timing;
Expand Down Expand Up @@ -60,9 +62,9 @@ public override void Shutdown()

public override void Update(float frameTime)
{
var leashQuery = EntityQueryEnumerator<LeashComponent>();
var leashQuery = EntityQueryEnumerator<LeashComponent, PhysicsComponent>();

while (leashQuery.MoveNext(out var leashEnt, out var leash))
while (leashQuery.MoveNext(out var leashEnt, out var leash, out var physics))
{
var sourceXForm = Transform(leashEnt);

Expand Down Expand Up @@ -133,10 +135,11 @@ private void OnJointRemoved(Entity<LeashComponent> ent, ref JointRemovedEvent ar
{
var id = args.Joint.ID;
if (!ent.Comp.Leashed.TryFirstOrDefault(it => it.JointId == id, out var data)
|| !TryComp<LeashedComponent>(GetEntity(data.Pulled), out var leashed))
|| !TryGetEntity(data.Pulled, out var leashedEnt)
|| !TryComp<LeashedComponent>(leashedEnt, out var leashed))
return;

RemoveLeash((leashed.Owner, leashed), ent!, false);
RemoveLeash((leashedEnt.Value, leashed), ent!, false);
}

private void OnGetEquipmentVerbs(Entity<LeashAnchorComponent> ent, ref GetVerbsEvent<EquipmentVerb> args)
Expand Down Expand Up @@ -197,15 +200,15 @@ private void OnAttachDoAfter(Entity<LeashAnchorComponent> ent, ref LeashAttachDo
|| !CanLeash(ent, (args.Used.Value, leash)))
return;

DoLeash(ent, (args.Used.Value, leash), args.Target!.Value);
DoLeash(ent, (args.Used.Value, leash), EntityUid.Invalid);
}

private void OnDetachDoAfter(Entity<LeashedComponent> ent, ref LeashDetachDoAfterEvent args)
{
if (args.Cancelled || args.Handled || ent.Comp.Puller is not { } leash)
return;

RemoveLeash(ent!, leash, true);
RemoveLeash(ent!, leash);
}

private bool OnRequestPullLeash(ICommonSession? session, EntityCoordinates targetCoords, EntityUid uid)
Expand Down Expand Up @@ -267,6 +270,19 @@ private bool TryGetLeashTarget(Entity<LeashAnchorComponent?> ent, out EntityUid
return true;
}

private DistanceJoint CreateLeashJoint(string jointId, Entity<LeashComponent> leash, EntityUid leashTarget)
{
var joint = _joints.CreateDistanceJoint(leash, leashTarget, id: jointId);
joint.CollideConnected = false;
joint.Length = leash.Comp.Length;
joint.MinLength = 0f;
joint.MaxLength = leash.Comp.Length;
joint.Stiffness = 1f;
joint.CollideConnected = true; // This is just for performance reasons and doesn't actually make mobs collide.

return joint;
}

#endregion

#region public api
Expand All @@ -281,7 +297,7 @@ public bool CanLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent>
&& !_xform.IsParentOf(Transform(leashTarget), leash); // google recursion - this makes the game explode for some reason
}

public bool TryLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent> leash, EntityUid user)
public bool TryLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent> leash, EntityUid user, bool popup = true)
{
if (!CanLeash(anchor, leash) || !TryGetLeashTarget(anchor!, out var leashTarget))
return false;
Expand All @@ -304,7 +320,7 @@ public bool TryLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent>
};

var result = _doAfters.TryStartDoAfter(doAfter);
if (result && _net.IsServer)
if (result && _net.IsServer && popup)
{
(string, object)[] locArgs = [("user", user), ("target", leashTarget), ("anchor", anchor.Owner), ("selfAnchor", anchor.Owner == leashTarget)];

Expand Down Expand Up @@ -345,6 +361,12 @@ public bool TryUnleash(Entity<LeashedComponent?> leashed, Entity<LeashComponent?
return result;
}

/// <summary>
/// Immediately creates the leash joint between the specified entities and sets up respective components.
/// </summary>
/// <param name="anchor">The anchor entity, usually either target's clothing or the target itself.</param>
/// <param name="leash">The leash entity.</param>
/// <param name="leashTarget">The entity to which the leash is actually connected. Can be EntityUid.Invalid, then it will be deduced.</param>
public void DoLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent> leash, EntityUid leashTarget)
{
if (_net.IsClient || leashTarget is { Valid: false } && !TryGetLeashTarget(anchor!, out leashTarget))
Expand All @@ -357,14 +379,7 @@ public void DoLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent>
leashedComp.Anchor = anchor;

// I'd like to use a chain joint or smth, but it's too hard and oftentimes buggy - lamia is a good bad example of that.
var joint = _joints.CreateDistanceJoint(leash, leashTarget, id: leashedComp.JointId);
joint.CollideConnected = false;
joint.Length = leash.Comp.Length;
joint.MinLength = 0f;
joint.MaxLength = leash.Comp.Length;
joint.Stiffness = 0f;
joint.Damping = 0f;

var joint = CreateLeashJoint(leashedComp.JointId, leash, leashTarget);
var data = new LeashComponent.LeashData(leashedComp.JointId, netLeashTarget)
{
NextDamage = _timing.CurTime + leash.Comp.DamageInterval
Expand Down

0 comments on commit 984a93c

Please sign in to comment.