NPC refactor (#10122)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -2,7 +2,6 @@ using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Buckle.Components;
|
||||
using Content.Shared.Rotatable;
|
||||
using JetBrains.Annotations;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.MobState.EntitySystems;
|
||||
|
||||
namespace Content.Shared.Interaction
|
||||
@@ -18,43 +17,97 @@ namespace Content.Shared.Interaction
|
||||
{
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
[Dependency] private readonly SharedMobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public bool TryFaceCoordinates(EntityUid user, Vector2 coordinates)
|
||||
/// <summary>
|
||||
/// Tries to rotate the entity towards the target rotation. Returns false if it needs to keep rotating.
|
||||
/// </summary>
|
||||
public bool TryRotateTo(EntityUid uid,
|
||||
Angle goalRotation,
|
||||
float frameTime,
|
||||
Angle tolerance,
|
||||
double rotationSpeed = float.MaxValue,
|
||||
TransformComponent? xform = null)
|
||||
{
|
||||
var diff = coordinates - EntityManager.GetComponent<TransformComponent>(user).MapPosition.Position;
|
||||
if (!Resolve(uid, ref xform))
|
||||
return true;
|
||||
|
||||
// If we have a max rotation speed then do that.
|
||||
// We'll rotate even if we can't shoot, looks better.
|
||||
if (rotationSpeed < float.MaxValue)
|
||||
{
|
||||
var worldRot = _transform.GetWorldRotation(xform);
|
||||
|
||||
var rotationDiff = Angle.ShortestDistance(worldRot, goalRotation).Theta;
|
||||
var maxRotate = rotationSpeed * frameTime;
|
||||
|
||||
if (Math.Abs(rotationDiff) > maxRotate)
|
||||
{
|
||||
var goalTheta = worldRot + Math.Sign(rotationDiff) * maxRotate;
|
||||
_transform.SetWorldRotation(xform, goalTheta);
|
||||
rotationDiff = (goalRotation - goalTheta);
|
||||
|
||||
if (Math.Abs(rotationDiff) > tolerance)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
_transform.SetWorldRotation(xform, goalRotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
_transform.SetWorldRotation(xform, goalRotation);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryFaceCoordinates(EntityUid user, Vector2 coordinates, TransformComponent? xform = null)
|
||||
{
|
||||
if (!Resolve(user, ref xform))
|
||||
return false;
|
||||
|
||||
var diff = coordinates - xform.MapPosition.Position;
|
||||
if (diff.LengthSquared <= 0.01f)
|
||||
return true;
|
||||
|
||||
var diffAngle = Angle.FromWorldVec(diff);
|
||||
return TryFaceAngle(user, diffAngle);
|
||||
}
|
||||
|
||||
public bool TryFaceAngle(EntityUid user, Angle diffAngle)
|
||||
public bool TryFaceAngle(EntityUid user, Angle diffAngle, TransformComponent? xform = null)
|
||||
{
|
||||
// TODO: MobState should be handling CanChangeDirection's event
|
||||
if (_actionBlockerSystem.CanChangeDirection(user) && !_mobState.IsIncapacitated(user))
|
||||
{
|
||||
Transform(user).WorldRotation = diffAngle;
|
||||
if (!Resolve(user, ref xform))
|
||||
return false;
|
||||
|
||||
xform.WorldRotation = diffAngle;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
if (EntityManager.TryGetComponent(user, out SharedBuckleComponent? buckle) && buckle.Buckled)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(user, out SharedBuckleComponent? buckle) && buckle.Buckled)
|
||||
var suid = buckle.LastEntityBuckledTo;
|
||||
if (suid != null)
|
||||
{
|
||||
var suid = buckle.LastEntityBuckledTo;
|
||||
if (suid != null)
|
||||
// We're buckled to another object. Is that object rotatable?
|
||||
if (TryComp<RotatableComponent>(suid.Value!, out var rotatable) && rotatable.RotateWhileAnchored)
|
||||
{
|
||||
// We're buckled to another object. Is that object rotatable?
|
||||
if (EntityManager.TryGetComponent<RotatableComponent>(suid.Value, out var rotatable) && rotatable.RotateWhileAnchored)
|
||||
{
|
||||
// Note the assumption that even if unanchored, user can only do spinnychair with an "independent wheel".
|
||||
// (Since the user being buckled to it holds it down with their weight.)
|
||||
// This is logically equivalent to RotateWhileAnchored.
|
||||
// Barstools and office chairs have independent wheels, while regular chairs don't.
|
||||
Transform(rotatable.Owner).WorldRotation = diffAngle;
|
||||
return true;
|
||||
}
|
||||
// Note the assumption that even if unanchored, user can only do spinnychair with an "independent wheel".
|
||||
// (Since the user being buckled to it holds it down with their weight.)
|
||||
// This is logically equivalent to RotateWhileAnchored.
|
||||
// Barstools and office chairs have independent wheels, while regular chairs don't.
|
||||
Transform(rotatable.Owner).WorldRotation = diffAngle;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user