Gravity generator rewrite (#4828)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Pieter-Jan Briers
2021-11-02 01:12:55 +01:00
committed by GitHub
parent 0e33b246db
commit 059fa9ae48
17 changed files with 739 additions and 361 deletions

View File

@@ -63,10 +63,31 @@ namespace Content.Client.Gravity
}
}
if (component.TryGetData(GravityGeneratorVisuals.CoreVisible, out bool visible))
if (component.TryGetData(GravityGeneratorVisuals.Charge, out float charge))
{
var layer = sprite.LayerMapGet(GravityGeneratorVisualLayers.Core);
sprite.LayerSetVisible(layer, visible);
switch (charge)
{
case < 0.2f:
sprite.LayerSetVisible(layer, false);
break;
case >= 0.2f and < 0.4f:
sprite.LayerSetVisible(layer, true);
sprite.LayerSetState(layer, "startup");
break;
case >= 0.4f and < 0.6f:
sprite.LayerSetVisible(layer, true);
sprite.LayerSetState(layer, "idle");
break;
case >= 0.6f and < 0.8f:
sprite.LayerSetVisible(layer, true);
sprite.LayerSetState(layer, "activating");
break;
default:
sprite.LayerSetVisible(layer, true);
sprite.LayerSetState(layer, "activated");
break;
}
}
}

View File

@@ -10,28 +10,25 @@ namespace Content.Client.Gravity.UI
{
private GravityGeneratorWindow? _window;
public bool IsOn;
public GravityGeneratorBoundUserInterface(ClientUserInterfaceComponent owner, object uiKey) : base (owner, uiKey)
{
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
}
protected override void Open()
{
base.Open();
IsOn = false;
_window = new GravityGeneratorWindow(this);
_window = new GravityGeneratorWindow(this, Owner);
/*
_window.Switch.OnPressed += _ =>
{
SendMessage(new SharedGravityGeneratorComponent.SwitchGeneratorMessage(!IsOn));
SendMessage(new SharedGravityGeneratorComponent.GeneratorStatusRequestMessage());
};
*/
_window.OpenCentered();
_window.OnClose += Close;
}
protected override void UpdateState(BoundUserInterfaceState state)
@@ -39,8 +36,7 @@ namespace Content.Client.Gravity.UI
base.UpdateState(state);
var castState = (SharedGravityGeneratorComponent.GeneratorState) state;
IsOn = castState.On;
_window?.UpdateButton();
_window?.UpdateState(castState);
}
protected override void Dispose(bool disposing)
@@ -50,5 +46,10 @@ namespace Content.Client.Gravity.UI
_window?.Dispose();
}
public void SetPowerSwitch(bool on)
{
SendMessage(new SharedGravityGeneratorComponent.SwitchGeneratorMessage(on));
}
}
}

View File

@@ -1,14 +1,35 @@
<SS14Window xmlns="https://spacestation14.io"
Title="{Loc 'gravity-generator-window-title'}"
MinSize="270 130"
SetSize="270 130">
<BoxContainer Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<RichTextLabel Name="Status" />
<ui:FancyWindow xmlns="https://spacestation14.io"
xmlns:ui="clr-namespace:Content.Client.UserInterface"
Title="{Loc 'gravity-generator-window-title'}"
MinSize="270 130"
SetSize="360 180">
<BoxContainer Margin="4 0" Orientation="Horizontal">
<BoxContainer Orientation="Vertical" HorizontalExpand="True">
<GridContainer Margin="2 0 0 0" Columns="2">
<!-- Power -->
<Label Text="{Loc 'gravity-generator-window-power'}" HorizontalExpand="True" StyleClasses="StatusFieldTitle" />
<BoxContainer Orientation="Horizontal" MinWidth="120">
<Button Name="OnButton" Text="{Loc 'gravity-generator-window-power-on'}" StyleClasses="OpenRight" />
<Button Name="OffButton" Text="{Loc 'gravity-generator-window-power-off'}" StyleClasses="OpenLeft" />
</BoxContainer>
<Control /> <!-- Empty control to act as a spacer in the grid. -->
<Label Name="PowerLabel" Text="0 / 0 W" />
<!-- Status -->
<Label Text="{Loc 'gravity-generator-window-status'}" StyleClasses="StatusFieldTitle" />
<Label Name="StatusLabel" Text="{Loc 'gravity-generator-window-status-fully-charged'}" />
<!-- ETA -->
<Label Text="{Loc 'gravity-generator-window-eta'}" StyleClasses="StatusFieldTitle" />
<Label Name="EtaLabel" Text="N/A" />
<!-- Charge -->
<Label Text="{Loc 'gravity-generator-window-charge'}" StyleClasses="StatusFieldTitle" />
<ProgressBar Name="ChargeBar" MaxValue="255">
<Label Name="ChargeText" Margin="4 0" Text="0 %" />
</ProgressBar>
</GridContainer>
</BoxContainer>
<Button Name="Switch"
Access="Public"
TextAlign="Center"
MinHeight="60" />
<PanelContainer Margin="12 0 0 0" StyleClasses="Inset" VerticalAlignment="Center">
<SpriteView Name="EntityView" SetSize="96 96" OverrideDirection="South" />
</PanelContainer>
</BoxContainer>
</SS14Window>
</ui:FancyWindow>

View File

