Files
RobustToolbox/Robust.Client/Physics/JointSystem.cs
T
metalgearslothandGitHub 5a343477a9 Joints refactor (#1954)
* feex

* Cleanup

* Remove unneeded matrix

* Fixes

* Fix preprocessor

* localanchor cleanup

* Break this build so I remember to fix it later.

* Fixes

* Woops

* Fixes

* Fixup merge

* Stuff

* Update debug drawing

* Also dis thanks fork

* fren

* More colors

* Default drawing

* Fixes

* reviews
2021-10-04 15:34:15 +11:00

69 lines
2.1 KiB
C#

using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics.Joints;
namespace Robust.Client.Physics
{
public sealed class JointSystem : SharedJointSystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<JointComponent, ComponentHandleState>(HandleComponentState);
}
private void HandleComponentState(EntityUid uid, JointComponent component, ref ComponentHandleState args)
{
if (args.Current is not JointComponent.JointComponentState jointState) return;
var changed = new List<string>();
foreach (var (existing, _) in component.Joints)
{
if (!jointState.Joints.ContainsKey(existing))
{
changed.Add(existing);
}
}
foreach (var removed in changed)
{
RemoveJoint(component.Joints[removed]);
}
foreach (var (id, state) in jointState.Joints)
{
Joint joint;
if (!component.Joints.ContainsKey(id))
{
// Add new joint (if possible).
// Need to wait for BOTH joint components to come in first before we can add it. Yay dependencies!
if (!EntityManager.HasComponent<JointComponent>(state.UidA) ||
!EntityManager.HasComponent<JointComponent>(state.UidB)) continue;
joint = state.GetJoint();
AddJoint(joint);
continue;
}
joint = state.GetJoint();
var existing = component.Joints[id];
if (!existing.Equals(joint))
{
existing.ApplyState(state);
component.Dirty();
}
}
if (changed.Count > 0)
{
component.Dirty();
}
}
}
}