using System.Linq; using Content.Shared.Buckle.Components; using Content.Shared.Construction; using Content.Shared.Destructible; using Content.Shared.Foldable; using Content.Shared.Storage; using Robust.Shared.Containers; namespace Content.Shared.Buckle; public abstract partial class SharedBuckleSystem { private void InitializeStrap() { SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); SubscribeLocalEvent((e, ref _) => StrapRemoveAll(e)); } [SubscribeLocalEvent] private void OnStrapStartup(Entity ent, ref ComponentStartup args) { 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); } } [SubscribeLocalEvent] private void OnStrapShutdown(Entity ent, ref ComponentShutdown args) { if (!TerminatingOrDeleted(ent)) StrapRemoveAll(ent); } [SubscribeLocalEvent] private void OnStrapTerminating(Entity ent, ref EntityTerminatingEvent args) { StrapRemoveAll(ent); } [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 && ent.Comp.BuckledEntities.Count != 0) args.Cancel(); } [SubscribeLocalEvent] private void OnAttemptFold(Entity ent, ref FoldAttemptEvent args) { if (args.Cancelled) return; args.Cancelled = ent.Comp.BuckledEntities.Count != 0; } /// /// Remove everything attached to the strap /// private void StrapRemoveAll(Entity ent) { foreach (var buckle in ent.Comp.BuckledEntities.ToArray()) { Unbuckle(buckle, buckle); } } private bool StrapHasSpace(EntityUid strapUid, BuckleComponent buckleComp, StrapComponent? strapComp = null) { if (!Resolve(strapUid, ref strapComp, false)) return false; var avail = strapComp.Size; foreach (var buckle in strapComp.BuckledEntities) { avail -= CompOrNull(buckle)?.Size ?? 0; } return avail >= buckleComp.Size; } /// /// Sets the enabled field in the strap component to a value /// public void StrapSetEnabled(EntityUid strapUid, bool enabled, StrapComponent? strapComp = null) { if (!Resolve(strapUid, ref strapComp, false) || strapComp.Enabled == enabled) return; strapComp.Enabled = enabled; Dirty(strapUid, strapComp); if (!enabled) StrapRemoveAll((strapUid, strapComp)); } }