Optimise flashlights (#2468)

Flashlights were calling dirty EVERY TICK so this just means they only call it when absoluetely necessary which saves a lot.

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2020-11-01 07:56:46 +11:00
committed by GitHub
parent 87cb22838b
commit 2e65b1e5fa
6 changed files with 79 additions and 57 deletions

View File

@@ -14,10 +14,9 @@ namespace Content.Client.GameObjects.Components
[RegisterComponent]
public sealed class HandheldLightComponent : SharedHandheldLightComponent, IItemStatus
{
private bool _hasCell;
[ViewVariables] protected override bool HasCell => _level != null;
[ViewVariables] public float? Charge { get; private set; }
[ViewVariables] protected override bool HasCell => _hasCell;
private byte? _level;
public Control MakeControl()
{
@@ -29,8 +28,7 @@ namespace Content.Client.GameObjects.Components
if (!(curState is HandheldLightComponentState cast))
return;
Charge = cast.Charge;
_hasCell = cast.HasCell;
_level = cast.Charge;
}
private sealed class StatusControl : Control
@@ -38,7 +36,7 @@ namespace Content.Client.GameObjects.Components
private const float TimerCycle = 1;
private readonly HandheldLightComponent _parent;
private readonly PanelContainer[] _sections = new PanelContainer[5];
private readonly PanelContainer[] _sections = new PanelContainer[StatusLevels - 1];
private float _timer;
@@ -76,22 +74,18 @@ namespace Content.Client.GameObjects.Components
{
base.Update(args);
if (!_parent.HasCell)
return;
_timer += args.DeltaSeconds;
_timer %= TimerCycle;
var charge = _parent.Charge ?? 0;
var level = _parent._level;
int level;
if (MathHelper.CloseTo(charge, 0))
for (var i = 0; i < _sections.Length; i++)
{
level = 0;
}
else
if (i == 0)
{
level = ContentHelpers.RoundToNearestLevels(charge, 1.0, 6) + 1;
}
if (level == 0)
{
_sections[0].PanelOverride = _styleBoxUnlit;
@@ -106,10 +100,11 @@ namespace Content.Client.GameObjects.Components
_sections[0].PanelOverride = _styleBoxLit;
}
_sections[1].PanelOverride = level >= 3 ? _styleBoxLit : _styleBoxUnlit;
_sections[2].PanelOverride = level >= 4 ? _styleBoxLit : _styleBoxUnlit;
_sections[3].PanelOverride = level >= 5 ? _styleBoxLit : _styleBoxUnlit;
_sections[4].PanelOverride = level >= 6 ? _styleBoxLit : _styleBoxUnlit;
continue;
}
_sections[i].PanelOverride = level >= i + 2 ? _styleBoxLit : _styleBoxUnlit;
}
}
}
}

View File

@@ -34,10 +34,10 @@ namespace Content.Client.GameObjects.Components.Power
base.OnChangeData(component);
var sprite = component.Owner.GetComponent<ISpriteComponent>();
if (component.TryGetData(PowerCellVisuals.ChargeLevel, out float fraction))
if (component.TryGetData(PowerCellVisuals.ChargeLevel, out byte level))
{
int level = ContentHelpers.RoundToNearestLevels(fraction, 1, 4) * 25;
sprite.LayerSetState(Layers.Charge, $"{_prefix}_{level}");
var adjustedLevel = level * 25;
sprite.LayerSetState(Layers.Charge, $"{_prefix}_{adjustedLevel}");
}
}

View File

@@ -7,12 +7,14 @@ using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -41,6 +43,11 @@ namespace Content.Server.GameObjects.Components.Interactable
[ViewVariables(VVAccess.ReadWrite)] public string? TurnOnFailSound;
[ViewVariables(VVAccess.ReadWrite)] public string? TurnOffSound;
/// <summary>
/// Client-side ItemStatus level
/// </summary>
private byte? _lastLevel;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
@@ -190,24 +197,34 @@ namespace Content.Server.GameObjects.Components.Interactable
}
if (Activated && !Cell.TryUseCharge(Wattage * frameTime)) TurnOff(false);
var level = GetLevel();
if (level != _lastLevel)
{
_lastLevel = level;
Dirty();
}
}
// Curently every single flashlight has the same number of levels for status and that's all it uses the charge for
// Thus we'll just check if the level changes.
private byte? GetLevel()
{
if (Cell == null)
return null;
var currentCharge = Cell.CurrentCharge;
if (MathHelper.CloseTo(currentCharge, 0) || Wattage > currentCharge)
return 0;
return (byte?) ContentHelpers.RoundToNearestLevels(currentCharge / Cell.MaxCharge * 255, 255, StatusLevels);
}
public override ComponentState GetComponentState()
{
if (Cell == null)
{
return new HandheldLightComponentState(null, false);
}
if (Wattage > Cell.CurrentCharge)
{
// Practically zero.
// This is so the item status works correctly.
return new HandheldLightComponentState(0, HasCell);
}
return new HandheldLightComponentState(Cell.CurrentCharge / Cell.MaxCharge, HasCell);
return new HandheldLightComponentState(GetLevel());
}
}
}

View File

@@ -1,5 +1,6 @@
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Utility;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
@@ -45,10 +46,15 @@ namespace Content.Server.GameObjects.Components.Power
{
if (Owner.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(PowerCellVisuals.ChargeLevel, CurrentCharge / MaxCharge);
appearance.SetData(PowerCellVisuals.ChargeLevel, GetLevel(CurrentCharge / MaxCharge));
}
}
private byte GetLevel(float fraction)
{
return (byte) ContentHelpers.RoundToNearestLevels(fraction, 1, SharedPowerCell.PowerCellVisualsLevels);
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if(inDetailsRange)

View File

@@ -3,6 +3,11 @@ using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Power
{
public static class SharedPowerCell
{
public const int PowerCellVisualsLevels = 4;
}
[Serializable, NetSerializable]
public enum PowerCellVisuals
{

View File

@@ -11,18 +11,17 @@ namespace Content.Shared.GameObjects.Components
protected abstract bool HasCell { get; }
protected const int StatusLevels = 6;
[Serializable, NetSerializable]
protected sealed class HandheldLightComponentState : ComponentState
{
public HandheldLightComponentState(float? charge, bool hasCell) : base(ContentNetIDs.HANDHELD_LIGHT)
public byte? Charge { get; }
public HandheldLightComponentState(byte? charge) : base(ContentNetIDs.HANDHELD_LIGHT)
{
Charge = charge;
HasCell = hasCell;
}
public float? Charge { get; }
public bool HasCell { get; }
}
}