ECS buckle (#12586)

This commit is contained in:
DrSmugleaf
2022-11-14 20:30:30 +01:00
committed by GitHub
parent 3b818e836b
commit d5ae5658a1
13 changed files with 752 additions and 776 deletions

View File

@@ -1,7 +1,4 @@
using Content.Shared.ActionBlocker;
using Content.Shared.Buckle.Components;
using Content.Shared.Vehicle.Components;
using Robust.Client.GameObjects;
namespace Content.Client.Buckle
{
@@ -9,65 +6,6 @@ namespace Content.Client.Buckle
[ComponentReference(typeof(SharedBuckleComponent))]
public sealed class BuckleComponent : SharedBuckleComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
private bool _buckled;
private int? _originalDrawDepth;
public override bool Buckled => _buckled;
public override bool TryBuckle(EntityUid user, EntityUid to)
{
// TODO: Prediction
return false;
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not BuckleComponentState buckle)
{
return;
}
_buckled = buckle.Buckled;
LastEntityBuckledTo = buckle.LastEntityBuckledTo;
DontCollide = buckle.DontCollide;
_sysMan.GetEntitySystem<ActionBlockerSystem>().UpdateCanMove(Owner);
if (!_entMan.TryGetComponent(Owner, out SpriteComponent? ownerSprite))
{
return;
}
if (LastEntityBuckledTo != null && _entMan.HasComponent<VehicleComponent>(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 (_buckled &&
LastEntityBuckledTo != null &&
EntMan.GetComponent<TransformComponent>(LastEntityBuckledTo.Value).LocalRotation.GetCardinalDir() == Direction.North &&
EntMan.TryGetComponent<SpriteComponent>(LastEntityBuckledTo, out var buckledSprite))
{
_originalDrawDepth ??= ownerSprite.DrawDepth;
ownerSprite.DrawDepth = buckledSprite.DrawDepth - 1;
return;
}
// If here, we're not turning north and should restore the saved draw depth.
if (_originalDrawDepth.HasValue)
{
ownerSprite.DrawDepth = _originalDrawDepth.Value;
_originalDrawDepth = null;
}
}
public int? OriginalDrawDepth { get; set; }
}
}

View File

@@ -1,18 +1,67 @@
using Content.Client.Buckle.Strap;
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
{
internal sealed class BuckleSystem : SharedBuckleSystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BuckleComponent, ComponentHandleState>(OnBuckleHandleState);
SubscribeLocalEvent<StrapComponent, ComponentHandleState>(OnStrapHandleState);
}
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;
}
}
private void OnStrapHandleState(EntityUid uid, StrapComponent component, ref ComponentHandleState args)
{
if (args.Current is not StrapComponentState state) return;