@@ -1,39 +1,80 @@
using Content.Client.Message;
using System;
using Content.Client.Message;
using Content.Client.UserInterface;
using Content.Shared.Gravity;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
namespace Content.Client.Gravity.UI
{
[GenerateTypedNameReferences]
public partial class GravityGeneratorWindow : SS14Window
public partial class GravityGeneratorWindow : FancyWindow
{
private readonly ButtonGroup _buttonGroup = new();
private readonly GravityGeneratorBoundUserInterface _owner;
public GravityGeneratorWindow(GravityGeneratorBoundUserInterface ui)
public GravityGeneratorWindow(GravityGeneratorBoundUserInterface ui, ClientUserInterfaceComponent component)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_owner = ui;
UpdateButton();
OnButton.Group = _buttonGroup;
OffButton.Group = _buttonGroup;
OnButton.OnPressed += _ => _owner.SetPowerSwitch(true);
OffButton.OnPressed += _ => _owner.SetPowerSwitch(false);
EntityView.Sprite = component.Owner.GetComponent<SpriteComponent>();
}
public void UpdateButton()
public void UpdateState(SharedGravityGeneratorComponent.GeneratorState state)
{
string status = Loc.GetString(_owner.IsOn
? "gravity-generator-window-is-on"
: "gravity-generator-window-is-off");
if (state.On)
OnButton.Pressed = true;
else
OffButton.Pressed = true;
Status.SetMarkup(Loc.GetString("gravity-generator-window-status-label", ("status", status)));
PowerLabel.Text = Loc.GetString(
"gravity-generator-window-power-label",
("draw", state.PowerDraw),
("max", state.PowerDrawMax));
Switch.Text = Loc.GetString(_owner.IsOn
? "gravity-generator-window-turn-off-button"
: "gravity-generator-window-turn-on-button");
PowerLabel.SetOnlyStyleClass(MathHelper.CloseTo(state.PowerDraw, state.PowerDrawMax) ? "Good" : "Caution");
ChargeBar.Value = state.Charge;
ChargeText.Text = (state.Charge / 255f).ToString("P0");
StatusLabel.Text = Loc.GetString(state.PowerStatus switch
{
GravityGeneratorPowerStatus.Off => "gravity-generator-window-status-off",
GravityGeneratorPowerStatus.Discharging => "gravity-generator-window-status-discharging",
GravityGeneratorPowerStatus.Charging => "gravity-generator-window-status-charging",
GravityGeneratorPowerStatus.FullyCharged => "gravity-generator-window-status-fully-charged",
_ => throw new ArgumentOutOfRangeException()
});
StatusLabel.SetOnlyStyleClass(state.PowerStatus switch
{
GravityGeneratorPowerStatus.Off => "Danger",
GravityGeneratorPowerStatus.Discharging => "Caution",
GravityGeneratorPowerStatus.Charging => "Caution",
GravityGeneratorPowerStatus.FullyCharged => "Good",
_ => throw new ArgumentOutOfRangeException()
});
EtaLabel.Text = state.EtaSeconds >= 0
? Loc.GetString("gravity-generator-window-eta-value", ("left", TimeSpan.FromSeconds(state.EtaSeconds)))
: Loc.GetString("gravity-generator-window-eta-none");
EtaLabel.SetOnlyStyleClass(state.EtaSeconds >= 0 ? "Caution" : "Disabled");
}
}
}

View File

@@ -54,6 +54,10 @@ namespace Content.Client.Stylesheets
public const string StyleClassPopupMessage = "PopupMessage";
public static readonly Color NanoGold = Color.FromHex("#A88B5E");
public static readonly Color GoodGreenFore = Color.FromHex("#31843E");
public static readonly Color ConcerningOrangeFore = Color.FromHex("#A5762F");
public static readonly Color DangerousRedFore = Color.FromHex("#BB3232");
public static readonly Color DisabledFore = Color.FromHex("#5A5A5A");
public static readonly Color ButtonColorDefault = Color.FromHex("#464966");
public static readonly Color ButtonColorDefaultRed = Color.FromHex("#D43B3B");
@@ -395,6 +399,15 @@ namespace Content.Client.Stylesheets
var sliderFillRed = new StyleBoxTexture(sliderFillBox) {Modulate = Color.Red};
var sliderFillBlue = new StyleBoxTexture(sliderFillBox) {Modulate = Color.Blue};
var boxFont13 = resCache.GetFont("/Fonts/Boxfont-round/Boxfont Round.ttf", 13);
var insetBack = new StyleBoxTexture
{
Texture = buttonTex,
Modulate = Color.FromHex("#202023"),
};
insetBack.SetPatchMargin(StyleBox.Margin.All, 10);
Stylesheet = new Stylesheet(BaseRules.Concat(new[]
{
// Window title.
@@ -1124,6 +1137,40 @@ namespace Content.Client.Stylesheets
.Prop(PanelContainer.StylePropertyPanel, BaseAngleRect)
.Prop(Control.StylePropertyModulateSelf, Color.FromHex("#25252A")),
Element<PanelContainer>().Class(ClassLowDivider)
.Prop(PanelContainer.StylePropertyPanel, new StyleBoxFlat
{
BackgroundColor = Color.FromHex("#444"),
ContentMarginLeftOverride = 2,
ContentMarginBottomOverride = 2
}),
Element<Label>().Class("FancyWindowTitle")
.Prop("font", boxFont13)
.Prop("font-color", NanoGold),
Element<PanelContainer>().Class("WindowHeadingBackground")
.Prop("panel", new StyleBoxTexture(BaseButtonOpenLeft) { Padding = default })
.Prop(Control.StylePropertyModulateSelf, Color.FromHex("#1F1F23")),
Element<PanelContainer>().Class("Inset")
.Prop("panel", insetBack),
Element<Label>().Class("StatusFieldTitle")
.Prop("font-color", NanoGold),
Element<Label>().Class("Good")
.Prop("font-color", GoodGreenFore),
Element<Label>().Class("Caution")
.Prop("font-color", ConcerningOrangeFore),
Element<Label>().Class("Danger")
.Prop("font-color", DangerousRedFore),
Element<Label>().Class("Disabled")
.Prop("font-color", DisabledFore),
}).ToList());
}
}

