| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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<StrapComponent, ComponentStartup>(OnStrapStartup);
- SubscribeLocalEvent<StrapComponent, ComponentShutdown>(OnStrapShutdown);
- SubscribeLocalEvent<StrapComponent, ComponentRemove>((e, c, _) => StrapRemoveAll(e, c));
- SubscribeLocalEvent<StrapComponent, ContainerGettingInsertedAttemptEvent>(OnStrapContainerGettingInsertedAttempt);
- SubscribeLocalEvent<StrapComponent, DestructionEventArgs>((e, c, _) => StrapRemoveAll(e, c));
- SubscribeLocalEvent<StrapComponent, BreakageEventArgs>((e, c, _) => StrapRemoveAll(e, c));
- SubscribeLocalEvent<StrapComponent, FoldAttemptEvent>(OnAttemptFold);
- SubscribeLocalEvent<StrapComponent, MachineDeconstructedEvent>((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 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;
- }
- /// <summary>
- /// Remove everything attached to the strap
- /// </summary>
- 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<BuckleComponent>(buckle)?.Size ?? 0;
- }
- return avail >= buckleComp.Size;
- }
- /// <summary>
- /// Sets the enabled field in the strap component to a value
- /// </summary>
- 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);
- }
- }
|