decouple ItemToggle from PowerCellDraw (#31392)

* remove ItemToggle from PowerCellDraw query

* add EntityQuery for resolves, make them all optional

* move integration to ToggleCellDraw

* add ToggleCellDraw to almost every PowerCellDraw prototype

* :trollface:

* :trollface:

* :trollface:

* let it disable on mapinit

* set update time on mapinit, make borg power logic consistent now

* :trollface:

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2024-08-25 12:17:03 +00:00
committed by GitHub
parent 42865ae705
commit e1df251cbc
15 changed files with 99 additions and 28 deletions

View File

@@ -1,5 +1,4 @@
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.PowerCell; using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components; using Content.Shared.PowerCell.Components;
@@ -14,11 +13,11 @@ public sealed partial class PowerCellSystem
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
base.Update(frameTime); base.Update(frameTime);
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent, ItemToggleComponent>(); var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
while (query.MoveNext(out var uid, out var comp, out var slot, out var toggle)) while (query.MoveNext(out var uid, out var comp, out var slot))
{ {
if (!comp.Enabled || !toggle.Activated) if (!comp.Enabled)
continue; continue;
if (Timing.CurTime < comp.NextUpdateTime) if (Timing.CurTime < comp.NextUpdateTime)
@@ -32,8 +31,6 @@ public sealed partial class PowerCellSystem
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery)) if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
continue; continue;
Toggle.TryDeactivate((uid, toggle));
var ev = new PowerCellSlotEmptyEvent(); var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev); RaiseLocalEvent(uid, ref ev);
} }
@@ -60,7 +57,10 @@ public sealed partial class PowerCellSystem
var canUse = !args.Ejected && HasActivatableCharge(uid, component); var canUse = !args.Ejected && HasActivatableCharge(uid, component);
if (!canDraw) if (!canDraw)
Toggle.TryDeactivate(uid); {
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
if (canUse != component.CanUse || canDraw != component.CanDraw) if (canUse != component.CanUse || canDraw != component.CanDraw)
{ {

View File

@@ -282,6 +282,7 @@ public sealed partial class BorgSystem : SharedBorgSystem
{ {
Popup.PopupEntity(Loc.GetString("borg-mind-added", ("name", Identity.Name(uid, EntityManager))), uid); Popup.PopupEntity(Loc.GetString("borg-mind-added", ("name", Identity.Name(uid, EntityManager))), uid);
Toggle.TryActivate(uid); Toggle.TryActivate(uid);
_powerCell.SetDrawEnabled(uid, _mobState.IsAlive(uid));
_appearance.SetData(uid, BorgVisuals.HasPlayer, true); _appearance.SetData(uid, BorgVisuals.HasPlayer, true);
} }
@@ -292,6 +293,7 @@ public sealed partial class BorgSystem : SharedBorgSystem
{ {
Popup.PopupEntity(Loc.GetString("borg-mind-removed", ("name", Identity.Name(uid, EntityManager))), uid); Popup.PopupEntity(Loc.GetString("borg-mind-removed", ("name", Identity.Name(uid, EntityManager))), uid);
Toggle.TryDeactivate(uid); Toggle.TryDeactivate(uid);
_powerCell.SetDrawEnabled(uid, false);
_appearance.SetData(uid, BorgVisuals.HasPlayer, false); _appearance.SetData(uid, BorgVisuals.HasPlayer, false);
} }

View File

@@ -23,10 +23,14 @@ public sealed class ItemToggleSystem : EntitySystem
[Dependency] private readonly SharedPointLightSystem _light = default!; [Dependency] private readonly SharedPointLightSystem _light = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!; [Dependency] private readonly SharedPopupSystem _popup = default!;
private EntityQuery<ItemToggleComponent> _query;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_query = GetEntityQuery<ItemToggleComponent>();
SubscribeLocalEvent<ItemToggleComponent, ComponentStartup>(OnStartup); SubscribeLocalEvent<ItemToggleComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<ItemToggleComponent, MapInitEvent>(OnMapInit); SubscribeLocalEvent<ItemToggleComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ItemToggleComponent, ItemUnwieldedEvent>(TurnOffOnUnwielded); SubscribeLocalEvent<ItemToggleComponent, ItemUnwieldedEvent>(TurnOffOnUnwielded);
@@ -69,7 +73,7 @@ public sealed class ItemToggleSystem : EntitySystem
/// <returns>Same as <see cref="TrySetActive"/></returns> /// <returns>Same as <see cref="TrySetActive"/></returns>
public bool Toggle(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true) public bool Toggle(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true)
{ {
if (!Resolve(ent, ref ent.Comp)) if (!_query.Resolve(ent, ref ent.Comp, false))
return false; return false;
return TrySetActive(ent, !ent.Comp.Activated, user, predicted); return TrySetActive(ent, !ent.Comp.Activated, user, predicted);
@@ -92,7 +96,7 @@ public sealed class ItemToggleSystem : EntitySystem
/// </summary> /// </summary>
public bool TryActivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true) public bool TryActivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true)
{ {
if (!Resolve(ent, ref ent.Comp)) if (!_query.Resolve(ent, ref ent.Comp, false))
return false; return false;
var uid = ent.Owner; var uid = ent.Owner;
@@ -135,7 +139,7 @@ public sealed class ItemToggleSystem : EntitySystem
/// </summary> /// </summary>
public bool TryDeactivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true) public bool TryDeactivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null, bool predicted = true)
{ {
if (!Resolve(ent, ref ent.Comp)) if (!_query.Resolve(ent, ref ent.Comp, false))
return false; return false;
var uid = ent.Owner; var uid = ent.Owner;
@@ -230,7 +234,7 @@ public sealed class ItemToggleSystem : EntitySystem
public bool IsActivated(Entity<ItemToggleComponent?> ent) public bool IsActivated(Entity<ItemToggleComponent?> ent)
{ {
if (!Resolve(ent, ref ent.Comp, false)) if (!_query.Resolve(ent, ref ent.Comp, false))
return true; // assume always activated if no component return true; // assume always activated if no component
return ent.Comp.Activated; return ent.Comp.Activated;

View File

@@ -0,0 +1,10 @@
using Robust.Shared.GameStates;
namespace Content.Shared.PowerCell.Components;
/// <summary>
/// Integrate PowerCellDraw and ItemToggle.
/// Make toggling this item require power, and deactivates the item when power runs out.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ToggleCellDrawComponent : Component;

View File

@@ -8,7 +8,6 @@ namespace Content.Shared.PowerCell;
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// With ActivatableUI it will activate and deactivate when the ui is opened and closed, drawing power inbetween. /// With ActivatableUI it will activate and deactivate when the ui is opened and closed, drawing power inbetween.
/// Requires <see cref="ItemToggleComponent"/> to work.
/// </remarks> /// </remarks>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause] [RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause]
public sealed partial class PowerCellDrawComponent : Component public sealed partial class PowerCellDrawComponent : Component
@@ -30,9 +29,8 @@ public sealed partial class PowerCellDrawComponent : Component
#endregion #endregion
/// <summary> /// <summary>
/// Whether drawing is enabled, regardless of ItemToggle. /// Whether drawing is enabled.
/// Having no cell will still disable it. /// Having no cell will still disable it.
/// Only use this if you really don't want it to use power for some time.
/// </summary> /// </summary>
[DataField, AutoNetworkedField] [DataField, AutoNetworkedField]
public bool Enabled = true; public bool Enabled = true;

View File

@@ -1,6 +1,4 @@
using Content.Shared.Containers.ItemSlots; using Content.Shared.Containers.ItemSlots;
using Content.Shared.Item.ItemToggle;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.PowerCell.Components; using Content.Shared.PowerCell.Components;
using Content.Shared.Rejuvenate; using Content.Shared.Rejuvenate;
using Robust.Shared.Containers; using Robust.Shared.Containers;
@@ -13,19 +11,22 @@ public abstract class SharedPowerCellSystem : EntitySystem
[Dependency] protected readonly IGameTiming Timing = default!; [Dependency] protected readonly IGameTiming Timing = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!; [Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] protected readonly ItemToggleSystem Toggle = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<PowerCellDrawComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<PowerCellSlotComponent, RejuvenateEvent>(OnRejuvenate); SubscribeLocalEvent<PowerCellSlotComponent, RejuvenateEvent>(OnRejuvenate);
SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted); SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved); SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt); SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
}
SubscribeLocalEvent<PowerCellDrawComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt); private void OnMapInit(Entity<PowerCellDrawComponent> ent, ref MapInitEvent args)
SubscribeLocalEvent<PowerCellDrawComponent, ItemToggledEvent>(OnToggled); {
QueueUpdate((ent, ent.Comp));
} }
private void OnRejuvenate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args) private void OnRejuvenate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args)
@@ -70,16 +71,13 @@ public abstract class SharedPowerCellSystem : EntitySystem
RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false); RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
} }
private void OnActivateAttempt(Entity<PowerCellDrawComponent> ent, ref ItemToggleActivateAttemptEvent args) /// <summary>
/// Makes the draw logic update in the next tick.
/// </summary>
public void QueueUpdate(Entity<PowerCellDrawComponent?> ent)
{ {
if (!HasDrawCharge(ent, ent.Comp, user: args.User) if (Resolve(ent, ref ent.Comp))
|| !HasActivatableCharge(ent, ent.Comp, user: args.User)) ent.Comp.NextUpdateTime = Timing.CurTime;
args.Cancelled = true;
}
private void OnToggled(Entity<PowerCellDrawComponent> ent, ref ItemToggledEvent args)
{
ent.Comp.NextUpdateTime = Timing.CurTime;
} }
public void SetDrawEnabled(Entity<PowerCellDrawComponent?> ent, bool enabled) public void SetDrawEnabled(Entity<PowerCellDrawComponent?> ent, bool enabled)