View File

@@ -0,0 +1,20 @@
<ui:FancyWindow xmlns="https://spacestation14.io"
xmlns:ui="clr-namespace:Content.Client.UserInterface"
MouseFilter="Stop"
MinWidth="200" MinHeight="150">
<PanelContainer StyleClasses="AngleRect" />
<BoxContainer Orientation="Vertical">
<Control>
<PanelContainer StyleClasses="WindowHeadingBackground" />
<BoxContainer Margin="4 2 8 0" Orientation="Horizontal">
<Label Name="WindowTitle"
HorizontalExpand="True" VAlign="Center" StyleClasses="FancyWindowTitle" />
<TextureButton Name="CloseButton" StyleClasses="windowCloseButton"
VerticalAlignment="Center" />
</BoxContainer>
</Control>
<PanelContainer StyleClasses="LowDivider" />
<Control Name="ContentsContainer" Margin="0 2" RectClipContent="True" VerticalExpand="true" />
</BoxContainer>
</ui:FancyWindow>

View File

@@ -0,0 +1,30 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Maths;
namespace Content.Client.UserInterface
{
[GenerateTypedNameReferences]
public partial class FancyWindow : BaseWindow
{
public FancyWindow()
{
RobustXamlLoader.Load(this);
CloseButton.OnPressed += _ => Close();
XamlChildren = ContentsContainer.Children;
}
public string? Title
{
get => WindowTitle.Text;
set => WindowTitle.Text = value;
}
protected override DragMode GetDragModeFor(Vector2 relativeMousePos)
{
return DragMode.Move;
}
}
}

View File

@@ -22,6 +22,16 @@ namespace Content.IntegrationTests.Tests.Gravity
id: HumanDummy
components:
- type: Alerts
- type: entity
name: GravityGeneratorDummy
id: GravityGeneratorDummy
components:
- type: GravityGenerator
chargeRate: 1000000000 # Set this really high so it discharges in a single tick.
activePower: 500
- type: ApcPowerReceiver
needsPower: false
- type: UserInterface
";
[Test]
public async Task WeightlessStatusTest()
@@ -56,14 +66,12 @@ namespace Content.IntegrationTests.Tests.Gravity
// Let WeightlessSystem and GravitySystem tick
await server.WaitRunTicks(1);
GravityGeneratorComponent gravityGenerator = null;
await server.WaitAssertion(() =>
{
// No gravity without a gravity generator
Assert.True(alerts.IsShowingAlert(AlertType.Weightless));
gravityGenerator = human.EnsureComponent<GravityGeneratorComponent>();
entityManager.SpawnEntity("GravityGeneratorDummy", human.Transform.Coordinates);
});
// Let WeightlessSystem and GravitySystem tick
@@ -73,17 +81,20 @@ namespace Content.IntegrationTests.Tests.Gravity
{
Assert.False(alerts.IsShowingAlert(AlertType.Weightless));
// TODO: Re-add gravity generator breaking when Vera is done with construction stuff.
/*
// Disable the gravity generator
var args = new BreakageEventArgs {Owner = human};
gravityGenerator.OnBreak(args);
// gravityGenerator.OnBreak(args);
*/
});
await server.WaitRunTicks(1);
/*await server.WaitRunTicks(1);
await server.WaitAssertion(() =>
{
Assert.True(alerts.IsShowingAlert(AlertType.Weightless));
});
});*/
}
}
}

View File

