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(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)); } private void OnStrapStartup(EntityUid uid, StrapComponent component, ComponentStartup args) { Appearance.SetData(uid, StrapVisuals.State, component.BuckledEntities.Count != 0); } private void OnStrapShutdown(EntityUid uid, StrapComponent component, ComponentShutdown args) { if (!TerminatingOrDeleted(uid)) StrapRemoveAll(uid, component); } private void OnStrapTerminating(Entity entity, ref EntityTerminatingEvent args) { StrapRemoveAll(entity, entity.Comp); } private void OnStrapContainerGettingInsertedAttempt(EntityUid uid, StrapComponent component, 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) args.Cancel(); } private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args) { if (args.Cancelled) return; args.Cancelled = component.BuckledEntities.Count != 0; } /// /// Remove everything attached to the strap /// private void StrapRemoveAll(EntityUid uid, StrapComponent strapComp) { foreach (var entity in strapComp.BuckledEntities.ToArray()) { Unbuckle(entity, entity); } } 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); } }