View File

@@ -0,0 +1,49 @@
using Content.Shared.Item.ItemToggle;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.PowerCell.Components;
namespace Content.Shared.PowerCell;
/// <summary>
/// Handles events to integrate PowerCellDraw with ItemToggle
/// </summary>
public sealed class ToggleCellDrawSystem : EntitySystem
{
[Dependency] private readonly ItemToggleSystem _toggle = default!;
[Dependency] private readonly SharedPowerCellSystem _cell = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ToggleCellDrawComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ToggleCellDrawComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt);
SubscribeLocalEvent<ToggleCellDrawComponent, ItemToggledEvent>(OnToggled);
SubscribeLocalEvent<ToggleCellDrawComponent, PowerCellSlotEmptyEvent>(OnEmpty);
}
private void OnMapInit(Entity<ToggleCellDrawComponent> ent, ref MapInitEvent args)
{
_cell.SetDrawEnabled(ent.Owner, _toggle.IsActivated(ent.Owner));
}
private void OnActivateAttempt(Entity<ToggleCellDrawComponent> ent, ref ItemToggleActivateAttemptEvent args)
{
if (!_cell.HasDrawCharge(ent, user: args.User)
|| !_cell.HasActivatableCharge(ent, user: args.User))
args.Cancelled = true;
}
private void OnToggled(Entity<ToggleCellDrawComponent> ent, ref ItemToggledEvent args)
{
var uid = ent.Owner;
var draw = Comp<PowerCellDrawComponent>(uid);
_cell.QueueUpdate((uid, draw));
_cell.SetDrawEnabled((uid, draw), args.Activated);
}
private void OnEmpty(Entity<ToggleCellDrawComponent> ent, ref PowerCellSlotEmptyEvent args)
{
_toggle.TryDeactivate(ent.Owner);
}
}

