Files
OldThink/Content.Client/Buckle/BuckleSystem.cs

87 lines
3.8 KiB
C#
Raw Normal View History

2022-12-20 18:24:50 -04:00
using Content.Client.Rotation;
2022-11-14 20:30:30 +01:00
using Content.Shared.ActionBlocker;
2021-06-09 22:19:39 +02:00
using Content.Shared.Buckle;
using Content.Shared.Buckle.Components;
2022-11-14 20:30:30 +01:00
using Content.Shared.Vehicle.Components;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
2021-06-09 22:19:39 +02:00
namespace Content.Client.Buckle
{
internal sealed class BuckleSystem : SharedBuckleSystem
{
2022-11-14 20:30:30 +01:00
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly RotationVisualizerSystem _rotationVisualizerSystem = default!;
2022-11-14 20:30:30 +01:00
public override void Initialize()
{
base.Initialize();
2022-11-14 20:30:30 +01:00
SubscribeLocalEvent<BuckleComponent, ComponentHandleState>(OnBuckleHandleState);
2022-12-20 18:24:50 -04:00
SubscribeLocalEvent<BuckleComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
2021-06-09 22:19:39 +02:00
2022-11-14 20:30:30 +01:00
private void OnBuckleHandleState(EntityUid uid, BuckleComponent buckle, ref ComponentHandleState args)
{
if (args.Current is not BuckleComponentState state)
return;
buckle.Buckled = state.Buckled;
buckle.LastEntityBuckledTo = state.LastEntityBuckledTo;
buckle.DontCollide = state.DontCollide;
_actionBlocker.UpdateCanMove(uid);
if (!TryComp(uid, out SpriteComponent? ownerSprite))
return;
if (HasComp<VehicleComponent>(buckle.LastEntityBuckledTo))
return;
// Adjust draw depth when the chair faces north so that the seat back is drawn over the player.
// Reset the draw depth when rotated in any other direction.
// TODO when ECSing, make this a visualizer
// This code was written before rotatable viewports were introduced, so hard-coding Direction.North
// and comparing it against LocalRotation now breaks this in other rotations. This is a FIXME, but
// better to get it working for most people before we look at a more permanent solution.
if (buckle.Buckled &&
buckle.LastEntityBuckledTo != null &&
Transform(buckle.LastEntityBuckledTo.Value).LocalRotation.GetCardinalDir() == Direction.North &&
TryComp<SpriteComponent>(buckle.LastEntityBuckledTo, out var buckledSprite))
{
buckle.OriginalDrawDepth ??= ownerSprite.DrawDepth;
ownerSprite.DrawDepth = buckledSprite.DrawDepth - 1;
return;
}
// If here, we're not turning north and should restore the saved draw depth.
if (buckle.OriginalDrawDepth.HasValue)
{
ownerSprite.DrawDepth = buckle.OriginalDrawDepth.Value;
buckle.OriginalDrawDepth = null;
}
}
2022-12-20 18:24:50 -04:00
private void OnAppearanceChange(EntityUid uid, BuckleComponent component, ref AppearanceChangeEvent args)
{
2023-01-24 07:50:35 +11:00
if (!TryComp<RotationVisualsComponent>(uid, out var rotVisuals))
return;
if (!_appearanceSystem.TryGetData<int>(uid, StrapVisuals.RotationAngle, out var angle, args.Component) ||
!_appearanceSystem.TryGetData<bool>(uid, BuckleVisuals.Buckled, out var buckled, args.Component) ||
2022-12-20 18:24:50 -04:00
!buckled ||
args.Sprite == null)
{
2023-01-24 07:50:35 +11:00
_rotationVisualizerSystem.SetHorizontalAngle(uid, RotationVisualsComponent.DefaultRotation, rotVisuals);
2022-12-20 18:24:50 -04:00
return;
}
// Animate strapping yourself to something at a given angle
2023-01-24 07:50:35 +11:00
_rotationVisualizerSystem.SetHorizontalAngle(uid, Angle.FromDegrees(angle), rotVisuals);
// TODO: Dump this when buckle is better
_rotationVisualizerSystem.AnimateSpriteRotation(uid, args.Sprite, rotVisuals.HorizontalRotation, 0.125f);
2022-12-20 18:24:50 -04:00
}
2021-06-09 22:19:39 +02:00
}
}