Re-organize all projects (#4166)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#nullable enable
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Solar.EntitySystems;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Solar;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Solar.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class SolarControlConsoleComponent : SharedSolarControlConsoleComponent, IActivate
|
||||
{
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
||||
|
||||
private PowerSolarSystem _powerSolarSystem = default!;
|
||||
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SolarControlConsoleUiKey.Key);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
|
||||
Owner.EnsureComponent<PowerReceiverComponent>();
|
||||
_powerSolarSystem = _entitySystemManager.GetEntitySystem<PowerSolarSystem>();
|
||||
}
|
||||
|
||||
public void UpdateUIState()
|
||||
{
|
||||
UserInterface?.SetState(new SolarControlConsoleBoundInterfaceState(_powerSolarSystem.TargetPanelRotation, _powerSolarSystem.TargetPanelVelocity, _powerSolarSystem.TotalPanelPower, _powerSolarSystem.TowardsSun));
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
switch (obj.Message)
|
||||
{
|
||||
case SolarControlConsoleAdjustMessage msg:
|
||||
if (double.IsFinite(msg.Rotation))
|
||||
{
|
||||
_powerSolarSystem.TargetPanelRotation = msg.Rotation.Reduced();
|
||||
}
|
||||
if (double.IsFinite(msg.AngularVelocity))
|
||||
{
|
||||
_powerSolarSystem.TargetPanelVelocity = msg.AngularVelocity.Reduced();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// always update the UI immediately before opening, just in case
|
||||
UpdateUIState();
|
||||
UserInterface?.Open(actor.PlayerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
92
Content.Server/Solar/Components/SolarPanelComponent.cs
Normal file
92
Content.Server/Solar/Components/SolarPanelComponent.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Solar.EntitySystems;
|
||||
using Content.Shared.Acts;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Solar.Components
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This is a solar panel.
|
||||
/// It generates power from the sun based on coverage.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class SolarPanelComponent : Component, IBreakAct
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public TimeSpan TimeOfNextCoverageUpdate = TimeSpan.MinValue;
|
||||
|
||||
private void UpdateSupply()
|
||||
{
|
||||
if (Owner.TryGetComponent<PowerSupplierComponent>(out var supplier))
|
||||
{
|
||||
supplier.SupplyRate = (int) (_maxSupply * _coverage);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Owner.EnsureComponentWarn<PowerSupplierComponent>();
|
||||
|
||||
UpdateSupply();
|
||||
}
|
||||
|
||||
public void OnBreak(BreakageEventArgs args)
|
||||
{
|
||||
if (!Owner.TryGetComponent<SpriteComponent>(out var sprite))
|
||||
return;
|
||||
|
||||
sprite.LayerSetState(0, "broken");
|
||||
MaxSupply = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user