@@ -22,7 +22,10 @@ namespace Content.IntegrationTests.Tests
id: GravityGeneratorDummy
components:
- type: GravityGenerator
chargeRate: 1000000000 # Set this really high so it discharges in a single tick.
activePower: 500
- type: ApcPowerReceiver
- type: UserInterface
";
[Test]
public async Task Test()
@@ -49,9 +52,8 @@ namespace Content.IntegrationTests.Tests
generator = entityMan.SpawnEntity("GravityGeneratorDummy", grid2.ToCoordinates());
Assert.That(generator.HasComponent<GravityGeneratorComponent>());
Assert.That(generator.HasComponent<ApcPowerReceiverComponent>());
var generatorComponent = generator.GetComponent<GravityGeneratorComponent>();
var powerComponent = generator.GetComponent<ApcPowerReceiverComponent>();
Assert.That(generatorComponent.Status, Is.EqualTo(GravityGeneratorStatus.Unpowered));
powerComponent.NeedsPower = false;
});
server.RunTicks(1);
@@ -59,8 +61,9 @@ namespace Content.IntegrationTests.Tests
server.Assert(() =>
{
var generatorComponent = generator.GetComponent<GravityGeneratorComponent>();
var powerComponent = generator.GetComponent<ApcPowerReceiverComponent>();
Assert.That(generatorComponent.Status, Is.EqualTo(GravityGeneratorStatus.On));
Assert.That(generatorComponent.GravityActive, Is.True);
var entityMan = IoCManager.Resolve<IEntityManager>();
var grid1Entity = entityMan.GetEntity(grid1.GridEntityId);
@@ -68,6 +71,22 @@ namespace Content.IntegrationTests.Tests
Assert.That(!grid1Entity.GetComponent<GravityComponent>().Enabled);
Assert.That(grid2Entity.GetComponent<GravityComponent>().Enabled);
// Re-enable needs power so it turns off again.
// Charge rate is ridiculously high so it finishes in one tick.
powerComponent.NeedsPower = true;
});
server.RunTicks(1);
server.Assert(() =>
{
var generatorComponent = generator.GetComponent<GravityGeneratorComponent>();
Assert.That(generatorComponent.GravityActive, Is.False);
var entityMan = IoCManager.Resolve<IEntityManager>();
var grid2Entity = entityMan.GetEntity(grid2.GridEntityId);
Assert.That(grid2Entity.GetComponent<GravityComponent>().Enabled, Is.False);
});
await server.WaitIdleAsync();

View File

