Change over solar panel breakage to use more construction graphs (#5395)

This commit is contained in:
20kdc
2021-11-26 09:17:21 +00:00
committed by GitHub
parent d4947bfb86
commit f0f913f5bc
4 changed files with 82 additions and 61 deletions

View File

@@ -1,8 +1,8 @@
using System;
using Content.Server.Power.Components;
using Content.Server.Solar.EntitySystems;
using Content.Shared.Acts;
using Robust.Server.GameObjects;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
@@ -16,67 +16,24 @@ namespace Content.Server.Solar.Components
/// It generates power from the sun based on coverage.
/// </summary>
[RegisterComponent]
public class SolarPanelComponent : Component, IBreakAct
[Friend(typeof(PowerSolarSystem))]
public class SolarPanelComponent : Component
{
public override string Name => "SolarPanel";
/// <summary>
/// Maximum supply output by this panel (coverage = 1)
/// </summary>
[DataField("maxsupply")]
private int _maxSupply = 1500;
[ViewVariables(VVAccess.ReadWrite)]
public int MaxSupply
{
get => _maxSupply;
set {
_maxSupply = value;
UpdateSupply();
}
}
[DataField("maxSupply")]
[ViewVariables]
public int MaxSupply = 1500;
/// <summary>
/// Current coverage of this panel (from 0 to 1).
/// This is updated by <see cref='PowerSolarSystem'/>.
/// </summary>
private float _coverage = 0;
[ViewVariables]
public float Coverage
{
get => _coverage;
set {
// This gets updated once-per-tick, so avoid updating it if truly unnecessary
if (_coverage != value) {
_coverage = value;
UpdateSupply();
}
}
}
/// <summary>
/// The game time (<see cref='IGameTiming'/>) of the next coverage update.
/// This may have a random offset applied.
/// This is used to reduce solar panel updates and stagger them to prevent lagspikes.
/// This should only be updated by the PowerSolarSystem but is viewable for debugging.
/// DO NOT WRITE WITHOUT CALLING UpdateSupply()!
/// </summary>
[ViewVariables]
public TimeSpan TimeOfNextCoverageUpdate = TimeSpan.MinValue;
private void UpdateSupply()
{
if (Owner.TryGetComponent<PowerSupplierComponent>(out var supplier))
{
supplier.MaxSupply = (int) (_maxSupply * _coverage);
}
}
public void OnBreak(BreakageEventArgs args)
{
if (!Owner.TryGetComponent<SpriteComponent>(out var sprite))
return;
sprite.LayerSetState(0, "broken");
MaxSupply = 0;
}
public float Coverage { get; set; } = 0;
}
}

View File

@@ -154,7 +154,6 @@ namespace Content.Server.Solar.EntitySystems
if (coverage > 0)
{
// Determine if the solar panel is occluded, and zero out coverage if so.
// FIXME: The "Opaque" collision group doesn't seem to work right now.
var ray = new CollisionRay(entity.Transform.WorldPosition, TowardsSun.ToWorldVec(), (int) CollisionGroup.Opaque);
var rayCastResults = _physicsSystem.IntersectRayWithPredicate(
entity.Transform.MapID,
@@ -167,9 +166,10 @@ namespace Content.Server.Solar.EntitySystems
// Total coverage calculated; apply it to the panel.
panel.Coverage = coverage;
UpdateSupply(panel.OwnerUid, panel);
}
private void UpdateSupply(
public void UpdateSupply(
EntityUid uid,
SolarPanelComponent? solar = null,
PowerSupplierComponent? supplier = null)