diff --git a/Content.Client/Buckle/BuckleSystem.cs b/Content.Client/Buckle/BuckleSystem.cs index 8e462659a2..22996f1553 100644 --- a/Content.Client/Buckle/BuckleSystem.cs +++ b/Content.Client/Buckle/BuckleSystem.cs @@ -15,26 +15,12 @@ internal sealed partial class BuckleSystem : SharedBuckleSystem [Dependency] private SharedTransformSystem _xformSystem = default!; [Dependency] private SpriteSystem _sprite = default!; - public override void Initialize() - { - base.Initialize(); + [Dependency] private EntityQuery _spriteQuery = default!; - SubscribeLocalEvent(OnAppearanceChange); - SubscribeLocalEvent(OnStrapMoveEvent); - SubscribeLocalEvent(OnBuckledEvent); - SubscribeLocalEvent(OnUnbuckledEvent); - SubscribeLocalEvent(OnMobCollide); - } + #region Event Handlers - private void OnMobCollide(Entity ent, ref AttemptMobCollideEvent args) - { - if (ent.Comp.Buckled) - { - args.Cancelled = true; - } - } - - private void OnStrapMoveEvent(EntityUid uid, StrapComponent component, ref MoveEvent args) + [SubscribeLocalEvent] + private void OnStrapMoveEvent(Entity ent, ref MoveEvent args) { // I'm moving this to the client-side system, but for the sake of posterity let's keep this comment: // > This is mega cursed. Please somebody save me from Mr Buckle's wild ride @@ -49,24 +35,24 @@ internal sealed partial class BuckleSystem : SharedBuckleSystem // Give some of the sprite rotations their own drawdepth, maybe as an offset within the rsi, or something like this // And we won't ever need to set the draw depth manually - if (!component.ModifyBuckleDrawDepth) + if (!ent.Comp.ModifyBuckleDrawDepth) return; if (args.NewRotation == args.OldRotation) return; - if (!TryComp(uid, out var strapSprite)) + if (!_spriteQuery.TryComp(ent, out SpriteComponent? strapSprite)) return; - var angle = _xformSystem.GetWorldRotation(uid) + _eye.CurrentEye.Rotation; // Get true screen position, or close enough + var angle = _xformSystem.GetWorldRotation(ent) + _eye.CurrentEye.Rotation; // Get true screen position, or close enough var isNorth = angle.GetCardinalDir() == Direction.North; - foreach (var buckledEntity in component.BuckledEntities) + foreach (var buckledEntity in ent.Comp.BuckledEntities) { if (!TryComp(buckledEntity, out var buckle)) continue; - if (!TryComp(buckledEntity, out var buckledSprite)) + if (!_spriteQuery.TryComp(buckledEntity, out SpriteComponent? buckledSprite)) continue; if (isNorth) @@ -83,19 +69,29 @@ internal sealed partial class BuckleSystem : SharedBuckleSystem } } + [SubscribeLocalEvent] + private void OnMobCollide(Entity ent, ref AttemptMobCollideEvent args) + { + if (ent.Comp.Buckled) + { + args.Cancelled = true; + } + } + /// /// Lower the draw depth of the buckled entity without needing for the strap entity to rotate/move. /// Only do so when the entity is facing screen-local north /// + [SubscribeLocalEvent] private void OnBuckledEvent(Entity ent, ref BuckledEvent args) { if (!args.Strap.Comp.ModifyBuckleDrawDepth) return; - if (!TryComp(args.Strap, out var strapSprite)) + if (!_spriteQuery.TryComp(args.Strap, out SpriteComponent? strapSprite)) return; - if (!TryComp(ent.Owner, out var buckledSprite)) + if (!_spriteQuery.TryComp(ent.Owner, out SpriteComponent? buckledSprite)) return; var angle = _xformSystem.GetWorldRotation(args.Strap) + _eye.CurrentEye.Rotation; // Get true screen position, or close enough @@ -110,12 +106,13 @@ internal sealed partial class BuckleSystem : SharedBuckleSystem /// /// Was the draw depth of the buckled entity lowered? Reset it upon unbuckling. /// + [SubscribeLocalEvent] private void OnUnbuckledEvent(Entity ent, ref UnbuckledEvent args) { if (!args.Strap.Comp.ModifyBuckleDrawDepth) return; - if (!TryComp(ent.Owner, out var buckledSprite)) + if (!_spriteQuery.TryComp(ent.Owner, out SpriteComponent? buckledSprite)) return; if (!ent.Comp.OriginalDrawDepth.HasValue) @@ -125,21 +122,23 @@ internal sealed partial class BuckleSystem : SharedBuckleSystem ent.Comp.OriginalDrawDepth = null; } - private void OnAppearanceChange(EntityUid uid, BuckleComponent component, ref AppearanceChangeEvent args) + [SubscribeLocalEvent] + private void OnAppearanceChange(Entity ent, ref AppearanceChangeEvent args) { - if (!TryComp(uid, out var rotVisuals)) + if (!TryComp(ent, out var rotVisuals)) return; - if (!Appearance.TryGetData(uid, BuckleVisuals.Buckled, out var buckled, args.Component) || + if (!Appearance.TryGetData(ent, BuckleVisuals.Buckled, out var buckled, args.Component) || !buckled || args.Sprite == null) { - _rotationVisualizerSystem.SetHorizontalAngle((uid, rotVisuals), rotVisuals.DefaultRotation); + _rotationVisualizerSystem.SetHorizontalAngle((ent, rotVisuals), rotVisuals.DefaultRotation); return; } // Animate strapping yourself to something at a given angle // TODO: Dump this when buckle is better - _rotationVisualizerSystem.AnimateSpriteRotation(uid, args.Sprite, rotVisuals.HorizontalRotation, 0.125f); + _rotationVisualizerSystem.AnimateSpriteRotation(ent, args.Sprite, rotVisuals.HorizontalRotation, 0.125f); } + #endregion Event Handlers } diff --git a/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs b/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs index c101340421..cae6f705b4 100644 --- a/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs +++ b/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using Content.Shared.Buckle.Components; using Content.Shared.Construction; using Content.Shared.Destructible; @@ -12,58 +12,69 @@ public abstract partial class SharedBuckleSystem { private void InitializeStrap() { - SubscribeLocalEvent(OnStrapStartup); - SubscribeLocalEvent(OnStrapShutdown); - SubscribeLocalEvent(OnStrapTerminating); - SubscribeLocalEvent((e, c, _) => StrapRemoveAll(e, c)); - - SubscribeLocalEvent(OnStrapContainerGettingInsertedAttempt); - SubscribeLocalEvent((e, c, _) => StrapRemoveAll(e, c)); - SubscribeLocalEvent((e, c, _) => StrapRemoveAll(e, c)); - - SubscribeLocalEvent(OnAttemptFold); - SubscribeLocalEvent((e, c, _) => StrapRemoveAll(e, c)); + SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); + SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); + SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); + SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); } - private void OnStrapStartup(EntityUid uid, StrapComponent component, ComponentStartup args) + [SubscribeLocalEvent] + private void OnStrapStartup(Entity ent, ref ComponentStartup args) { - Appearance.SetData(uid, StrapVisuals.State, component.BuckledEntities.Count != 0); + Appearance.SetData(ent, StrapVisuals.State, ent.Comp.BuckledEntities.Count != 0); + + // Raise events on anything that starts buckled. + foreach (var buckle in ent.Comp.BuckledEntities) + { + if (!TryComp(buckle, out var buckleComp)) + continue; + + var ev = new StrappedEvent(ent, (buckle, buckleComp)); + RaiseLocalEvent(ent, ref ev); + + var gotEv = new BuckledEvent(ent, (buckle, buckleComp)); + RaiseLocalEvent(buckle, ref gotEv); + } } - private void OnStrapShutdown(EntityUid uid, StrapComponent component, ComponentShutdown args) + [SubscribeLocalEvent] + private void OnStrapShutdown(Entity ent, ref ComponentShutdown args) { - if (!TerminatingOrDeleted(uid)) - StrapRemoveAll(uid, component); + if (!TerminatingOrDeleted(ent)) + StrapRemoveAll(ent); } - private void OnStrapTerminating(Entity entity, ref EntityTerminatingEvent args) + [SubscribeLocalEvent] + private void OnStrapTerminating(Entity ent, ref EntityTerminatingEvent args) { - StrapRemoveAll(entity, entity.Comp); + StrapRemoveAll(ent); } - private void OnStrapContainerGettingInsertedAttempt(EntityUid uid, StrapComponent component, ContainerGettingInsertedAttemptEvent args) + [SubscribeLocalEvent] + private void OnStrapContainerGettingInsertedAttempt(Entity ent, ref ContainerGettingInsertedAttemptEvent args) { // If someone is attempting to put this item inside of a backpack, ensure that it has no entities strapped to it. - if (args.Container.ID == StorageComponent.ContainerId && component.BuckledEntities.Count != 0) + if (args.Container.ID == StorageComponent.ContainerId && ent.Comp.BuckledEntities.Count != 0) args.Cancel(); } - private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args) + [SubscribeLocalEvent] + private void OnAttemptFold(Entity ent, ref FoldAttemptEvent args) { if (args.Cancelled) return; - args.Cancelled = component.BuckledEntities.Count != 0; + args.Cancelled = ent.Comp.BuckledEntities.Count != 0; } /// /// Remove everything attached to the strap /// - private void StrapRemoveAll(EntityUid uid, StrapComponent strapComp) + private void StrapRemoveAll(Entity ent) { - foreach (var entity in strapComp.BuckledEntities.ToArray()) + foreach (var buckle in ent.Comp.BuckledEntities.ToArray()) { - Unbuckle(entity, entity); + Unbuckle(buckle, buckle); } } @@ -94,6 +105,6 @@ public abstract partial class SharedBuckleSystem Dirty(strapUid, strapComp); if (!enabled) - StrapRemoveAll(strapUid, strapComp); + StrapRemoveAll((strapUid, strapComp)); } }