Update battery-gun item status when charge changes (#6579)
Co-authored-by: mirrorcult <lunarautomaton6@gmail.com>
This commit is contained in:
@@ -1,62 +1,39 @@
|
|||||||
using System;
|
|
||||||
using Content.Client.Items.Components;
|
using Content.Client.Items.Components;
|
||||||
using Content.Client.Stylesheets;
|
using Content.Client.Stylesheets;
|
||||||
using Content.Shared.Containers.ItemSlots;
|
|
||||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||||
using Robust.Client.Graphics;
|
using Robust.Client.Graphics;
|
||||||
using Robust.Client.UserInterface;
|
using Robust.Client.UserInterface;
|
||||||
using Robust.Client.UserInterface.Controls;
|
using Robust.Client.UserInterface.Controls;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.GameStates;
|
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;
|
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
||||||
|
|
||||||
namespace Content.Client.Weapons.Ranged.Barrels.Components
|
namespace Content.Client.Weapons.Ranged.Barrels.Components
|
||||||
{
|
{
|
||||||
[RegisterComponent]
|
[RegisterComponent]
|
||||||
[NetworkedComponent()]
|
[NetworkedComponent()]
|
||||||
public class ClientBatteryBarrelComponent : Component, IItemStatus
|
public sealed class ClientBatteryBarrelComponent : Component, IItemStatus
|
||||||
{
|
{
|
||||||
private StatusControl? _statusControl;
|
public StatusControl? ItemStatus;
|
||||||
|
|
||||||
/// <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 Control MakeControl()
|
public Control MakeControl()
|
||||||
{
|
{
|
||||||
_statusControl = new StatusControl(this);
|
ItemStatus = new StatusControl(this);
|
||||||
_statusControl.Update();
|
|
||||||
return _statusControl;
|
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out AppearanceComponent appearance))
|
||||||
|
ItemStatus.Update(appearance);
|
||||||
|
|
||||||
|
return ItemStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DestroyControl(Control control)
|
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 ClientBatteryBarrelComponent _parent;
|
||||||
private readonly BoxContainer _bulletsList;
|
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();
|
_bulletsList.RemoveAllChildren();
|
||||||
|
|
||||||
if (_parent.MagazineCount == null)
|
if (!appearance.TryGetData(MagazineBarrelVisuals.MagLoaded, out bool loaded) || !loaded)
|
||||||
{
|
{
|
||||||
_noBatteryLabel.Visible = true;
|
_noBatteryLabel.Visible = true;
|
||||||
_ammoCount.Visible = false;
|
_ammoCount.Visible = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var (count, capacity) = _parent.MagazineCount.Value;
|
appearance.TryGetData(AmmoVisuals.AmmoCount, out int count);
|
||||||
|
appearance.TryGetData(AmmoVisuals.AmmoMax, out int capacity);
|
||||||
|
|
||||||
_noBatteryLabel.Visible = false;
|
_noBatteryLabel.Visible = false;
|
||||||
_ammoCount.Visible = true;
|
_ammoCount.Visible = true;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
using System;
|
|
||||||
using Content.Server.PowerCell;
|
|
||||||
using Content.Server.Projectiles.Components;
|
using Content.Server.Projectiles.Components;
|
||||||
using Content.Server.Weapon.Ranged.Barrels.Components;
|
using Content.Server.Weapon.Ranged.Barrels.Components;
|
||||||
using Content.Shared.PowerCell.Components;
|
using Content.Shared.PowerCell.Components;
|
||||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||||
using Robust.Shared.Containers;
|
using Robust.Shared.Containers;
|
||||||
using Robust.Shared.GameObjects;
|
|
||||||
using Robust.Shared.GameStates;
|
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
|
|
||||||
namespace Content.Server.Weapon.Ranged;
|
namespace Content.Server.Weapon.Ranged;
|
||||||
@@ -28,15 +24,6 @@ public sealed partial class GunSystem
|
|||||||
UpdateBatteryAppearance(component);
|
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)
|
private void OnCellSlotUpdated(EntityUid uid, BatteryBarrelComponent component, PowerCellChangedEvent args)
|
||||||
{
|
{
|
||||||
UpdateBatteryAppearance(component);
|
UpdateBatteryAppearance(component);
|
||||||
|
|||||||
@@ -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).
|
// (All of these would be comp references so max you only ever have 2 components on the gun).
|
||||||
SubscribeLocalEvent<BatteryBarrelComponent, ComponentInit>(OnBatteryInit);
|
SubscribeLocalEvent<BatteryBarrelComponent, ComponentInit>(OnBatteryInit);
|
||||||
SubscribeLocalEvent<BatteryBarrelComponent, MapInitEvent>(OnBatteryMapInit);
|
SubscribeLocalEvent<BatteryBarrelComponent, MapInitEvent>(OnBatteryMapInit);
|
||||||
SubscribeLocalEvent<BatteryBarrelComponent, ComponentGetState>(OnBatteryGetState);
|
|
||||||
SubscribeLocalEvent<BatteryBarrelComponent, PowerCellChangedEvent>(OnCellSlotUpdated);
|
SubscribeLocalEvent<BatteryBarrelComponent, PowerCellChangedEvent>(OnCellSlotUpdated);
|
||||||
|
|
||||||
SubscribeLocalEvent<BoltActionBarrelComponent, ComponentInit>(OnBoltInit);
|
SubscribeLocalEvent<BoltActionBarrelComponent, ComponentInit>(OnBoltInit);
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user