@@ -0,0 +1,286 @@
using System;
using Content.Server.Audio;
using Content.Server.Power.Components;
using Content.Shared.Gravity;
using Content.Shared.Interaction;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.Gravity.EntitySystems
{
public sealed class GravityGeneratorSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly AmbientSoundSystem _ambientSoundSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly GravitySystem _gravitySystem = default!;
[Dependency] private readonly GravityShakeSystem _gravityShakeSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityGeneratorComponent, ComponentInit>(OnComponentInitialized);
SubscribeLocalEvent<GravityGeneratorComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<GravityGeneratorComponent, SharedGravityGeneratorComponent.SwitchGeneratorMessage>(
OnSwitchGenerator);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var (gravGen, powerReceiver) in EntityManager
.EntityQuery<GravityGeneratorComponent, ApcPowerReceiverComponent>())
{
if (!gravGen.Intact)
return;
// Calculate charge rate based on power state and such.
// Negative charge rate means discharging.
float chargeRate;
if (gravGen.SwitchedOn)
{
if (powerReceiver.Powered)
{
chargeRate = gravGen.ChargeRate;
}
else
{
// Scale discharge rate such that if we're at 25% active power we discharge at 75% rate.
var receiving = powerReceiver.PowerReceived;
var mainSystemPower = Math.Max(0, receiving - gravGen.IdlePowerUse);
var ratio = 1 - mainSystemPower / (gravGen.ActivePowerUse - gravGen.IdlePowerUse);
chargeRate = -(ratio * gravGen.ChargeRate);
}
}
else
{
chargeRate = -gravGen.ChargeRate;
}
var updateGravity = gravGen.NeedGravityUpdate;
var shakeGravity = false;
var lastCharge = gravGen.Charge;
gravGen.Charge = Math.Clamp(gravGen.Charge + frameTime * chargeRate, 0, 1);
if (chargeRate > 0)
{
// Charging.
if (MathHelper.CloseTo(gravGen.Charge, 1) && !gravGen.GravityActive)
{
shakeGravity = true;
updateGravity = true;
gravGen.GravityActive = true;
}
}
else
{
// Discharging
if (MathHelper.CloseTo(gravGen.Charge, 0) && gravGen.GravityActive)
{
shakeGravity = true;
updateGravity = true;
gravGen.GravityActive = false;
}
}
var updateUI = gravGen.NeedUIUpdate;
if (!MathHelper.CloseTo(lastCharge, gravGen.Charge))
{
UpdateState(gravGen, powerReceiver);
updateUI = true;
}
if (updateUI)
UpdateUI(gravGen, powerReceiver, chargeRate);
if (updateGravity)
{
UpdateGravityActive(gravGen, shakeGravity);
}
}
}
private void SetSwitchedOn(EntityUid uid, GravityGeneratorComponent component, bool on, ApcPowerReceiverComponent? powerReceiver = null)
{
if (!Resolve(uid, ref powerReceiver))
return;
component.SwitchedOn = on;
UpdatePowerState(component, powerReceiver);
component.NeedUIUpdate = true;
}
private static void UpdatePowerState(
GravityGeneratorComponent component,
ApcPowerReceiverComponent powerReceiver)
{
powerReceiver.Load = component.SwitchedOn ? component.ActivePowerUse : component.IdlePowerUse;
}
private void UpdateUI(
GravityGeneratorComponent component,
ApcPowerReceiverComponent powerReceiver,
float chargeRate)
{
if (!_uiSystem.IsUiOpen(component.Owner.Uid, SharedGravityGeneratorComponent.GravityGeneratorUiKey.Key))
return;
var chargeTarget = chargeRate < 0 ? 0 : 1;
short chargeEta;
var atTarget = false;
if (MathHelper.CloseTo(component.Charge, chargeTarget))
{
chargeEta = short.MinValue; // N/A
atTarget = true;
}
else
{
var diff = chargeTarget - component.Charge;
chargeEta = (short) Math.Abs(diff / chargeRate);
}
var status = chargeRate switch
{
> 0 when atTarget => GravityGeneratorPowerStatus.FullyCharged,
< 0 when atTarget => GravityGeneratorPowerStatus.Off,
> 0 => GravityGeneratorPowerStatus.Charging,
< 0 => GravityGeneratorPowerStatus.Discharging,
_ => throw new ArgumentOutOfRangeException()
};
var state = new SharedGravityGeneratorComponent.GeneratorState(
component.SwitchedOn,
(byte) (component.Charge * 255),
status,
(short) Math.Round(powerReceiver.PowerReceived),
(short) Math.Round(powerReceiver.Load),
chargeEta
);
_uiSystem.TrySetUiState(
component.Owner.Uid,
SharedGravityGeneratorComponent.GravityGeneratorUiKey.Key,
state);
component.NeedUIUpdate = false;
}
private void OnComponentInitialized(EntityUid uid, GravityGeneratorComponent component, ComponentInit args)
{
// Always update gravity on init.
component.NeedGravityUpdate = true;
ApcPowerReceiverComponent? powerReceiver = null;
if (!Resolve(uid, ref powerReceiver, false))
return;
UpdatePowerState(component, powerReceiver);
UpdateState(component, powerReceiver);
}
private void UpdateGravityActive(GravityGeneratorComponent grav, bool shake)
{
var gridId = grav.Owner.Transform.GridID;
if (gridId == GridId.Invalid)
return;
var grid = _mapManager.GetGrid(gridId);
var gravity = EntityManager.GetComponent<GravityComponent>(grid.GridEntityId);
if (grav.GravityActive)
_gravitySystem.EnableGravity(gravity);
else
_gravitySystem.DisableGravity(gravity);
if (shake)
_gravityShakeSystem.ShakeGrid(gridId, gravity);
}
private void OnInteractHand(EntityUid uid, GravityGeneratorComponent component, InteractHandEvent args)
{
if (!EntityManager.TryGetComponent(args.User.Uid, out ActorComponent? actor))
return;
ApcPowerReceiverComponent? powerReceiver = default!;
if (!Resolve(uid, ref powerReceiver))
return;
// Do not allow opening UI if broken or unpowered.
if (!component.Intact || powerReceiver.PowerReceived < component.IdlePowerUse)
return;
_uiSystem.TryOpen(uid, SharedGravityGeneratorComponent.GravityGeneratorUiKey.Key, actor.PlayerSession);
component.NeedUIUpdate = true;
}
public void UpdateState(GravityGeneratorComponent grav, ApcPowerReceiverComponent powerReceiver)
{
var uid = grav.Owner.Uid;
var appearance = EntityManager.GetComponentOrNull<AppearanceComponent>(uid);
appearance?.SetData(GravityGeneratorVisuals.Charge, grav.Charge);
if (EntityManager.TryGetComponent(uid, out PointLightComponent? pointLight))
{
pointLight.Enabled = grav.Charge > 0;
pointLight.Radius = MathHelper.Lerp(grav.LightRadiusMin, grav.LightRadiusMax, grav.Charge);
}
if (!grav.Intact)
{
MakeBroken(grav, appearance);
}
else if (powerReceiver.PowerReceived < grav.IdlePowerUse)
{
MakeUnpowered(grav, appearance);
}
else if (!grav.SwitchedOn)
{
MakeOff(grav, appearance);
}
else
{
MakeOn(grav, appearance);
}
}
private void MakeBroken(GravityGeneratorComponent component, AppearanceComponent? appearance)
{
_ambientSoundSystem.SetAmbience(component.Owner.Uid, false);
appearance?.SetData(GravityGeneratorVisuals.State, GravityGeneratorStatus.Broken);
}
private void MakeUnpowered(GravityGeneratorComponent component, AppearanceComponent? appearance)
{
_ambientSoundSystem.SetAmbience(component.Owner.Uid, false);
appearance?.SetData(GravityGeneratorVisuals.State, GravityGeneratorStatus.Unpowered);
}
private void MakeOff(GravityGeneratorComponent component, AppearanceComponent? appearance)
{
_ambientSoundSystem.SetAmbience(component.Owner.Uid, false);
appearance?.SetData(GravityGeneratorVisuals.State, GravityGeneratorStatus.Off);
}
private void MakeOn(GravityGeneratorComponent component, AppearanceComponent? appearance)
{
_ambientSoundSystem.SetAmbience(component.Owner.Uid, true);
appearance?.SetData(GravityGeneratorVisuals.State, GravityGeneratorStatus.On);
}
private void OnSwitchGenerator(
EntityUid uid,
GravityGeneratorComponent component,
SharedGravityGeneratorComponent.SwitchGeneratorMessage args)
{
SetSwitchedOn(uid, component, args.On);
}
}
}

View File

@@ -0,0 +1,93 @@
using System.Collections.Generic;
using Content.Server.Camera;
using Content.Shared.Gravity;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Gravity.EntitySystems
{
/// <summary>
/// Handles the grid shake effect used by the gravity generator.
/// </summary>
public sealed class GravityShakeSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private Dictionary<GridId, uint> _gridsToShake = new();
private const float GravityKick = 100.0f;
private const uint ShakeTimes = 10;
private float _internalTimer = 0.0f;
public override void Update(float frameTime)
{
if (_gridsToShake.Count > 0)
{
_internalTimer += frameTime;
if (_internalTimer > 0.2f)
{
// TODO: Could just have clients do this themselves via event and save bandwidth.
ShakeGrids();
_internalTimer -= 0.2f;
}
}
else
{
_internalTimer = 0.0f;
}
}
public void ShakeGrid(GridId gridId, GravityComponent comp)
{
_gridsToShake[gridId] = ShakeTimes;
SoundSystem.Play(
Filter.BroadcastGrid(gridId),
comp.GravityShakeSound.GetSound(),
AudioParams.Default.WithVolume(-2f));
}
private void ShakeGrids()
{
// I have to copy this because C# doesn't allow changing collections while they're
// getting enumerated.
var gridsToShake = new Dictionary<GridId, uint>(_gridsToShake);
foreach (var gridId in _gridsToShake.Keys)
{
if (_gridsToShake[gridId] == 0)
{
gridsToShake.Remove(gridId);
continue;
}
ShakeGrid(gridId);
gridsToShake[gridId] -= 1;
}
_gridsToShake = gridsToShake;
}
private void ShakeGrid(GridId gridId)
{
foreach (var player in _playerManager.GetAllPlayers())
{
if (player.AttachedEntity == null
|| player.AttachedEntity.Transform.GridID != gridId
|| !player.AttachedEntity.TryGetComponent(out CameraRecoilComponent? recoil))
{
continue;
}
recoil.Kick(new Vector2(_random.NextFloat(), _random.NextFloat()) * GravityKick);
}
}
}
}

View File

@@ -1,56 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Camera;
using Content.Shared.Gravity;
using Content.Shared.Sound;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Gravity.EntitySystems
{
[UsedImplicitly]
internal sealed class GravitySystem : SharedGravitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private const float GravityKick = 100.0f;
private const uint ShakeTimes = 10;
private Dictionary<GridId, uint> _gridsToShake = new();
private float _internalTimer = 0.0f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityComponent, ComponentInit>(HandleGravityInitialize);
SubscribeLocalEvent<GravityGeneratorUpdateEvent>(HandleGeneratorUpdate);
}
private void HandleGeneratorUpdate(GravityGeneratorUpdateEvent ev)
{
if (ev.GridId == GridId.Invalid) return;
var gravity = EntityManager.GetComponent<GravityComponent>(_mapManager.GetGrid(ev.GridId).GridEntityId);
if (ev.Status == GravityGeneratorStatus.On)
{
EnableGravity(gravity);
}
else
{
DisableGravity(gravity);
}
}
private void HandleGravityInitialize(EntityUid uid, GravityComponent component, ComponentInit args)
@@ -61,7 +21,7 @@ namespace Content.Server.Gravity.EntitySystems
foreach (var generator in EntityManager.EntityQuery<GravityGeneratorComponent>())
{
if (generator.Owner.Transform.GridID == gridId && generator.Status == GravityGeneratorStatus.On)
if (generator.Owner.Transform.GridID == gridId && generator.GravityActive)
{
component.Enabled = true;
message = new GravityChangedMessage(gridId, true);
@@ -75,103 +35,24 @@ namespace Content.Server.Gravity.EntitySystems
RaiseLocalEvent(message);
}
public override void Update(float frameTime)
{
// TODO: Pointless iteration, just make both of these event-based PLEASE
foreach (var generator in EntityManager.EntityQuery<GravityGeneratorComponent>())
{
if (generator.NeedsUpdate)
{
generator.UpdateState();
}
}
if (_gridsToShake.Count > 0)
{
_internalTimer += frameTime;
if (_internalTimer > 0.2f)
{
// TODO: Could just have clients do this themselves via event and save bandwidth.
ShakeGrids();
_internalTimer -= 0.2f;
}
}
else
{
_internalTimer = 0.0f;
}
}
private void EnableGravity(GravityComponent comp)
public void EnableGravity(GravityComponent comp)
{
if (comp.Enabled) return;
comp.Enabled = true;
var gridId = comp.Owner.Transform.GridID;
ScheduleGridToShake(gridId, ShakeTimes, comp);
var message = new GravityChangedMessage(gridId, true);
RaiseLocalEvent(message);
}
private void DisableGravity(GravityComponent comp)
public void DisableGravity(GravityComponent comp)
{
if (!comp.Enabled) return;
comp.Enabled = false;
var gridId = comp.Owner.Transform.GridID;
ScheduleGridToShake(gridId, ShakeTimes, comp);
var message = new GravityChangedMessage(gridId, false);
RaiseLocalEvent(message);
}
private void ScheduleGridToShake(GridId gridId, uint shakeTimes, GravityComponent comp)
{
if (!_gridsToShake.Keys.Contains(gridId))
{
_gridsToShake.Add(gridId, shakeTimes);
}
else
{
_gridsToShake[gridId] = shakeTimes;
}
SoundSystem.Play(Filter.BroadcastGrid(gridId), comp.GravityShakeSound.GetSound(), AudioParams.Default.WithVolume(-2f));
}
private void ShakeGrids()
{
// I have to copy this because C# doesn't allow changing collections while they're
// getting enumerated.
var gridsToShake = new Dictionary<GridId, uint>(_gridsToShake);
foreach (var gridId in _gridsToShake.Keys)
{
if (_gridsToShake[gridId] == 0)
{
gridsToShake.Remove(gridId);
continue;
}
ShakeGrid(gridId);
gridsToShake[gridId] -= 1;
}
_gridsToShake = gridsToShake;
}
private void ShakeGrid(GridId gridId)
{
foreach (var player in _playerManager.GetAllPlayers())
{
if (player.AttachedEntity == null
|| player.AttachedEntity.Transform.GridID != gridId
|| !player.AttachedEntity.TryGetComponent(out CameraRecoilComponent? recoil))
{
continue;
}
recoil.Kick(new Vector2(_random.NextFloat(), _random.NextFloat()) * GravityKick);
}
}
}
}

