Gravity generator rewrite (#4828)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
committed by
GitHub
parent
0e33b246db
commit
059fa9ae48
286
Content.Server/Gravity/EntitySystems/GravityGeneratorSystem.cs
Normal file
286
Content.Server/Gravity/EntitySystems/GravityGeneratorSystem.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
93
Content.Server/Gravity/EntitySystems/GravityShakeSystem.cs
Normal file
93
Content.Server/Gravity/EntitySystems/GravityShakeSystem.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user