Fix taser visuals not updating after charging. (#6065)

This commit is contained in:
Leon Friedrich
2022-01-10 01:48:58 +13:00
committed by GitHub
parent 259914b919
commit 5a64936ade
4 changed files with 21 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ using Content.Shared.Examine;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Rounding;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -20,6 +21,7 @@ public class PowerCellSystem : SharedPowerCellSystem
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly AdminLogSystem _logSystem = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
public override void Initialize()
{
@@ -48,6 +50,14 @@ public class PowerCellSystem : SharedPowerCellSystem
var frac = battery.CurrentCharge / battery.MaxCharge;
var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels);
appearance.SetData(PowerCellVisuals.ChargeLevel, level);
// If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such).
if (_containerSystem.TryGetContainingContainer(uid, out var container)
&& TryComp(container.Owner, out PowerCellSlotComponent? slot)
&& slot.CellSlot.Item == uid)
{
RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false), false);
}
}
private void Explode(EntityUid uid, BatteryComponent? battery = null)

View File

@@ -99,7 +99,12 @@ namespace Content.Server.Stunnable
private void OnPowerCellChanged(EntityUid uid, StunbatonComponent comp, PowerCellChangedEvent args)
{
if (args.Ejected)
if (!comp.Activated)
return;
if (args.Ejected
|| !_cellSystem.TryGetBatteryFromSlot(comp.Owner, out var battery)
|| battery.CurrentCharge < comp.EnergyPerUse)
{
TurnOff(comp);
}
@@ -175,9 +180,6 @@ namespace Content.Server.Stunnable
return;
var playerFilter = Filter.Pvs(comp.Owner);
if (!EntityManager.TryGetComponent<PowerCellSlotComponent?>(comp.Owner, out var slot))
return;
if (!_cellSystem.TryGetBatteryFromSlot(comp.Owner, out var battery))
{
SoundSystem.Play(playerFilter, comp.TurnOnFailSound.GetSound(), comp.Owner, AudioHelpers.WithVariation(0.25f));

View File

@@ -163,8 +163,8 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
throw new InvalidOperationException("Ammo doesn't have hitscan or projectile?");
}
Dirty();
UpdateAppearance();
// capacitor.UseCharge() triggers a PowerCellChangedEvent which will cause appearance to be updated.
// So let's not double-call UpdateAppearance() here.
return entity.Value;
}
}

View File

@@ -64,6 +64,9 @@ public sealed class PowerCellSlotComponent : Component
public bool FitsInCharger = true;
}
/// <summary>
/// Raised directed at an entity with a power cell slot when the power cell inside has its charge updated or is ejected/inserted.
/// </summary>
public class PowerCellChangedEvent : EntityEventArgs
{
public readonly bool Ejected;