Moves buckling and vehicles to shared, some cleanup (#15923)

This commit is contained in:
AJCM-git
2023-05-01 03:04:23 -04:00
committed by GitHub
parent 2343dd1d3c
commit 878c7c0b85
33 changed files with 1451 additions and 1405 deletions

View File

@@ -1,86 +1,82 @@
using Content.Client.Rotation;
using Content.Shared.ActionBlocker;
using Content.Shared.Buckle;
using Content.Shared.Buckle.Components;
using Content.Shared.Vehicle.Components;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
namespace Content.Client.Buckle
namespace Content.Client.Buckle;
internal sealed class BuckleSystem : SharedBuckleSystem
{
internal sealed class BuckleSystem : SharedBuckleSystem
[Dependency] private readonly RotationVisualizerSystem _rotationVisualizerSystem = default!;
public override void Initialize()
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly RotationVisualizerSystem _rotationVisualizerSystem = default!;
base.Initialize();
public override void Initialize()
SubscribeLocalEvent<BuckleComponent, ComponentHandleState>(OnBuckleHandleState);
SubscribeLocalEvent<BuckleComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private void OnBuckleHandleState(EntityUid uid, BuckleComponent component, ref ComponentHandleState args)
{
if (args.Current is not BuckleComponentState state)
return;
component.Buckled = state.Buckled;
component.BuckledTo = state.BuckledTo;
component.LastEntityBuckledTo = state.LastEntityBuckledTo;
component.DontCollide = state.DontCollide;
ActionBlockerSystem.UpdateCanMove(uid);
if (!TryComp<SpriteComponent>(uid, out var ownerSprite))
return;
if (HasComp<VehicleComponent>(component.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 (component is { Buckled: true, LastEntityBuckledTo: { } } &&
Transform(component.LastEntityBuckledTo.Value).LocalRotation.GetCardinalDir() == Direction.North &&
TryComp<SpriteComponent>(component.LastEntityBuckledTo, out var buckledSprite))
{
base.Initialize();
SubscribeLocalEvent<BuckleComponent, ComponentHandleState>(OnBuckleHandleState);
SubscribeLocalEvent<BuckleComponent, AppearanceChangeEvent>(OnAppearanceChange);
component.OriginalDrawDepth ??= ownerSprite.DrawDepth;
ownerSprite.DrawDepth = buckledSprite.DrawDepth - 1;
return;
}
private void OnBuckleHandleState(EntityUid uid, BuckleComponent buckle, ref ComponentHandleState args)
// If here, we're not turning north and should restore the saved draw depth.
if (component.OriginalDrawDepth.HasValue)
{
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;
}
}
private void OnAppearanceChange(EntityUid uid, BuckleComponent component, ref AppearanceChangeEvent args)
{
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) ||
!buckled ||
args.Sprite == null)
{
_rotationVisualizerSystem.SetHorizontalAngle(uid, RotationVisualsComponent.DefaultRotation, rotVisuals);
return;
}
// Animate strapping yourself to something at a given angle
_rotationVisualizerSystem.SetHorizontalAngle(uid, Angle.FromDegrees(angle), rotVisuals);
// TODO: Dump this when buckle is better
_rotationVisualizerSystem.AnimateSpriteRotation(uid, args.Sprite, rotVisuals.HorizontalRotation, 0.125f);
ownerSprite.DrawDepth = component.OriginalDrawDepth.Value;
component.OriginalDrawDepth = null;
}
}
private void OnAppearanceChange(EntityUid uid, BuckleComponent component, ref AppearanceChangeEvent args)
{
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) ||
!buckled ||
args.Sprite == null)
{
_rotationVisualizerSystem.SetHorizontalAngle(uid, RotationVisualsComponent.DefaultRotation, rotVisuals);
return;
}
// Animate strapping yourself to something at a given angle
_rotationVisualizerSystem.SetHorizontalAngle(uid, Angle.FromDegrees(angle), rotVisuals);
// TODO: Dump this when buckle is better
_rotationVisualizerSystem.AnimateSpriteRotation(uid, args.Sprite, rotVisuals.HorizontalRotation, 0.125f);
}
}

View File

