using Content.Shared.Containers.ItemSlots; using Content.Shared.PowerCell.Components; using Content.Shared.Rejuvenate; using Robust.Shared.Containers; using Robust.Shared.Timing; namespace Content.Shared.PowerCell; public abstract class SharedPowerCellSystem : EntitySystem { [Dependency] protected readonly IGameTiming Timing = default!; [Dependency] private readonly ItemSlotsSystem _itemSlots = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnRejuvenate); SubscribeLocalEvent(OnCellInserted); SubscribeLocalEvent(OnCellRemoved); SubscribeLocalEvent(OnCellInsertAttempt); } private void OnMapInit(Entity ent, ref MapInitEvent args) { ent.Comp.NextUpdateTime = Timing.CurTime + ent.Comp.Delay; } private void OnRejuvenate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args) { if (!_itemSlots.TryGetSlot(uid, component.CellSlotId, out var itemSlot) || !itemSlot.Item.HasValue) return; // charge entity batteries and remove booby traps. RaiseLocalEvent(itemSlot.Item.Value, args); } private void OnCellInsertAttempt(EntityUid uid, PowerCellSlotComponent component, ContainerIsInsertingAttemptEvent args) { if (!component.Initialized) return; if (args.Container.ID != component.CellSlotId) return; if (!HasComp(args.EntityUid)) { args.Cancel(); } } private void OnCellInserted(EntityUid uid, PowerCellSlotComponent component, EntInsertedIntoContainerMessage args) { if (!component.Initialized) return; if (args.Container.ID != component.CellSlotId) return; _appearance.SetData(uid, PowerCellSlotVisuals.Enabled, true); RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false); } protected virtual void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args) { if (args.Container.ID != component.CellSlotId) return; _appearance.SetData(uid, PowerCellSlotVisuals.Enabled, false); RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false); } public void SetDrawEnabled(Entity ent, bool enabled) { if (!Resolve(ent, ref ent.Comp, false) || ent.Comp.Enabled == enabled) return; if (enabled) ent.Comp.NextUpdateTime = Timing.CurTime; ent.Comp.Enabled = enabled; Dirty(ent, ent.Comp); } /// /// Returns whether the entity has a slotted battery and charge. /// /// /// /// /// Popup to this user with the relevant detail if specified. public abstract bool HasActivatableCharge( EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null); /// /// Whether the power cell has any power at all for the draw rate. /// public abstract bool HasDrawCharge( EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null); }