Skip to content

Commit 4dfcacb

Browse files
Update battery-gun item status when charge changes (space-wizards#6579)
Co-authored-by: mirrorcult <[email protected]>
1 parent f88cbba commit 4dfcacb

File tree

5 files changed

+34
-73
lines changed

5 files changed

+34
-73
lines changed

Content.Client/Weapons/Ranged/Barrels/Components/ClientBatteryBarrelComponent.cs

+15-37
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,39 @@
1-
using System;
21
using Content.Client.Items.Components;
32
using Content.Client.Stylesheets;
4-
using Content.Shared.Containers.ItemSlots;
53
using Content.Shared.Weapons.Ranged.Barrels.Components;
64
using Robust.Client.Graphics;
75
using Robust.Client.UserInterface;
86
using Robust.Client.UserInterface.Controls;
9-
using Robust.Shared.GameObjects;
107
using Robust.Shared.GameStates;
11-
using Robust.Shared.Maths;
12-
using Robust.Shared.Serialization.Manager.Attributes;
13-
using Robust.Shared.ViewVariables;
148
using static Robust.Client.UserInterface.Controls.BoxContainer;
159

1610
namespace Content.Client.Weapons.Ranged.Barrels.Components
1711
{
1812
[RegisterComponent]
1913
[NetworkedComponent()]
20-
public class ClientBatteryBarrelComponent : Component, IItemStatus
14+
public sealed class ClientBatteryBarrelComponent : Component, IItemStatus
2115
{
22-
private StatusControl? _statusControl;
23-
24-
/// <summary>
25-
/// Count of bullets in the magazine.
26-
/// </summary>
27-
/// <remarks>
28-
/// Null if no magazine is inserted.
29-
/// </remarks>
30-
[ViewVariables]
31-
public (int count, int max)? MagazineCount { get; private set; }
32-
33-
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
34-
{
35-
base.HandleComponentState(curState, nextState);
36-
37-
if (curState is not BatteryBarrelComponentState cast)
38-
return;
39-
40-
MagazineCount = cast.Magazine;
41-
_statusControl?.Update();
42-
}
16+
public StatusControl? ItemStatus;
4317

4418
public Control MakeControl()
4519
{
46-
_statusControl = new StatusControl(this);
47-
_statusControl.Update();
48-
return _statusControl;
20+
ItemStatus = new StatusControl(this);
21+
22+
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out AppearanceComponent appearance))
23+
ItemStatus.Update(appearance);
24+
25+
return ItemStatus;
4926
}
5027

5128
public void DestroyControl(Control control)
5229
{
53-
if (_statusControl == control)
30+
if (ItemStatus == control)
5431
{
55-
_statusControl = null;
32+
ItemStatus = null;
5633
}
5734
}
5835

59-
private sealed class StatusControl : Control
36+
public sealed class StatusControl : Control
6037
{
6138
private readonly ClientBatteryBarrelComponent _parent;
6239
private readonly BoxContainer _bulletsList;
@@ -104,18 +81,19 @@ public StatusControl(ClientBatteryBarrelComponent parent)
10481
});
10582
}
10683

107-
public void Update()
84+
public void Update(AppearanceComponent appearance)
10885
{
10986
_bulletsList.RemoveAllChildren();
11087

111-
if (_parent.MagazineCount == null)
88+
if (!appearance.TryGetData(MagazineBarrelVisuals.MagLoaded, out bool loaded) || !loaded)
11289
{
11390
_noBatteryLabel.Visible = true;
11491
_ammoCount.Visible = false;
11592
return;
11693
}
11794

118-
var (count, capacity) = _parent.MagazineCount.Value;
95+
appearance.TryGetData(AmmoVisuals.AmmoCount, out int count);
96+
appearance.TryGetData(AmmoVisuals.AmmoMax, out int capacity);
11997

12098
_noBatteryLabel.Visible = false;
12199
_ammoCount.Visible = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Content.Client.Weapons.Ranged.Barrels.Components;
2+
using Robust.Client.GameObjects;
3+
4+
namespace Content.Client.Weapons.Ranged.Barrels.EntitySystems;
5+
6+
public sealed class ClientBatteryBarrelSystem : EntitySystem
7+
{
8+
public override void Initialize()
9+
{
10+
base.Initialize();
11+
12+
SubscribeLocalEvent<ClientBatteryBarrelComponent, AppearanceChangeEvent>(OnAppearanceChange);
13+
}
14+
15+
private void OnAppearanceChange(EntityUid uid, ClientBatteryBarrelComponent component, ref AppearanceChangeEvent args)
16+
{
17+
component.ItemStatus?.Update(args.Component);
18+
}
19+
}

Content.Server/Weapon/Ranged/GunSystem.Battery.cs

-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using System;
2-
using Content.Server.PowerCell;
31
using Content.Server.Projectiles.Components;
42
using Content.Server.Weapon.Ranged.Barrels.Components;
53
using Content.Shared.PowerCell.Components;
64
using Content.Shared.Weapons.Ranged.Barrels.Components;
75
using Robust.Shared.Containers;
8-
using Robust.Shared.GameObjects;
9-
using Robust.Shared.GameStates;
106
using Robust.Shared.Map;
117

128
namespace Content.Server.Weapon.Ranged;
@@ -28,15 +24,6 @@ private void OnBatteryMapInit(EntityUid uid, BatteryBarrelComponent component, M
2824
UpdateBatteryAppearance(component);
2925
}
3026

31-
private void OnBatteryGetState(EntityUid uid, BatteryBarrelComponent component, ref ComponentGetState args)
32-
{
33-
(int, int)? count = (component.ShotsLeft, component.Capacity);
34-
35-
args.State = new BatteryBarrelComponentState(
36-
component.FireRateSelector,
37-
count);
38-
}
39-
4027
private void OnCellSlotUpdated(EntityUid uid, BatteryBarrelComponent component, PowerCellChangedEvent args)
4128
{
4229
UpdateBatteryAppearance(component);

Content.Server/Weapon/Ranged/GunSystem.cs

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public override void Initialize()
8686
// (All of these would be comp references so max you only ever have 2 components on the gun).
8787
SubscribeLocalEvent<BatteryBarrelComponent, ComponentInit>(OnBatteryInit);
8888
SubscribeLocalEvent<BatteryBarrelComponent, MapInitEvent>(OnBatteryMapInit);
89-
SubscribeLocalEvent<BatteryBarrelComponent, ComponentGetState>(OnBatteryGetState);
9089
SubscribeLocalEvent<BatteryBarrelComponent, PowerCellChangedEvent>(OnCellSlotUpdated);
9190

9291
SubscribeLocalEvent<BoltActionBarrelComponent, ComponentInit>(OnBoltInit);

Content.Shared/Weapons/Ranged/Barrels/Components/SharedBatteryBarrelComponent.cs

-22
This file was deleted.

0 commit comments

Comments
 (0)