Files
wwdpublic/Content.Shared/Interaction/RotateToFaceSystem.cs
SimpleStation14 04c45bb9da Mirror: Prevent dead players from turning bar stools (#213)
## Mirror of PR #24308: [Prevent dead players from turning bar
stools](https://github.com/space-wizards/space-station-14/pull/24308)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `15a7520df17a6fa95cbd8ce8914edab5b0d7ed50`

PR opened by <img
src="https://avatars.githubusercontent.com/u/4543739?v=4" width="16"/><a
href="https://github.com/Nopey"> Nopey</a> at 2024-01-19 22:47:45 UTC

---

PR changed 3 files with 11 additions and 18 deletions.

The PR had the following labels:


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> Bugfix.
> 
> ## Why / Balance
> Previously, players could always turn a bar stool (or office chair,
etc..) they were buckled into; even while stone cold dead.
> 
> ## Technical details
> RotateToFaceSystem now only lets players rotate barstools (& similar)
when the actionBlockerSystem reports that they CanChangeDirection; this
also means players who are frozen by admins (see AdminFrozenSystem) can
no longer spin barstools.
> 
> SharedBuckleSystem no longer tries to prevent buckled players from
being able to change direction, instead relying on RotateToFaceSystem to
ensure that players on beds and such don't rotate.
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
> 
> - [X] I have added screenshots/videos to this PR showcasing its
changes ingame
> 
> Video showcasing prior broken behavior: 
> 
>
https://github.com/space-wizards/space-station-14/assets/4543739/74be203a-0961-4850-842a-768d927f6691
> 
> 
> 
> 
> ## Breaking changes
> <!--
> List any breaking changes, including namespace, public
class/method/field changes, prototype renames; and provide instructions
for fixing them. This will be pasted in #codebase-changes.
> -->
> 
> **Changelog**
> 🆑
> - fix: Dead players can no longer spin on a bar stool.
> <!--
> Make players aware of new features and changes that could affect how
they play the game by adding a Changelog entry. Please read the
Changelog guidelines located at:
https://docs.spacestation14.io/en/getting-started/pr-guideline#changelog
> -->
> 
> <!--
> Make sure to take this Changelog template out of the comment block in
order for it to show up.
> 🆑
> - add: Added fun!
> - remove: Removed fun!
> - tweak: Changed fun!
> - fix: Fixed fun!
> -->
> 


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-09 01:19:09 -04:00

115 lines
4.3 KiB
C#

using System.Numerics;
using Content.Shared.ActionBlocker;
using Content.Shared.Buckle.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Rotatable;
using JetBrains.Annotations;
namespace Content.Shared.Interaction
{
/// <summary>
/// Contains common code used to rotate a player to face a given target or direction.
/// This interaction in itself is useful for various roleplay purposes.
/// But it needs specialized code to handle chairs and such.
/// Doesn't really fit with SharedInteractionSystem so it's not there.
/// </summary>
[UsedImplicitly]
public sealed class RotateToFaceSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
/// <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)
{
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;
TryFaceAngle(uid, goalTheta, xform);
rotationDiff = (goalRotation - goalTheta);
if (Math.Abs(rotationDiff) > tolerance)
{
return false;
}
return true;
}
TryFaceAngle(uid, goalRotation, xform);
}
else
{
TryFaceAngle(uid, goalRotation, xform);
}
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, TransformComponent? xform = null)
{
if (!_actionBlockerSystem.CanChangeDirection(user))
return false;
if (EntityManager.TryGetComponent(user, out BuckleComponent? buckle) && buckle.Buckled)
{
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)
{
// 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.SetWorldRotation(Transform(suid.Value), diffAngle);
return true;
}
}
return false;
}
// user is not buckled in; apply to their transform
if (!Resolve(user, ref xform))
return false;
_transform.SetWorldRotation(xform, diffAngle);
return true;
}
}
}