Files
tbd-station-14/Content.Shared/Physics/Pull/PullController.cs
DrSmugleaf 48b61f6bcc Replace every usage of GridCoordinates with EntityCoordinates (#2021)
* Update RobustToolbox

* Transition direct type usages

* More updates

* Fix invalid use of to map

* Update RobustToolbox

* Fix dropping items

* Rename name usages of "GridCoordinates" to "EntityCoordinates"

* Revert "Update RobustToolbox"

This reverts commit 9f334a17c5908ded0043a63158bb671e4aa3f346.

* Revert "Update RobustToolbox"

This reverts commit 3a9c8cfa3606fa501aa84407796d2ad920853a09.

# Conflicts:
#	RobustToolbox

* Fix cursed IMapGrid method usage.

* GridTileLookupTest now uses EntityCoordinates

Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es>
2020-09-06 16:11:53 +02:00

163 lines
4.3 KiB
C#

#nullable enable
using System;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Utility;
namespace Content.Shared.Physics.Pull
{
public class PullController : VirtualController
{
private const float DistBeforePull = 1.0f;
private const float DistBeforeStopPull = SharedInteractionSystem.InteractionRange;
private ICollidableComponent? _puller;
public bool GettingPulled => _puller != null;
private EntityCoordinates? _movingTo;
public ICollidableComponent? Puller => _puller;
public void StartPull(ICollidableComponent puller)
{
DebugTools.AssertNotNull(puller);
if (_puller == puller)
{
return;
}
_puller = puller;
if (ControlledComponent == null)
{
return;
}
ControlledComponent.WakeBody();
var message = new PullStartedMessage(this, _puller, ControlledComponent);
_puller.Owner.SendMessage(null, message);
ControlledComponent.Owner.SendMessage(null, message);
}
public void StopPull()
{
var oldPuller = _puller;
if (oldPuller == null)
{
return;
}
_puller = null;
if (ControlledComponent == null)
{
return;
}
ControlledComponent.WakeBody();
var message = new PullStoppedMessage(this, oldPuller, ControlledComponent);
oldPuller.Owner.SendMessage(null, message);
ControlledComponent.Owner.SendMessage(null, message);
ControlledComponent.TryRemoveController<PullController>();
}
public void TryMoveTo(EntityCoordinates from, EntityCoordinates to)
{
if (_puller == null || ControlledComponent == null)
{
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!from.InRange(entityManager, to, SharedInteractionSystem.InteractionRange))
{
return;
}
ControlledComponent.WakeBody();
var dist = _puller.Owner.Transform.Coordinates.Position - to.Position;
if (Math.Sqrt(dist.LengthSquared) > DistBeforeStopPull ||
Math.Sqrt(dist.LengthSquared) < 0.25f)
{
return;
}
_movingTo = to;
}
public override void UpdateBeforeProcessing()
{
if (_puller == null || ControlledComponent == null)
{
return;
}
if (!_puller.Owner.IsInSameOrNoContainer(ControlledComponent.Owner))
{
StopPull();
return;
}
// Are we outside of pulling range?
var dist = _puller.Owner.Transform.WorldPosition - ControlledComponent.Owner.Transform.WorldPosition;
if (dist.Length > DistBeforeStopPull)
{
StopPull();
}
else if (_movingTo.HasValue)
{
var diff = _movingTo.Value.Position - ControlledComponent.Owner.Transform.Coordinates.Position;
LinearVelocity = diff.Normalized * 5;
}
else if (dist.Length > DistBeforePull)
{
LinearVelocity = dist.Normalized * _puller.LinearVelocity.Length * 1.1f;
}
else
{
LinearVelocity = Vector2.Zero;
}
}
public override void UpdateAfterProcessing()
{
base.UpdateAfterProcessing();
if (ControlledComponent == null)
{
_movingTo = null;
return;
}
if (_movingTo == null)
{
return;
}
if (ControlledComponent.Owner.Transform.Coordinates.Position.EqualsApprox(_movingTo.Value.Position, 0.01))
{
_movingTo = null;
}
}
}
}