@@ -3,41 +3,68 @@ using Content.Shared.Vehicle.Components;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
namespace Content.Client.Vehicle
namespace Content.Client.Vehicle;
public sealed class VehicleSystem : SharedVehicleSystem
{
public sealed class VehicleSystem : SharedVehicleSystem
public override void Initialize()
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RiderComponent, ComponentStartup>(OnRiderStartup);
SubscribeLocalEvent<RiderComponent, ComponentShutdown>(OnRiderShutdown);
SubscribeLocalEvent<RiderComponent, ComponentHandleState>(OnRiderHandleState);
}
base.Initialize();
private void OnRiderStartup(EntityUid uid, RiderComponent component, ComponentStartup args)
{
// Center the player's eye on the vehicle
if (TryComp(uid, out EyeComponent? eyeComp))
eyeComp.Target ??= component.Vehicle;
}
SubscribeLocalEvent<RiderComponent, ComponentStartup>(OnRiderStartup);
SubscribeLocalEvent<RiderComponent, ComponentShutdown>(OnRiderShutdown);
SubscribeLocalEvent<RiderComponent, ComponentHandleState>(OnRiderHandleState);
SubscribeLocalEvent<VehicleComponent, AppearanceChangeEvent>(OnVehicleAppearanceChange);
}
private void OnRiderShutdown(EntityUid uid, RiderComponent component, ComponentShutdown args)
{
// reset the riders eye centering.
if (TryComp(uid, out EyeComponent? eyeComp) && eyeComp.Target == component.Vehicle)
eyeComp.Target = null;
}
private void OnRiderStartup(EntityUid uid, RiderComponent component, ComponentStartup args)
{
// Center the player's eye on the vehicle
if (TryComp(uid, out EyeComponent? eyeComp))
eyeComp.Target ??= component.Vehicle;
}
private void OnRiderHandleState(EntityUid uid, RiderComponent component, ref ComponentHandleState args)
{
if (args.Current is not RiderComponentState state)
return;
private void OnRiderShutdown(EntityUid uid, RiderComponent component, ComponentShutdown args)
{
// reset the riders eye centering.
if (TryComp(uid, out EyeComponent? eyeComp) && eyeComp.Target == component.Vehicle)
eyeComp.Target = null;
}
if (TryComp(uid, out EyeComponent? eyeComp) && eyeComp.Target == component.Vehicle)
eyeComp.Target = state.Entity;
private void OnRiderHandleState(EntityUid uid, RiderComponent component, ref ComponentHandleState args)
{
if (args.Current is not RiderComponentState state)
return;
component.Vehicle = state.Entity;
}
if (TryComp(uid, out EyeComponent? eyeComp) && eyeComp.Target == component.Vehicle)
eyeComp.Target = state.Entity;
component.Vehicle = state.Entity;
}
private void OnVehicleAppearanceChange(EntityUid uid, VehicleComponent component, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;
if (component.HideRider
&& Appearance.TryGetData<bool>(uid, VehicleVisuals.HideRider, out var hide, args.Component)
&& TryComp<SpriteComponent>(component.LastRider, out var riderSprite))
riderSprite.Visible = !hide;
// First check is for the sprite itself
if (Appearance.TryGetData<int>(uid, VehicleVisuals.DrawDepth, out var drawDepth, args.Component))
args.Sprite.DrawDepth = drawDepth;
// Set vehicle layer to animated or not (i.e. are the wheels turning or not)
if (component.AutoAnimate
&& Appearance.TryGetData<bool>(uid, VehicleVisuals.AutoAnimate, out var autoAnimate, args.Component))
args.Sprite.LayerSetAutoAnimated(VehicleVisualLayers.AutoAnimate, autoAnimate);
}
}
public enum VehicleVisualLayers : byte
{
/// Layer for the vehicle's wheels
AutoAnimate,
}

View File

@@ -1,10 +0,0 @@
namespace Content.Client.Vehicle;
/// <summary>
/// Controls visuals for vehicles
/// </summary>
[RegisterComponent]
public sealed class VehicleVisualsComponent : Component
{
public int DrawDepth = 0;
}

View File

@@ -1,35 +0,0 @@
using Robust.Client.GameObjects;
using Content.Shared.Vehicle;
namespace Content.Client.Vehicle
{
/// <summary>
/// Controls client-side visuals for
/// vehicles
/// </summary>
public sealed class VehicleVisualsSystem : VisualizerSystem<VehicleVisualsComponent>
{
protected override void OnAppearanceChange(EntityUid uid, VehicleVisualsComponent component, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;
// First check is for the sprite itself
if (AppearanceSystem.TryGetData<int>(uid, VehicleVisuals.DrawDepth, out var drawDepth, args.Component))
{
args.Sprite.DrawDepth = drawDepth;
}
// Set vehicle layer to animated or not (i.e. are the wheels turning or not)
if (AppearanceSystem.TryGetData<bool>(uid, VehicleVisuals.AutoAnimate, out var autoAnimate, args.Component))
{
args.Sprite.LayerSetAutoAnimated(VehicleVisualLayers.AutoAnimate, autoAnimate);
}
}
}
}
public enum VehicleVisualLayers : byte
{
/// Layer for the vehicle's wheels
AutoAnimate,
}