View File

@@ -1,186 +1,52 @@
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.Acts;
using Content.Shared.Audio;
using Content.Server.Gravity.EntitySystems;
using Content.Shared.Gravity;
using Content.Shared.Interaction;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Gravity
{
[RegisterComponent]
public class GravityGeneratorComponent : SharedGravityGeneratorComponent, IBreakAct, IInteractHand
[Friend(typeof(GravityGeneratorSystem))]
public sealed class GravityGeneratorComponent : SharedGravityGeneratorComponent
{
[ComponentDependency] private readonly AppearanceComponent? _appearance = default!;
[DataField("switchedOn")]
private bool _switchedOn = true;
[DataField("intact")]
private bool _intact = true;
private GravityGeneratorStatus _status;
public bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered;
public bool SwitchedOn => _switchedOn;
public bool Intact => _intact;
public GravityGeneratorStatus Status => _status;
public bool NeedsUpdate
{
get
{
switch (_status)
{
case GravityGeneratorStatus.On:
return !(Powered && SwitchedOn && Intact);
case GravityGeneratorStatus.Off:
return SwitchedOn || !(Powered && Intact);
case GravityGeneratorStatus.Unpowered:
return SwitchedOn || Powered || !Intact;
case GravityGeneratorStatus.Broken:
return SwitchedOn || Powered || Intact;
default:
return true; // This _should_ be unreachable
}
}
}
public override string Name => "GravityGenerator";
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(GravityGeneratorUiKey.Key);
// 1% charge per second.
[ViewVariables(VVAccess.ReadWrite)] [DataField("chargeRate")] public float ChargeRate { get; set; } = 0.01f;
// The gravity generator has two power values.
// Idle power is assumed to be the power needed to run the control systems and interface.
[DataField("idlePower")] public float IdlePowerUse { get; set; }
// Active power is the power needed to keep the gravity field stable.
[DataField("activePower")] public float ActivePowerUse { get; set; }
[DataField("lightRadiusMin")] public float LightRadiusMin { get; set; }
[DataField("lightRadiusMax")] public float LightRadiusMax { get; set; }
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += HandleUIMessage;
}
/// <summary>
/// Is the power switch on?
/// </summary>
[DataField("switchedOn")]
public bool SwitchedOn { get; set; } = true;
_switchedOn = true;
_intact = true;
_status = GravityGeneratorStatus.On;
UpdateState();
}
/// <summary>
/// Is the gravity generator intact?
/// </summary>
[DataField("intact")]
public bool Intact { get; set; } = true;
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent<ActorComponent>(out var actor))
return false;
if (Status != GravityGeneratorStatus.Off && Status != GravityGeneratorStatus.On)
{
return false;
}
OpenUserInterface(actor.PlayerSession);
return true;
}
// 0 -> 1
[ViewVariables(VVAccess.ReadWrite)] [DataField("charge")] public float Charge { get; set; } = 1;
public void OnBreak(BreakageEventArgs eventArgs)
{
_intact = false;
_switchedOn = false;
}
/// <summary>
/// Is the gravity generator currently "producing" gravity?
/// </summary>
[DataField("active")]
public bool GravityActive { get; set; } = true;
public void UpdateState()
{
if (!Intact)
{
MakeBroken();
}
else if (!Powered)
{
MakeUnpowered();
}
else if (!SwitchedOn)
{
MakeOff();
}
else
{
MakeOn();
}
var msg = new GravityGeneratorUpdateEvent(Owner.Transform.GridID, Status);
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, msg);
}
private void HandleUIMessage(ServerBoundUserInterfaceMessage message)
{
switch (message.Message)
{
case GeneratorStatusRequestMessage _:
UserInterface?.SetState(new GeneratorState(Status == GravityGeneratorStatus.On));
break;
case SwitchGeneratorMessage msg:
_switchedOn = msg.On;
UpdateState();
break;
default:
break;
}
}
private void OpenUserInterface(IPlayerSession playerSession)
{
UserInterface?.Open(playerSession);
}
private void MakeBroken()
{
_status = GravityGeneratorStatus.Broken;
EntitySystem.Get<SharedAmbientSoundSystem>().SetAmbience(Owner.Uid, false);
_appearance?.SetData(GravityGeneratorVisuals.State, Status);
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, false);
}
private void MakeUnpowered()
{
_status = GravityGeneratorStatus.Unpowered;
EntitySystem.Get<SharedAmbientSoundSystem>().SetAmbience(Owner.Uid, false);
_appearance?.SetData(GravityGeneratorVisuals.State, Status);
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, false);
}
private void MakeOff()
{
_status = GravityGeneratorStatus.Off;
EntitySystem.Get<SharedAmbientSoundSystem>().SetAmbience(Owner.Uid, false);
_appearance?.SetData(GravityGeneratorVisuals.State, Status);
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, false);
}
private void MakeOn()
{
_status = GravityGeneratorStatus.On;
EntitySystem.Get<SharedAmbientSoundSystem>().SetAmbience(Owner.Uid, true);
_appearance?.SetData(GravityGeneratorVisuals.State, Status);
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, true);
}
}
public sealed class GravityGeneratorUpdateEvent : EntityEventArgs
{
public GridId GridId { get; }
public GravityGeneratorStatus Status { get; }
public GravityGeneratorUpdateEvent(GridId gridId, GravityGeneratorStatus status)
{
GridId = gridId;
Status = status;
}
// Do we need a UI update even if the charge doesn't change? Used by power button.
[ViewVariables] public bool NeedUIUpdate { get; set; }
[ViewVariables] public bool NeedGravityUpdate { get; set; }
}
}