View File

@@ -160,6 +160,7 @@
lastVisibility: 0.1 lastVisibility: 0.1
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 1.8 # 200 seconds on the default cell drawRate: 1.8 # 200 seconds on the default cell
- type: ToggleCellDraw
# throwing star ability # throwing star ability
- type: ItemCreator - type: ItemCreator
action: ActionCreateThrowingStar action: ActionCreateThrowingStar

View File

@@ -116,6 +116,7 @@
price: 500 price: 500
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 4 drawRate: 4
- type: ToggleCellDraw
- type: ItemSlots - type: ItemSlots
slots: slots:
cell_slot: cell_slot:

View File

@@ -126,6 +126,7 @@
# TODO: or just have sentient speedboots be fast idk # TODO: or just have sentient speedboots be fast idk
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 0.6 drawRate: 0.6
# no ToggleCellDraw since dont want to lose access when power is gone
- type: ItemSlots - type: ItemSlots
slots: slots:
cell_slot: cell_slot:

View File

@@ -9,3 +9,4 @@
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 0 drawRate: 0
useRate: 20 useRate: 20
- type: ToggleCellDraw

View File

@@ -47,6 +47,7 @@
components: components:
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 1.2 #Calculated for 5 minutes on a small cell drawRate: 1.2 #Calculated for 5 minutes on a small cell
- type: ToggleCellDraw
- type: ActivatableUIRequiresPowerCell - type: ActivatableUIRequiresPowerCell
- type: entity - type: entity

View File

@@ -68,6 +68,7 @@
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 1 drawRate: 1
useRate: 0 useRate: 0
- type: ToggleCellDraw
- type: entity - type: entity
id: AnomalyLocatorEmpty id: AnomalyLocatorEmpty
@@ -101,6 +102,7 @@
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 1 drawRate: 1
useRate: 0 useRate: 0
- type: ToggleCellDraw
- type: entity - type: entity
id: AnomalyLocatorWideEmpty id: AnomalyLocatorWideEmpty

View File

@@ -26,6 +26,7 @@
False: { visible: false } False: { visible: false }
- type: PowerCellDraw - type: PowerCellDraw
drawRate: 1.5 drawRate: 1.5
- type: ToggleCellDraw
- type: ActivatableUI - type: ActivatableUI
key: enum.RadarConsoleUiKey.Key key: enum.RadarConsoleUiKey.Key
inHandsOnly: true inHandsOnly: true

View File

@@ -169,6 +169,7 @@
- type: ItemToggle - type: ItemToggle
onUse: false onUse: false
- type: PowerCellDraw - type: PowerCellDraw
- type: ToggleCellDraw
- type: Sprite - type: Sprite
sprite: Objects/Weapons/Guns/Launchers/tether_gun.rsi sprite: Objects/Weapons/Guns/Launchers/tether_gun.rsi
layers: layers:
@@ -216,6 +217,7 @@
- type: ItemToggle - type: ItemToggle
onUse: false onUse: false
- type: PowerCellDraw - type: PowerCellDraw
- type: ToggleCellDraw
- type: Sprite - type: Sprite
sprite: Objects/Weapons/Guns/Launchers/force_gun.rsi sprite: Objects/Weapons/Guns/Launchers/force_gun.rsi
layers: layers: