2020-10-16 20:35:09 +02:00
|
|
|
using System;
|
|
|
|
|
using Content.Shared.Physics.Pull;
|
2021-10-04 16:10:54 +01:00
|
|
|
using Robust.Shared.Analyzers;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Containers;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2021-07-12 01:32:10 -07:00
|
|
|
using Robust.Shared.GameStates;
|
2020-11-19 00:37:16 +11:00
|
|
|
using Robust.Shared.Log;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Map;
|
2020-10-28 10:12:46 +01:00
|
|
|
using Robust.Shared.Physics;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Physics.Dynamics.Joints;
|
2021-04-05 14:08:45 +02:00
|
|
|
using Robust.Shared.Players;
|
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
|
|
|
{
|
|
|
|
|
public override string Name => "Pullable";
|
|
|
|
|
|
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-10-04 16:10:54 +01:00
|
|
|
public IEntity? 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
|
|
|
|
2021-10-04 16:10:54 +01: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
|
|
|
{
|
|
|
|
|
return new PullableComponentState(Puller?.Uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state.Puller == null)
|
|
|
|
|
{
|
2021-10-05 22:51:34 +01:00
|
|
|
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPullable(this);
|
2020-10-16 20:35:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 00:37:16 +11:00
|
|
|
if (!Owner.EntityManager.TryGetEntity(state.Puller.Value, out var entity))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error($"Invalid entity {state.Puller.Value} for pulling");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 22:51:34 +01:00
|
|
|
if (Puller == entity)
|
|
|
|
|
{
|
|
|
|
|
// don't disconnect and reconnect a puller for no reason
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!entity.TryGetComponent<SharedPullerComponent>(out var comp))
|
|
|
|
|
{
|
|
|
|
|
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 Shutdown()
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-10-04 16:10:54 +01:00
|
|
|
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPullable(this);
|
|
|
|
|
base.Shutdown();
|
|
|
|
|
}
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2021-10-04 16:10:54 +01:00
|
|
|
protected override void OnRemove()
|
|
|
|
|
{
|
|
|
|
|
if (Puller != null)
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
}
|