Powernet Recalculation simplification (#1427)

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2020-07-26 04:14:03 -06:00
committed by GitHub
parent 96ec60adab
commit fbbe43fff8
7 changed files with 80 additions and 87 deletions

View File

@@ -0,0 +1,38 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using System.Collections.Generic;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
/// <summary>
/// Maintains a set of <see cref="IPowerNet"/>s that need to be updated with <see cref="IPowerNet.UpdateConsumerReceivedPower"/>.
/// Defers updating to reduce recalculations when a group is altered multiple times in a frame.
/// </summary>
public interface IPowerNetManager
{
/// <summary>
/// Queue up an <see cref="IPowerNet"/> to be updated.
/// </summary>
void AddDirtyPowerNet(IPowerNet powerNet);
void Update(float frameTime);
}
public class PowerNetManager : IPowerNetManager
{
private readonly HashSet<IPowerNet> _dirtyPowerNets = new HashSet<IPowerNet>();
public void AddDirtyPowerNet(IPowerNet powerNet)
{
_dirtyPowerNets.Add(powerNet);
}
public void Update(float frameTime)
{
foreach (var powerNet in _dirtyPowerNets)
{
powerNet.UpdateConsumerReceivedPower();
}
_dirtyPowerNets.Clear();
}
}
}