Refactoring of solar control console (#4072)
* Refactor/fix client-side of solar control computer (introduce ComputerBoundUserInterface & fix bugs) * Refactor server side of solar control computer (introduce BaseComputerUserInterfaceComponent) * If you can't interact, then messages to computers are blocked. * Add 'not powered' messages, migrate activation logic partially to an EntitySystem * Move solar control console to a XAML UI * Remove useless comment on UserInterfaceKey * BaseComputerUserInterfaceComponent: Remove EnsureComponent<PowerReceiver>, it's not necessary * Fix solar panel occlusion check direction * Solar Control Console refactors/etc. : Handle namespace renames
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.GameObjects.Components;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using Robust.Shared.Localization;
|
||||
|
||||
namespace Content.Server.GameObjects.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// This component is used as a base class for classes like SolarControlConsoleComponent.
|
||||
/// These components operate the server-side logic for the "primary UI" of a computer.
|
||||
/// That means showing the UI when a user activates it, for example.
|
||||
/// </summary>
|
||||
public abstract class BaseComputerUserInterfaceComponent : Component
|
||||
{
|
||||
protected readonly object UserInterfaceKey;
|
||||
|
||||
[ViewVariables] protected BoundUserInterface? UserInterface => Owner.GetUIOrNull(UserInterfaceKey);
|
||||
[ViewVariables] public bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
public BaseComputerUserInterfaceComponent(object key)
|
||||
{
|
||||
UserInterfaceKey = key;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
UserInterface.OnReceiveMessage += OnReceiveUIMessageCallback;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal callback used to grab session and session attached entity before any more work is done.
|
||||
/// This is so that sessionEntity is always available to checks up and down the line.
|
||||
/// </summary>
|
||||
private void OnReceiveUIMessageCallback(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
var session = obj.Session;
|
||||
var sessionEntity = session.AttachedEntity;
|
||||
if (sessionEntity == null)
|
||||
return; // No session entity, so we're probably not able to touch this.
|
||||
OnReceiveUnfilteredUserInterfaceMessage(obj, sessionEntity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to handle messages from the UI before filtering them.
|
||||
/// Calling base is necessary if you want this class to have any meaning.
|
||||
/// </summary>
|
||||
protected void OnReceiveUnfilteredUserInterfaceMessage(ServerBoundUserInterfaceMessage obj, IEntity sessionEntity)
|
||||
{
|
||||
// "Across all computers" "anti-cheats" ought to be put here or at some parent level (BaseDeviceUserInterfaceComponent?)
|
||||
// Determine some facts about the session.
|
||||
// Powered?
|
||||
if (!Powered)
|
||||
{
|
||||
sessionEntity.PopupMessageCursor(Loc.GetString("base-computer-ui-component-not-powered"));
|
||||
return; // Not powered, so this computer should probably do nothing.
|
||||
}
|
||||
// Can we interact?
|
||||
if (!ActionBlockerSystem.CanInteract(sessionEntity))
|
||||
{
|
||||
sessionEntity.PopupMessageCursor(Loc.GetString("base-computer-ui-component-cannot-interact"));
|
||||
return;
|
||||
}
|
||||
// Good to go!
|
||||
OnReceiveUserInterfaceMessage(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this to handle messages from the UI.
|
||||
/// Calling base is unnecessary.
|
||||
/// These messages will automatically be blocked if the user shouldn't be able to access this computer, or if the computer has lost power.
|
||||
/// </summary>
|
||||
protected virtual void OnReceiveUserInterfaceMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
// Nothing!
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PowerChangedMessage powerChanged:
|
||||
PowerReceiverOnOnPowerStateChanged(powerChanged);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void PowerReceiverOnOnPowerStateChanged(PowerChangedMessage e)
|
||||
{
|
||||
if (!e.Powered)
|
||||
{
|
||||
// We need to kick off users who are using it when it loses power.
|
||||
UserInterface?.CloseAll();
|
||||
// Now alert subclass.
|
||||
ComputerLostPower();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override this if you want the computer to do something when it loses power (i.e. reset state)
|
||||
/// All UIs should have been closed by the time this is called.
|
||||
/// Calling base is unnecessary.
|
||||
/// </summary>
|
||||
public virtual void ComputerLostPower()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is called from ComputerUIActivatorSystem.
|
||||
/// Override this to add additional activation conditions of some sort.
|
||||
/// Calling base runs standard activation logic.
|
||||
/// *This remains inside the component for overridability.*
|
||||
/// </summary>
|
||||
public virtual void ActivateThunk(ActivateInWorldEvent eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Powered)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("base-computer-ui-component-not-powered"));
|
||||
return;
|
||||
}
|
||||
|
||||
UserInterface?.Open(actor.PlayerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Server.GameObjects.Components;
|
||||
using Content.Shared.GameTicking;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class ComputerUIActivatorSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<BaseComputerUserInterfaceComponent, ActivateInWorldEvent>(HandleActivate);
|
||||
}
|
||||
|
||||
private void HandleActivate(EntityUid uid, BaseComputerUserInterfaceComponent component, ActivateInWorldEvent args)
|
||||
{
|
||||
component.ActivateThunk(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
#nullable enable
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Solar.EntitySystems;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Interaction;
|
||||
#nullable enable
|
||||
using Content.Shared.Solar;
|
||||
using Content.Server.Solar.EntitySystems;
|
||||
using Content.Server.GameObjects.Components;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
@@ -12,26 +11,20 @@ using Robust.Shared.ViewVariables;
|
||||
namespace Content.Server.Solar.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class SolarControlConsoleComponent : SharedSolarControlConsoleComponent, IActivate
|
||||
[ComponentReference(typeof(BaseComputerUserInterfaceComponent))]
|
||||
public class SolarControlConsoleComponent : BaseComputerUserInterfaceComponent
|
||||
{
|
||||
public override string Name => "SolarControlConsole";
|
||||
|
||||
[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 SolarControlConsoleComponent() : base(SolarControlConsoleUiKey.Key) { }
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
|
||||
Owner.EnsureComponent<PowerReceiverComponent>();
|
||||
_powerSolarSystem = _entitySystemManager.GetEntitySystem<PowerSolarSystem>();
|
||||
}
|
||||
|
||||
@@ -40,7 +33,7 @@ namespace Content.Server.Solar.Components
|
||||
UserInterface?.SetState(new SolarControlConsoleBoundInterfaceState(_powerSolarSystem.TargetPanelRotation, _powerSolarSystem.TargetPanelVelocity, _powerSolarSystem.TotalPanelPower, _powerSolarSystem.TowardsSun));
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
protected override void OnReceiveUserInterfaceMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
switch (obj.Message)
|
||||
{
|
||||
@@ -56,22 +49,5 @@ namespace Content.Server.Solar.Components
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ namespace Content.Server.Solar.EntitySystems
|
||||
{
|
||||
// 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.ToVec(), (int) CollisionGroup.Opaque);
|
||||
var ray = new CollisionRay(entity.Transform.WorldPosition, TowardsSun.ToWorldVec(), (int) CollisionGroup.Opaque);
|
||||
var rayCastResults = EntitySystem.Get<SharedBroadPhaseSystem>().IntersectRay(entity.Transform.MapID, ray, SunOcclusionCheckDistance, entity);
|
||||
if (rayCastResults.Any())
|
||||
coverage = 0;
|
||||
|
||||
Reference in New Issue
Block a user