Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouple ItemToggle from PowerCellDraw #31392

Merged
merged 10 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Content.Server/PowerCell/PowerCellSystem.Draw.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Content.Server.Power.Components;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;

Expand All @@ -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)
Expand All @@ -32,8 +31,6 @@ public override void Update(float frameTime)
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
continue;

Toggle.TryDeactivate((uid, toggle));

var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
Expand All @@ -60,7 +57,10 @@ private void OnDrawCellChanged(EntityUid uid, PowerCellDrawComponent component,
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)
{
Expand Down
2 changes: 2 additions & 0 deletions Content.Server/Silicons/Borgs/BorgSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public void BorgActivate(EntityUid uid, BorgChassisComponent component)
{
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);
}

Expand All @@ -292,6 +293,7 @@ public void BorgDeactivate(EntityUid uid, BorgChassisComponent component)
{
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);
}

Expand Down
12 changes: 8 additions & 4 deletions Content.Shared/Item/ItemToggle/ItemToggleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -69,7 +73,7 @@ private void OnUseInHand(Entity<ItemToggleComponent> ent, ref UseInHandEvent arg
/// <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);
Expand All @@ -92,7 +96,7 @@ public bool TrySetActive(Entity<ItemToggleComponent?> ent, bool active, EntityUi
/// </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;
Expand Down Expand Up @@ -135,7 +139,7 @@ public bool TryActivate(Entity<ItemToggleComponent?> ent, EntityUid? user = null
/// </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;
Expand Down Expand Up @@ -230,7 +234,7 @@ private void TurnOnOnWielded(Entity<ItemToggleComponent> ent, ref ItemWieldedEve

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;
Expand Down
10 changes: 10 additions & 0 deletions Content.Shared/PowerCell/Components/ToggleCellDrawComponent.cs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions Content.Shared/PowerCell/PowerCellDrawComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
26 changes: 12 additions & 14 deletions Content.Shared/PowerCell/SharedPowerCellSystem.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -70,16 +71,13 @@ protected virtual void OnCellRemoved(EntityUid uid, PowerCellSlotComponent compo
RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
}

private void OnActivateAttempt(Entity<PowerCellDrawComponent> ent, ref ItemToggleActivateAttemptEvent args)
{
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)
/// <summary>
/// Makes the draw logic update in the next tick.
/// </summary>
public void QueueUpdate(Entity<PowerCellDrawComponent?> ent)
{
ent.Comp.NextUpdateTime = Timing.CurTime;
if (Resolve(ent, ref ent.Comp))
ent.Comp.NextUpdateTime = Timing.CurTime;
}

public void SetDrawEnabled(Entity<PowerCellDrawComponent?> ent, bool enabled)
Expand Down
49 changes: 49 additions & 0 deletions Content.Shared/PowerCell/ToggleCellDrawSystem.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Clothing/Shoes/misc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
price: 500
- type: PowerCellDraw
drawRate: 4
- type: ToggleCellDraw
- type: ItemSlots
slots:
cell_slot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- type: PowerCellDraw
drawRate: 0
useRate: 20
- type: ToggleCellDraw
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
components:
- type: PowerCellDraw
drawRate: 1.2 #Calculated for 5 minutes on a small cell
- type: ToggleCellDraw
- type: ActivatableUIRequiresPowerCell

- type: entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- type: PowerCellDraw
drawRate: 1
useRate: 0
- type: ToggleCellDraw

- type: entity
id: AnomalyLocatorEmpty
Expand Down Expand Up @@ -101,6 +102,7 @@
- type: PowerCellDraw
drawRate: 1
useRate: 0
- type: ToggleCellDraw

- type: entity
id: AnomalyLocatorWideEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
False: { visible: false }
- type: PowerCellDraw
drawRate: 1.5
- type: ToggleCellDraw
- type: ActivatableUI
key: enum.RadarConsoleUiKey.Key
inHandsOnly: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
- type: ItemToggle
onUse: false
- type: PowerCellDraw
- type: ToggleCellDraw
- type: Sprite
sprite: Objects/Weapons/Guns/Launchers/tether_gun.rsi
layers:
Expand Down Expand Up @@ -216,6 +217,7 @@
- type: ItemToggle
onUse: false
- type: PowerCellDraw
- type: ToggleCellDraw
- type: Sprite
sprite: Objects/Weapons/Guns/Launchers/force_gun.rsi
layers:
Expand Down
Loading