2020-10-16 20:35:09 +02:00
|
|
|
using System;
|
2021-10-04 16:10:54 +01:00
|
|
|
using Robust.Shared.Analyzers;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-07-12 01:32:10 -07:00
|
|
|
using Robust.Shared.GameStates;
|
2021-12-03 11:11:52 +01:00
|
|
|
using Robust.Shared.IoC;
|
2020-11-19 00:37:16 +11:00
|
|
|
using Robust.Shared.Log;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Map;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Physics.Dynamics.Joints;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Serialization;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Pulling.Components
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-10-04 16:10:54 +01:00
|
|
|
// Before you try to add another type than SharedPullingStateManagementSystem, consider the can of worms you may be opening!
|
2021-07-12 01:32:10 -07:00
|
|
|
[NetworkedComponent()]
|
2021-10-04 16:10:54 +01:00
|
|
|
[Friend(typeof(SharedPullingStateManagementSystem))]
|
2021-10-05 14:29:03 +11:00
|
|
|
[RegisterComponent]
|
|
|
|
|
public class SharedPullableComponent : Component
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-10-04 16:10:54 +01:00
|
|
|
public float? MaxDistance => PullJoint?.MaxLength;
|
2021-04-05 14:08:45 +02:00
|
|
|
|
2020-11-26 13:48:10 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The current entity pulling this component.
|
2021-10-05 22:51:34 +01:00
|
|
|
/// SharedPullingStateManagementSystem should be writing this. This means definitely not you.
|
2020-11-26 13:48:10 +00:00
|
|
|
/// </summary>
|
2021-12-05 21:02:04 +01:00
|
|
|
public EntityUid? Puller { get; set; }
|
2020-11-26 13:48:10 +00:00
|
|
|
/// <summary>
|
2021-10-04 16:10:54 +01:00
|
|
|
/// The pull joint.
|
|
|
|
|
/// SharedPullingStateManagementSystem should be writing this. This means probably not you.
|
2020-11-26 13:48:10 +00:00
|
|
|
/// </summary>
|
2021-10-04 16:10:54 +01:00
|
|
|
public DistanceJoint? PullJoint { get; set; }
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2022-02-03 18:40:22 +13:00
|
|
|
public bool BeingPulled => Puller != null;
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2021-11-07 02:16:49 +00:00
|
|
|
public EntityCoordinates? MovingTo { get; set; }
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2021-11-30 15:20:38 +01:00
|
|
|
public override ComponentState GetComponentState()
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-12-03 15:53:09 +01:00
|
|
|
return new PullableComponentState(Puller);
|
2020-10-16 20:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
|
|
|
|
{
|
|
|
|
|
base.HandleComponentState(curState, nextState);
|
|
|
|
|
|
2020-11-26 14:33:31 +01:00
|
|
|
if (curState is not PullableComponentState state)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (!state.Puller.HasValue)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-10-05 22:51:34 +01:00
|
|
|
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPullable(this);
|
2020-10-16 20:35:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (!state.Puller.Value.IsValid())
|
2020-11-19 00:37:16 +11:00
|
|
|
{
|
|
|
|
|
Logger.Error($"Invalid entity {state.Puller.Value} for pulling");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (Puller == state.Puller)
|
2021-10-05 22:51:34 +01:00
|
|
|
{
|
|
|
|
|
// don't disconnect and reconnect a puller for no reason
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<SharedPullerComponent?>(state.Puller.Value, out var comp))
|
2021-10-05 22:51:34 +01:00
|
|
|
{
|
|
|
|
|
Logger.Error($"Entity {state.Puller.Value} for pulling had no Puller component");
|
|
|
|
|
// ensure it disconnects from any different puller, still
|
|
|
|
|
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPullable(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceRelationship(comp, this);
|
2020-10-16 20:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 16:10:54 +01:00
|
|
|
protected override void OnRemove()
|
|
|
|
|
{
|
2022-02-03 18:40:22 +13:00
|
|
|
if (Puller != null)
|
2021-10-04 16:10:54 +01:00
|
|
|
{
|
|
|
|
|
// This is absolute paranoia but it's also absolutely necessary. Too many puller state bugs. - 20kdc
|
|
|
|
|
Logger.ErrorS("c.go.c.pulling", "PULLING STATE CORRUPTION IMMINENT IN PULLABLE {0} - OnRemove called when Puller is set!", Owner);
|
|
|
|
|
}
|
2020-10-16 20:35:09 +02:00
|
|
|
base.OnRemove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable, NetSerializable]
|
|
|
|
|
public class PullableComponentState : ComponentState
|
|
|
|
|
{
|
|
|
|
|
public readonly EntityUid? Puller;
|
|
|
|
|
|
2021-07-12 01:32:10 -07:00
|
|
|
public PullableComponentState(EntityUid? puller)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
Puller = puller;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-17 00:37:05 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raised when a request is made to stop pulling an entity.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class StopPullingEvent : CancellableEntityEventArgs
|
|
|
|
|
{
|
|
|
|
|
public EntityUid? User { get; }
|
|
|
|
|
|
|
|
|
|
public StopPullingEvent(EntityUid? uid = null)
|
|
|
|
|
{
|
|
|
|
|
User = uid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-16 20:35:09 +02:00
|
|
|
}
|