Update battery-gun item status when charge changes (#6579)

Co-authored-by: mirrorcult <lunarautomaton6@gmail.com>
This commit is contained in:
Leon Friedrich
2022-02-16 19:57:16 +13:00
committed by GitHub
parent f88cbbac11
commit 4dfcacb86a
5 changed files with 34 additions and 73 deletions

View File

@@ -1,62 +1,39 @@
using System;
using Content.Client.Items.Components;
using Content.Client.Stylesheets;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Weapons.Ranged.Barrels.Components;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Weapons.Ranged.Barrels.Components
{
[RegisterComponent]
[NetworkedComponent()]
public class ClientBatteryBarrelComponent : Component, IItemStatus
public sealed class ClientBatteryBarrelComponent : Component, IItemStatus
{
private StatusControl? _statusControl;
/// <summary>
/// Count of bullets in the magazine.
/// </summary>
/// <remarks>
/// Null if no magazine is inserted.
/// </remarks>
[ViewVariables]
public (int count, int max)? MagazineCount { get; private set; }
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not BatteryBarrelComponentState cast)
return;
MagazineCount = cast.Magazine;
_statusControl?.Update();
}
public StatusControl? ItemStatus;
public Control MakeControl()
{
_statusControl = new StatusControl(this);
_statusControl.Update();
return _statusControl;
ItemStatus = new StatusControl(this);
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out AppearanceComponent appearance))
ItemStatus.Update(appearance);
return ItemStatus;
}
public void DestroyControl(Control control)
{
if (_statusControl == control)
if (ItemStatus == control)
{
_statusControl = null;
ItemStatus = null;
}
}
private sealed class StatusControl : Control
public sealed class StatusControl : Control
{
private readonly ClientBatteryBarrelComponent _parent;
private readonly BoxContainer _bulletsList;
@@ -104,18 +81,19 @@ namespace Content.Client.Weapons.Ranged.Barrels.Components
});
}
public void Update()
public void Update(AppearanceComponent appearance)
{
_bulletsList.RemoveAllChildren();
if (_parent.MagazineCount == null)
if (!appearance.TryGetData(MagazineBarrelVisuals.MagLoaded, out bool loaded) || !loaded)
{
_noBatteryLabel.Visible = true;
_ammoCount.Visible = false;
return;
}
var (count, capacity) = _parent.MagazineCount.Value;
appearance.TryGetData(AmmoVisuals.AmmoCount, out int count);
appearance.TryGetData(AmmoVisuals.AmmoMax, out int capacity);
_noBatteryLabel.Visible = false;
_ammoCount.Visible = true;

View File

@@ -0,0 +1,19 @@
using Content.Client.Weapons.Ranged.Barrels.Components;
using Robust.Client.GameObjects;
namespace Content.Client.Weapons.Ranged.Barrels.EntitySystems;
public sealed class ClientBatteryBarrelSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ClientBatteryBarrelComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private void OnAppearanceChange(EntityUid uid, ClientBatteryBarrelComponent component, ref AppearanceChangeEvent args)
{
component.ItemStatus?.Update(args.Component);
}
}

View File

@@ -1,12 +1,8 @@
using System;
using Content.Server.PowerCell;
using Content.Server.Projectiles.Components;
using Content.Server.Weapon.Ranged.Barrels.Components;
using Content.Shared.PowerCell.Components;
using Content.Shared.Weapons.Ranged.Barrels.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
namespace Content.Server.Weapon.Ranged;
@@ -28,15 +24,6 @@ public sealed partial class GunSystem
UpdateBatteryAppearance(component);
}
private void OnBatteryGetState(EntityUid uid, BatteryBarrelComponent component, ref ComponentGetState args)
{
(int, int)? count = (component.ShotsLeft, component.Capacity);
args.State = new BatteryBarrelComponentState(
component.FireRateSelector,
count);
}
private void OnCellSlotUpdated(EntityUid uid, BatteryBarrelComponent component, PowerCellChangedEvent args)
{
UpdateBatteryAppearance(component);

View File

@@ -86,7 +86,6 @@ public sealed partial class GunSystem : EntitySystem
// (All of these would be comp references so max you only ever have 2 components on the gun).
SubscribeLocalEvent<BatteryBarrelComponent, ComponentInit>(OnBatteryInit);
SubscribeLocalEvent<BatteryBarrelComponent, MapInitEvent>(OnBatteryMapInit);
SubscribeLocalEvent<BatteryBarrelComponent, ComponentGetState>(OnBatteryGetState);
SubscribeLocalEvent<BatteryBarrelComponent, PowerCellChangedEvent>(OnCellSlotUpdated);
SubscribeLocalEvent<BoltActionBarrelComponent, ComponentInit>(OnBoltInit);

View File

@@ -1,22 +0,0 @@
using System;
using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Weapons.Ranged.Barrels.Components
{
[Serializable, NetSerializable]
public class BatteryBarrelComponentState : ComponentState
{
public FireRateSelector FireRateSelector { get; }
public (int count, int max)? Magazine { get; }
public BatteryBarrelComponentState(
FireRateSelector fireRateSelector,
(int count, int max)? magazine)
{
FireRateSelector = fireRateSelector;
Magazine = magazine;
}
}
}