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:
@@ -1,5 +1,4 @@
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Shared.Item.ItemToggle.Components;
|
||||
using Content.Shared.PowerCell;
|
||||
using Content.Shared.PowerCell.Components;
|
||||
|
||||
@@ -14,11 +13,11 @@ public sealed partial class PowerCellSystem
|
||||
public override void Update(float 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;
|
||||
|
||||
if (Timing.CurTime < comp.NextUpdateTime)
|
||||
@@ -32,8 +31,6 @@ public sealed partial class PowerCellSystem
|
||||
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
|
||||
continue;
|
||||
|
||||
Toggle.TryDeactivate((uid, toggle));
|
||||
|
||||
var ev = new PowerCellSlotEmptyEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
}
|
||||
@@ -60,7 +57,10 @@ public sealed partial class PowerCellSystem
|
||||
var canUse = !args.Ejected && HasActivatableCharge(uid, component);
|
||||
|
||||
if (!canDraw)
|
||||
Toggle.TryDeactivate(uid);
|
||||
{
|
||||
var ev = new PowerCellSlotEmptyEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
}
|
||||
|
||||
if (canUse != component.CanUse || canDraw != component.CanDraw)
|
||||
{
|
||||
|
||||
@@ -282,6 +282,7 @@ public sealed partial class BorgSystem : SharedBorgSystem
|
||||
{
|
||||
Popup.PopupEntity(Loc.GetString("borg-mind-added", ("name", Identity.Name(uid, EntityManager))), uid);
|
||||
Toggle.TryActivate(uid);
|
||||
_powerCell.SetDrawEnabled(uid, _mobState.IsAlive(uid));
|
||||
_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);
|
||||
Toggle.TryDeactivate(uid);
|
||||
_powerCell.SetDrawEnabled(uid, false);
|
||||
_appearance.SetData(uid, BorgVisuals.HasPlayer, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,10 +23,14 @@ public sealed class ItemToggleSystem : EntitySystem
|
||||
[Dependency] private readonly SharedPointLightSystem _light = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
private EntityQuery<ItemToggleComponent> _query;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_query = GetEntityQuery<ItemToggleComponent>();
|
||||
|
||||
SubscribeLocalEvent<ItemToggleComponent, ComponentStartup>(OnStartup);
|
||||
SubscribeLocalEvent<ItemToggleComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<ItemToggleComponent, ItemUnwieldedEvent>(TurnOffOnUnwielded);
|
||||
@@ -69,7 +73,7 @@ public sealed class ItemToggleSystem : EntitySystem
|
||||
/// <returns>Same as <see cref="TrySetActive"/></returns>
|
||||
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 TrySetActive(ent, !ent.Comp.Activated, user, predicted);
|
||||
@@ -92,7 +96,7 @@ public sealed class ItemToggleSystem : EntitySystem
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
var uid = ent.Owner;
|
||||
@@ -135,7 +139,7 @@ public sealed class ItemToggleSystem : EntitySystem
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
var uid = ent.Owner;
|
||||
@@ -230,7 +234,7 @@ public sealed class ItemToggleSystem : EntitySystem
|
||||
|
||||
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 ent.Comp.Activated;
|
||||
|
||||
@@ -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;
|
||||
@@ -8,7 +8,6 @@ namespace Content.Shared.PowerCell;
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// With ActivatableUI it will activate and deactivate when the ui is opened and closed, drawing power inbetween.
|
||||
/// Requires <see cref="ItemToggleComponent"/> to work.
|
||||
/// </remarks>
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause]
|
||||
public sealed partial class PowerCellDrawComponent : Component
|
||||
@@ -30,9 +29,8 @@ public sealed partial class PowerCellDrawComponent : Component
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Whether drawing is enabled, regardless of ItemToggle.
|
||||
/// Whether drawing is enabled.
|
||||
/// Having no cell will still disable it.
|
||||
/// Only use this if you really don't want it to use power for some time.
|
||||
/// </summary>
|
||||
[DataField, AutoNetworkedField]
|
||||
public bool Enabled = true;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.Item.ItemToggle;
|
||||
using Content.Shared.Item.ItemToggle.Components;
|
||||
using Content.Shared.PowerCell.Components;
|
||||
using Content.Shared.Rejuvenate;
|
||||
using Robust.Shared.Containers;
|
||||
@@ -13,19 +11,22 @@ public abstract class SharedPowerCellSystem : EntitySystem
|
||||
[Dependency] protected readonly IGameTiming Timing = default!;
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] protected readonly ItemToggleSystem Toggle = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PowerCellDrawComponent, MapInitEvent>(OnMapInit);
|
||||
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, RejuvenateEvent>(OnRejuvenate);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
|
||||
}
|
||||
|
||||
SubscribeLocalEvent<PowerCellDrawComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt);
|
||||
SubscribeLocalEvent<PowerCellDrawComponent, ItemToggledEvent>(OnToggled);
|
||||
private void OnMapInit(Entity<PowerCellDrawComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
QueueUpdate((ent, ent.Comp));
|
||||
}
|
||||
|
||||
private void OnRejuvenate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args)
|
||||
@@ -70,16 +71,13 @@ public abstract class SharedPowerCellSystem : EntitySystem
|
||||
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)
|
||||
|| !HasActivatableCharge(ent, ent.Comp, user: args.User))
|
||||
args.Cancelled = true;
|
||||
}
|
||||
|
||||
private void OnToggled(Entity<PowerCellDrawComponent> ent, ref ItemToggledEvent args)
|
||||
{
|
||||
ent.Comp.NextUpdateTime = Timing.CurTime;
|
||||
if (Resolve(ent, ref ent.Comp))
|
||||
ent.Comp.NextUpdateTime = Timing.CurTime;
|
||||
}
|
||||
|
||||
public void SetDrawEnabled(Entity<PowerCellDrawComponent?> ent, bool enabled)
|
||||
|
||||
49
Content.Shared/PowerCell/ToggleCellDrawSystem.cs
Normal file
49
Content.Shared/PowerCell/ToggleCellDrawSystem.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,7 @@
|
||||
lastVisibility: 0.1
|
||||
- type: PowerCellDraw
|
||||
drawRate: 1.8 # 200 seconds on the default cell
|
||||
- type: ToggleCellDraw
|
||||
# throwing star ability
|
||||
- type: ItemCreator
|
||||
action: ActionCreateThrowingStar
|
||||
|
||||
@@ -116,6 +116,7 @@
|
||||
price: 500
|
||||
- type: PowerCellDraw
|
||||
drawRate: 4
|
||||
- type: ToggleCellDraw
|
||||
- type: ItemSlots
|
||||
slots:
|
||||
cell_slot:
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
# TODO: or just have sentient speedboots be fast idk
|
||||
- type: PowerCellDraw
|
||||
drawRate: 0.6
|
||||
# no ToggleCellDraw since dont want to lose access when power is gone
|
||||
- type: ItemSlots
|
||||
slots:
|
||||
cell_slot:
|
||||
|
||||
@@ -9,3 +9,4 @@
|
||||
- type: PowerCellDraw
|
||||
drawRate: 0
|
||||
useRate: 20
|
||||
- type: ToggleCellDraw
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
components:
|
||||
- type: PowerCellDraw
|
||||
drawRate: 1.2 #Calculated for 5 minutes on a small cell
|
||||
- type: ToggleCellDraw
|
||||
- type: ActivatableUIRequiresPowerCell
|
||||
|
||||
- type: entity
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
- type: PowerCellDraw
|
||||
drawRate: 1
|
||||
useRate: 0
|
||||
- type: ToggleCellDraw
|
||||
|
||||
- type: entity
|
||||
id: AnomalyLocatorEmpty
|
||||
@@ -101,6 +102,7 @@
|
||||
- type: PowerCellDraw
|
||||
drawRate: 1
|
||||
useRate: 0
|
||||
- type: ToggleCellDraw
|
||||
|
||||
- type: entity
|
||||
id: AnomalyLocatorWideEmpty
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
False: { visible: false }
|
||||
- type: PowerCellDraw
|
||||
drawRate: 1.5
|
||||
- type: ToggleCellDraw
|
||||
- type: ActivatableUI
|
||||
key: enum.RadarConsoleUiKey.Key
|
||||
inHandsOnly: true
|
||||
|
||||
@@ -169,6 +169,7 @@
|
||||
- type: ItemToggle
|
||||
onUse: false
|
||||
- type: PowerCellDraw
|
||||
- type: ToggleCellDraw
|
||||
- type: Sprite
|
||||
sprite: Objects/Weapons/Guns/Launchers/tether_gun.rsi
|
||||
layers:
|
||||
@@ -216,6 +217,7 @@
|
||||
- type: ItemToggle
|
||||
onUse: false
|
||||
- type: PowerCellDraw
|
||||
- type: ToggleCellDraw
|
||||
- type: Sprite
|
||||
sprite: Objects/Weapons/Guns/Launchers/force_gun.rsi
|
||||
layers:
|
||||
|
||||
Reference in New Issue
Block a user