Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,378 @@
#nullable enable
using System;
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Movement.Components;
using Content.Shared.NetIDs;
using Content.Shared.Physics.Pull;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics.Joints;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
namespace Content.Shared.Pulling.Components
{
public abstract class SharedPullableComponent : Component, IRelayMoveInput
{
public override string Name => "Pullable";
public override uint? NetID => ContentNetIDs.PULLABLE;
[ComponentDependency] private readonly PhysicsComponent? _physics = default!;
/// <summary>
/// Only set in Puller->set! Only set in unison with _pullerPhysics!
/// </summary>
private IEntity? _puller;
public IPhysBody? PullerPhysics { get; private set; }
private DistanceJoint? _pullJoint;
public float? MaxDistance => _pullJoint?.MaxLength;
private MapCoordinates? _movingTo;
/// <summary>
/// The current entity pulling this component.
/// Setting this performs the entire setup process for pulling.
/// </summary>
public virtual IEntity? Puller
{
get => _puller;
set
{
if (_puller == value)
{
return;
}
// New value. Abandon being pulled by any existing object.
if (_puller != null)
{
var oldPuller = _puller;
var oldPullerPhysics = PullerPhysics;
_puller = null;
Dirty();
PullerPhysics = null;
if (_physics != null && oldPullerPhysics != null)
{
var message = new PullStoppedMessage(oldPullerPhysics, _physics);
oldPuller.SendMessage(null, message);
Owner.SendMessage(null, message);
oldPuller.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
_physics.WakeBody();
}
// else-branch warning is handled below
}
// Now that is settled, prepare to be pulled by a new object.
if (_physics == null)
{
Logger.WarningS("c.go.c.pulling", "Well now you've done it, haven't you? SharedPullableComponent on {0} didn't have an IPhysBody.", Owner);
return;
}
if (value == null)
{
MovingTo = null;
}
else
{
// Pulling a new object : Perform sanity checks.
if (!_canStartPull(value))
{
return;
}
if (!value.TryGetComponent<PhysicsComponent>(out var pullerPhysics))
{
return;
}
if (!value.TryGetComponent<SharedPullerComponent>(out var valuePuller))
{
return;
}
// Ensure that the puller is not currently pulling anything.
// If this isn't done, then it happens too late, and the start/stop messages go out of order,
// and next thing you know it thinks it's not pulling anything even though it is!
var oldPulling = valuePuller.Pulling;
if (oldPulling != null)
{
if (oldPulling.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.TryStopPull();
}
else
{
Logger.WarningS("c.go.c.pulling", "Well now you've done it, haven't you? Someone transferred pulling to this component (on {0}) while presently pulling something that has no Pullable component (on {1})!", Owner, oldPulling);
return;
}
}
// Continue with pulling process.
var pullAttempt = new PullAttemptMessage(pullerPhysics, _physics);
value.SendMessage(null, pullAttempt);
if (pullAttempt.Cancelled)
{
return;
}
Owner.SendMessage(null, pullAttempt);
if (pullAttempt.Cancelled)
{
return;
}
// Pull start confirm
_puller = value;
Dirty();
PullerPhysics = pullerPhysics;
var message = new PullStartedMessage(PullerPhysics, _physics);
_puller.SendMessage(null, message);
Owner.SendMessage(null, message);
_puller.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
var union = PullerPhysics.GetWorldAABB().Union(_physics.GetWorldAABB());
var length = Math.Max(union.Size.X, union.Size.Y) * 0.75f;
_physics.WakeBody();
_pullJoint = pullerPhysics.CreateDistanceJoint(_physics, $"pull-joint-{_physics.Owner.Uid}");
// _physics.BodyType = BodyType.Kinematic; // TODO: Need to consider their original bodytype
_pullJoint.CollideConnected = false;
_pullJoint.Length = length * 0.75f;
_pullJoint.MaxLength = length;
}
// Code here will not run if pulling a new object was attempted and failed because of the returns from the refactor.
}
}
public bool BeingPulled => Puller != null;
public MapCoordinates? MovingTo
{
get => _movingTo;
set
{
if (_movingTo == value)
{
return;
}
_movingTo = value;
if (value == null)
{
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PullableStopMovingMessage());
}
else
{
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PullableMoveMessage());
}
}
}
/// <summary>
/// Sanity-check pull. This is called from Puller setter, so it will never deny a pull that's valid by setting Puller.
/// It might allow an impossible pull (i.e: puller has no PhysicsComponent somehow).
/// Ultimately this is only used separately to stop TryStartPull from cancelling a pull for no reason.
/// </summary>
private bool _canStartPull(IEntity puller)
{
if (!puller.HasComponent<SharedPullerComponent>())
{
return false;
}
if (_physics == null)
{
return false;
}
if (_physics.BodyType == BodyType.Static)
{
return false;
}
if (puller == Owner)
{
return false;
}
if (!puller.IsInSameOrNoContainer(Owner))
{
return false;
}
return true;
}
public bool TryStartPull(IEntity puller)
{
if (!_canStartPull(puller))
{
return false;
}
TryStopPull();
Puller = puller;
if (Puller != puller)
{
return false;
}
return true;
}
public bool TryStopPull()
{
if (!BeingPulled)
{
return false;
}
if (_physics != null && _pullJoint != null)
{
_physics.RemoveJoint(_pullJoint);
}
_pullJoint = null;
Puller = null;
return true;
}
public bool TogglePull(IEntity puller)
{
if (BeingPulled)
{
if (Puller == puller)
{
return TryStopPull();
}
else
{
TryStopPull();
return TryStartPull(puller);
}
}
return TryStartPull(puller);
}
public bool TryMoveTo(MapCoordinates to)
{
if (Puller == null)
{
return false;
}
if (_physics == null)
{
return false;
}
MovingTo = to;
return true;
}
public override ComponentState GetComponentState(ICommonSession player)
{
return new PullableComponentState(Puller?.Uid);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not PullableComponentState state)
{
return;
}
if (state.Puller == null)
{
Puller = null;
return;
}
if (!Owner.EntityManager.TryGetEntity(state.Puller.Value, out var entity))
{
Logger.Error($"Invalid entity {state.Puller.Value} for pulling");
return;
}
Puller = entity;
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (message is not PullMessage pullMessage ||
pullMessage.Pulled.Owner != Owner)
{
return;
}
var pulledStatus = Owner.GetComponentOrNull<SharedAlertsComponent>();
switch (message)
{
case PullStartedMessage:
pulledStatus?.ShowAlert(AlertType.Pulled);
break;
case PullStoppedMessage:
pulledStatus?.ClearAlert(AlertType.Pulled);
break;
}
}
public override void OnRemove()
{
TryStopPull();
MovingTo = null;
base.OnRemove();
}
// TODO: Need a component bus relay so all entities can use this and not just players
void IRelayMoveInput.MoveInputPressed(ICommonSession session)
{
var entity = session.AttachedEntity;
if (entity == null || !ActionBlockerSystem.CanMove(entity)) return;
TryStopPull();
}
}
[Serializable, NetSerializable]
public class PullableComponentState : ComponentState
{
public readonly EntityUid? Puller;
public PullableComponentState(EntityUid? puller) : base(ContentNetIDs.PULLABLE)
{
Puller = puller;
}
}
}

View File

@@ -0,0 +1,76 @@
#nullable enable
using Content.Shared.Alert;
using Content.Shared.Movement.Components;
using Content.Shared.Physics.Pull;
using Robust.Shared.GameObjects;
using Component = Robust.Shared.GameObjects.Component;
namespace Content.Shared.Pulling.Components
{
[RegisterComponent]
public class SharedPullerComponent : Component, IMoveSpeedModifier
{
public override string Name => "Puller";
private IEntity? _pulling;
public float WalkSpeedModifier => Pulling == null ? 1.0f : 0.75f;
public float SprintSpeedModifier => Pulling == null ? 1.0f : 0.75f;
public IEntity? Pulling
{
get => _pulling;
private set
{
if (_pulling == value)
{
return;
}
_pulling = value;
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? speed))
{
speed.RefreshMovementSpeedModifiers();
}
}
}
public override void OnRemove()
{
if (Pulling != null &&
Pulling.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.TryStopPull();
}
base.OnRemove();
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (message is not PullMessage pullMessage ||
pullMessage.Puller.Owner != Owner)
{
return;
}
SharedAlertsComponent? ownerStatus = Owner.GetComponentOrNull<SharedAlertsComponent>();
switch (message)
{
case PullStartedMessage msg:
Pulling = msg.Pulled.Owner;
ownerStatus?.ShowAlert(AlertType.Pulling);
break;
case PullStoppedMessage _:
Pulling = null;
ownerStatus?.ClearAlert(AlertType.Pulling);
break;
}
}
}
}

View File

@@ -0,0 +1,8 @@
using Robust.Shared.GameObjects;
namespace Content.Shared.Pulling
{
public class PullableMoveMessage : EntityEventArgs
{
}
}

View File

@@ -0,0 +1,8 @@
using Robust.Shared.GameObjects;
namespace Content.Shared.Pulling
{
public class PullableStopMovingMessage : EntityEventArgs
{
}
}

View File

@@ -0,0 +1,241 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.GameTicking;
using Content.Shared.Input;
using Content.Shared.Physics.Pull;
using Content.Shared.Pulling.Components;
using Content.Shared.Rotatable;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Players;
namespace Content.Shared.Pulling
{
[UsedImplicitly]
public abstract class SharedPullingSystem : EntitySystem, IResettingEntitySystem
{
/// <summary>
/// A mapping of pullers to the entity that they are pulling.
/// </summary>
private readonly Dictionary<IEntity, IEntity> _pullers =
new();
private readonly HashSet<SharedPullableComponent> _moving = new();
private readonly HashSet<SharedPullableComponent> _stoppedMoving = new();
/// <summary>
/// If distance between puller and pulled entity lower that this threshold,
/// pulled entity will not change its rotation.
/// Helps with small distance jittering
/// </summary>
private const float ThresholdRotDistance = 1;
/// <summary>
/// If difference between puller and pulled angle lower that this threshold,
/// pulled entity will not change its rotation.
/// Helps with diagonal movement jittering
/// </summary>
private const float ThresholdRotAngle = 30;
public IReadOnlySet<SharedPullableComponent> Moving => _moving;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PullStartedMessage>(OnPullStarted);
SubscribeLocalEvent<PullStoppedMessage>(OnPullStopped);
SubscribeLocalEvent<MoveEvent>(PullerMoved);
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleContainerInsert);
CommandBinds.Builder
.Bind(ContentKeyFunctions.MovePulledObject, new PointerInputCmdHandler(HandleMovePulledObject))
.Bind(ContentKeyFunctions.ReleasePulledObject, InputCmdHandler.FromDelegate(HandleReleasePulledObject))
.Register<SharedPullingSystem>();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
_moving.ExceptWith(_stoppedMoving);
_stoppedMoving.Clear();
}
public void Reset()
{
_pullers.Clear();
_moving.Clear();
_stoppedMoving.Clear();
}
private void OnPullStarted(PullStartedMessage message)
{
if (_pullers.TryGetValue(message.Puller.Owner, out var pulled) &&
pulled.TryGetComponent(out SharedPullableComponent? pulledComponent))
{
pulledComponent.TryStopPull();
}
SetPuller(message.Puller.Owner, message.Pulled.Owner);
}
private void OnPullStopped(PullStoppedMessage message)
{
RemovePuller(message.Puller.Owner);
}
protected void OnPullableMove(EntityUid uid, SharedPullableComponent component, PullableMoveMessage args)
{
_moving.Add(component);
}
protected void OnPullableStopMove(EntityUid uid, SharedPullableComponent component, PullableStopMovingMessage args)
{
_stoppedMoving.Add(component);
}
private void PullerMoved(MoveEvent ev)
{
var puller = ev.Sender;
if (!TryGetPulled(ev.Sender, out var pulled))
{
return;
}
if (!pulled.TryGetComponent(out IPhysBody? physics))
{
return;
}
UpdatePulledRotation(puller, pulled);
physics.WakeBody();
if (pulled.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.MovingTo = null;
}
}
// TODO: When Joint networking is less shitcodey fix this to use a dedicated joints message.
private void HandleContainerInsert(EntInsertedIntoContainerMessage message)
{
if (message.Entity.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.TryStopPull();
}
if (message.Entity.TryGetComponent(out SharedPullerComponent? puller))
{
if (puller.Pulling == null) return;
if (!puller.Pulling.TryGetComponent(out SharedPullableComponent? pulling))
{
return;
}
pulling.TryStopPull();
}
}
private bool HandleMovePulledObject(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
var player = session?.AttachedEntity;
if (player == null)
{
return false;
}
if (!TryGetPulled(player, out var pulled))
{
return false;
}
if (!pulled.TryGetComponent(out SharedPullableComponent? pullable))
{
return false;
}
pullable.TryMoveTo(coords.ToMap(EntityManager));
return false;
}
private void HandleReleasePulledObject(ICommonSession? session)
{
var player = session?.AttachedEntity;
if (player == null)
{
return;
}
if (!TryGetPulled(player, out var pulled))
{
return;
}
if (!pulled.TryGetComponent(out SharedPullableComponent? pullable))
{
return;
}
pullable.TryStopPull();
}
private void SetPuller(IEntity puller, IEntity pulled)
{
_pullers[puller] = pulled;
}
private bool RemovePuller(IEntity puller)
{
return _pullers.Remove(puller);
}
public IEntity? GetPulled(IEntity by)
{
return _pullers.GetValueOrDefault(by);
}
public bool TryGetPulled(IEntity by, [NotNullWhen(true)] out IEntity? pulled)
{
return (pulled = GetPulled(by)) != null;
}
public bool IsPulling(IEntity puller)
{
return _pullers.ContainsKey(puller);
}
private void UpdatePulledRotation(IEntity puller, IEntity pulled)
{
// TODO: update once ComponentReference works with directed event bus.
if (!pulled.TryGetComponent(out SharedRotatableComponent? rotatable))
return;
if (!rotatable.RotateWhilePulling)
return;
var dir = puller.Transform.WorldPosition - pulled.Transform.WorldPosition;
if (dir.LengthSquared > ThresholdRotDistance * ThresholdRotDistance)
{
var oldAngle = pulled.Transform.WorldRotation;
var newAngle = Angle.FromWorldVec(dir);
var diff = newAngle - oldAngle;
if (Math.Abs(diff.Degrees) > ThresholdRotAngle)
pulled.Transform.WorldRotation = newAngle;
}
}
}
}