View File

@@ -72,6 +72,8 @@ namespace Content.Server.Power.Components
DesiredPower = 5
};
public float PowerReceived => NetworkLoad.ReceivingPower;
protected override void OnRemove()
{
Provider?.RemoveReceiver(this);
@@ -81,15 +83,10 @@ namespace Content.Server.Power.Components
public void ApcPowerChanged()
{
OnNewPowerState();
}
private void OnNewPowerState()
{
#pragma warning disable 618
SendMessage(new PowerChangedMessage(Powered));
#pragma warning restore 618
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PowerChangedEvent(Powered));
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PowerChangedEvent(Powered, NetworkLoad.ReceivingPower));
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{
@@ -126,10 +123,12 @@ namespace Content.Server.Power.Components
public sealed class PowerChangedEvent : EntityEventArgs
{
public readonly bool Powered;
public readonly float ReceivingPower;
public PowerChangedEvent(bool powered)
public PowerChangedEvent(bool powered, float receivingPower)
{
Powered = powered;
ReceivingPower = receivingPower;
}
}
}

View File

@@ -24,26 +24,31 @@ namespace Content.Shared.Gravity
}
}
/// <summary>
/// Sent to the server when requesting the status of the generator
/// </summary>
[Serializable, NetSerializable]
public class GeneratorStatusRequestMessage : BoundUserInterfaceMessage
{
public GeneratorStatusRequestMessage()
{
}
}
[Serializable, NetSerializable]
public class GeneratorState : BoundUserInterfaceState
{
public bool On;
// 0 -> 255
public byte Charge;
public GravityGeneratorPowerStatus PowerStatus;
public short PowerDraw;
public short PowerDrawMax;
public short EtaSeconds;
public GeneratorState(bool on)
public GeneratorState(
bool on,
byte charge,
GravityGeneratorPowerStatus powerStatus,
short powerDraw,
short powerDrawMax,
short etaSeconds)
{
On = on;
Charge = charge;
PowerStatus = powerStatus;
PowerDraw = powerDraw;
PowerDrawMax = powerDrawMax;
EtaSeconds = etaSeconds;
}
}
@@ -58,7 +63,7 @@ namespace Content.Shared.Gravity
public enum GravityGeneratorVisuals
{
State,
CoreVisible
Charge
}
[Serializable, NetSerializable]
@@ -69,4 +74,13 @@ namespace Content.Shared.Gravity
Off,
On
}
[Serializable, NetSerializable]
public enum GravityGeneratorPowerStatus : byte
{
Off,
Discharging,
Charging,
FullyCharged
}
}

View File

@@ -1,8 +1,28 @@
### Gravity Generator
## UI
gravity-generator-window-title = Gravity Generator Control
gravity-generator-window-status-label = Current Status: {$status}
gravity-generator-window-turn-off-button = Turn off
gravity-generator-window-turn-on-button = Turn on
gravity-generator-window-is-on = [color=green]On[/color]
gravity-generator-window-is-off = [color=red]Off[/color]
gravity-generator-window-title = Gravity Generator
## UI field names
gravity-generator-window-status = Status:
gravity-generator-window-power = Power:
gravity-generator-window-eta = ETA:
gravity-generator-window-charge = Charge:
## UI statuses
gravity-generator-window-status-fully-charged = Fully Charged
gravity-generator-window-status-off = Off
gravity-generator-window-status-charging = Charging
gravity-generator-window-status-discharging = Discharging
## UI Power Buttons
gravity-generator-window-power-on = On
gravity-generator-window-power-off = Off
gravity-generator-window-power-label = { $draw } / { $max } W
## UI ETA label
gravity-generator-window-eta-none = N/A
gravity-generator-window-eta-value = { TOSTRING($left, "m\\:ss") }

View File

@@ -23,7 +23,6 @@
- type: Transform
anchored: true
- type: ApcPowerReceiver
powerLoad: 500
- type: ExtensionCableReceiver
- type: Physics
bodyType: Static
@@ -51,6 +50,10 @@
- !type:DoActsBehavior
acts: ["Breakage"]
- type: GravityGenerator
idlePower: 15
activePower: 500
lightRadiusMin: 0.75
lightRadiusMax: 2.5
- type: UserInterface
interfaces:
- key: enum.GravityGeneratorUiKey.Key
@@ -63,3 +66,8 @@
unpowered: "off"
off: "off"
on: "on"
- type: PointLight
energy: 0.5
# Gravity generator is a large machine, not casting shadows is fine within the radius set above.
castShadows: false
color: "#a8ffd9"