add gateway for admeme (#17587)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-08-02 10:47:18 +01:00
committed by GitHub
parent ce25812215
commit 3d1ade9684
13 changed files with 682 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
using Content.Shared.Gateway;
using Robust.Client.GameObjects;
namespace Content.Client.Gateway.UI;
public sealed class GatewayBoundUserInterface : BoundUserInterface
{
private GatewayWindow? _window;
public GatewayBoundUserInterface(ClientUserInterfaceComponent owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_window = new GatewayWindow();
_window.OpenPortal += destination =>
{
SendMessage(new GatewayOpenPortalMessage(destination));
};
_window.OnClose += Close;
_window?.OpenCentered();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_window?.Dispose();
_window = null;
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not GatewayBoundUserInterfaceState current)
return;
_window?.UpdateState(current);
}
}

View File

@@ -0,0 +1,22 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
Title="{Loc 'gateway-window-title'}"
MinSize="800 360">
<BoxContainer Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<Label Name="NextCloseLabel"
Text="{Loc 'gateway-window-portal-closing'}"
Margin="5"></Label>
<ProgressBar Name="NextCloseBar"
HorizontalExpand="True"
MinValue="0"
MaxValue="1"
SetHeight="25"/>
<Label Name="NextCloseText" Text="0" Margin="5"/>
</BoxContainer>
<controls:HLine Color="#404040" Thickness="2" Margin="0 5 0 5"/>
<BoxContainer Name="Container"
Orientation="Vertical"
Margin="5 0 5 0"/>
</BoxContainer>
</controls:FancyWindow>

View File

@@ -0,0 +1,180 @@
using Content.Client.Computer;
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Controls;
using Content.Shared.Gateway;
using Content.Shared.Shuttles.BUIStates;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
namespace Content.Client.Gateway.UI;
[GenerateTypedNameReferences]
public sealed partial class GatewayWindow : FancyWindow,
IComputerWindow<EmergencyConsoleBoundUserInterfaceState>
{
private readonly IGameTiming _timing;
public event Action<EntityUid>? OpenPortal;
private List<(EntityUid, string, TimeSpan, bool)> _destinations = default!;
private EntityUid? _current;
private TimeSpan _nextClose;
private TimeSpan _lastOpen;
private List<Label> _readyLabels = default!;
private List<Button> _openButtons = default!;
public GatewayWindow()
{
RobustXamlLoader.Load(this);
_timing = IoCManager.Resolve<IGameTiming>();
}
public void UpdateState(GatewayBoundUserInterfaceState state)
{
_destinations = state.Destinations;
_current = state.Current;
_nextClose = state.NextClose;
_lastOpen = state.LastOpen;
Container.DisposeAllChildren();
_readyLabels = new List<Label>(_destinations.Count);
_openButtons = new List<Button>(_destinations.Count);
if (_destinations.Count == 0)
{
Container.AddChild(new BoxContainer()
{
HorizontalExpand = true,
VerticalExpand = true,
Children =
{
new Label()
{
Text = Loc.GetString("gateway-window-no-destinations"),
HorizontalAlignment = HAlignment.Center
}
}
});
return;
}
var now = _timing.CurTime;
foreach (var dest in _destinations)
{
var uid = dest.Item1;
var name = dest.Item2;
var nextReady = dest.Item3;
var busy = dest.Item4;
var box = new BoxContainer()
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
Margin = new Thickness(5f, 5f)
};
box.AddChild(new Label()
{
Text = name
});
var readyLabel = new Label
{
Text = ReadyText(now, nextReady),
Margin = new Thickness(10f, 0f, 0f, 0f)
};
_readyLabels.Add(readyLabel);
box.AddChild(readyLabel);
var openButton = new Button()
{
Text = Loc.GetString("gateway-window-open-portal"),
Pressed = uid == _current,
ToggleMode = true,
Disabled = _current != null || busy || now < nextReady
};
openButton.OnPressed += args =>
{
OpenPortal?.Invoke(uid);
};
if (uid == state.Current)
{
openButton.AddStyleClass(StyleBase.ButtonCaution);
}
_openButtons.Add(openButton);
box.AddChild(new BoxContainer()
{
HorizontalExpand = true,
Align = BoxContainer.AlignMode.End,
Children =
{
openButton
}
});
Container.AddChild(new PanelContainer()
{
PanelOverride = new StyleBoxFlat(new Color(30, 30, 34)),
Margin = new Thickness(10f, 5f),
Children =
{
box
}
});
}
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
var now = _timing.CurTime;
// if its not going to close then show it as empty
if (_current == null)
{
NextCloseBar.Value = 0f;
NextCloseText.Text = "00:00";
}
else
{
var remaining = _nextClose - _timing.CurTime;
if (remaining < TimeSpan.Zero)
{
NextCloseBar.Value = 1f;
NextCloseText.Text = "00:00";
}
else
{
var openTime = _nextClose - _lastOpen;
NextCloseBar.Value = 1f - (float) (remaining / openTime);
NextCloseText.Text = $"{remaining.Minutes:00}:{remaining.Seconds:00}";
}
}
for (var i = 0; i < _destinations.Count; i++)
{
var dest = _destinations[i];
var nextReady = dest.Item3;
var busy = dest.Item4;
_readyLabels[i].Text = ReadyText(now, nextReady);
_openButtons[i].Disabled = _current != null || busy || now < nextReady;
}
}
private string ReadyText(TimeSpan now, TimeSpan nextReady)
{
if (now < nextReady)
{
var time = nextReady - now;
return Loc.GetString("gateway-window-ready-in", ("time", $"{time.Minutes:00}:{time.Seconds:00}"));
}
return Loc.GetString("gateway-window-ready");
}
}

View File

@@ -0,0 +1,40 @@
using Content.Server.Gateway.Systems;
using Robust.Shared.Audio;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Server.Gateway.Components;
/// <summary>
/// Controlling gateway that links to other gateway destinations on the server.
/// </summary>
[RegisterComponent, Access(typeof(GatewaySystem))]
public sealed class GatewayComponent : Component
{
/// <summary>
/// Sound to play when opening or closing the portal.
/// </summary>
[DataField("portalSound")]
public SoundSpecifier PortalSound = new SoundPathSpecifier("/Audio/Effects/Lightning/lightningbolt.ogg");
/// <summary>
/// Every other gateway destination on the server.
/// </summary>
/// <remarks>
/// Added on
/// </remarks>
[ViewVariables]
public HashSet<EntityUid> Destinations = new();
/// <summary>
/// The time at which the portal will be closed.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("nextClose", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextClose;
/// <summary>
/// The time at which the portal was last opened.
/// Only used for UI.
/// </summary>
[ViewVariables]
public TimeSpan LastOpen;
}

View File

@@ -0,0 +1,42 @@
using Content.Server.Gateway.Systems;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Server.Gateway.Components;
/// <summary>
/// A gateway destination linked to by station gateway(s).
/// </summary>
[RegisterComponent, Access(typeof(GatewaySystem))]
public sealed class GatewayDestinationComponent : Component
{
/// <summary>
/// Whether this destination is shown in the gateway ui.
/// If you are making a gateway for an admeme set this once you are ready for players to select it.
/// </summary>
[DataField("enabled"), ViewVariables(VVAccess.ReadWrite)]
public bool Enabled;
/// <summary>
/// Name as it shows up on the ui of station gateways.
/// </summary>
[DataField("name"), ViewVariables(VVAccess.ReadWrite)]
public string Name = string.Empty;
/// <summary>
/// Time at which this destination is ready to be linked to.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("nextReady", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextReady;
/// <summary>
/// How long the portal will be open for after linking.
/// </summary>
[DataField("openTime"), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan OpenTime = TimeSpan.FromSeconds(600);
/// <summary>
/// How long the destination is not ready for after the portal closes.
/// </summary>
[DataField("cooldown"), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan Cooldown = TimeSpan.FromSeconds(60);
}

View File

@@ -0,0 +1,181 @@
using Content.Server.Gateway.Components;
using Content.Shared.Gateway;
using Content.Shared.Teleportation.Components;
using Content.Shared.Teleportation.Systems;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Timing;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Content.Server.Gateway.Systems;
public sealed class GatewaySystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly LinkedEntitySystem _linkedEntity = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GatewayComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<GatewayComponent, BoundUIOpenedEvent>(UpdateUserInterface);
SubscribeLocalEvent<GatewayComponent, GatewayOpenPortalMessage>(OnOpenPortal);
SubscribeLocalEvent<GatewayDestinationComponent, ComponentStartup>(OnDestinationStartup);
SubscribeLocalEvent<GatewayDestinationComponent, ComponentShutdown>(OnDestinationShutdown);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
// close portals after theyve been open long enough
var query = EntityQueryEnumerator<GatewayComponent, PortalComponent>();
while (query.MoveNext(out var uid, out var comp, out var _))
{
if (_timing.CurTime < comp.NextClose)
continue;
ClosePortal(uid, comp);
}
}
private void OnStartup(EntityUid uid, GatewayComponent comp, ComponentStartup args)
{
// add existing destinations
var query = EntityQueryEnumerator<GatewayDestinationComponent>();
while (query.MoveNext(out var dest, out _))
{
comp.Destinations.Add(dest);
}
// no need to update ui since its just been created, just do portal
UpdateAppearance(uid);
}
private void UpdateUserInterface<T>(EntityUid uid, GatewayComponent comp, T args)
{
UpdateUserInterface(uid, comp);
}
private void UpdateUserInterface(EntityUid uid, GatewayComponent comp)
{
var destinations = new List<(EntityUid, String, TimeSpan, bool)>();
foreach (var destUid in comp.Destinations)
{
var dest = Comp<GatewayDestinationComponent>(destUid);
if (!dest.Enabled)
continue;
destinations.Add((destUid, dest.Name, dest.NextReady, HasComp<PortalComponent>(destUid)));
}
GetDestination(uid, out var current);
var state = new GatewayBoundUserInterfaceState(destinations, current, comp.NextClose, comp.LastOpen);
_ui.TrySetUiState(uid, GatewayUiKey.Key, state);
}
private void UpdateAppearance(EntityUid uid)
{
_appearance.SetData(uid, GatewayVisuals.Active, HasComp<PortalComponent>(uid));
}
private void OnOpenPortal(EntityUid uid, GatewayComponent comp, GatewayOpenPortalMessage args)
{
// can't link if portal is already open on either side, the destination is invalid or on cooldown
if (HasComp<PortalComponent>(uid) ||
HasComp<PortalComponent>(args.Destination) ||
!TryComp<GatewayDestinationComponent>(args.Destination, out var dest) ||
!dest.Enabled ||
_timing.CurTime < dest.NextReady)
return;
// TODO: admin log???
OpenPortal(uid, comp, args.Destination, dest);
}
private void OpenPortal(EntityUid uid, GatewayComponent comp, EntityUid dest, GatewayDestinationComponent destComp)
{
_linkedEntity.TryLink(uid, dest);
EnsureComp<PortalComponent>(uid);
EnsureComp<PortalComponent>(dest);
// for ui
comp.LastOpen = _timing.CurTime;
// close automatically after time is up
comp.NextClose = comp.LastOpen + destComp.OpenTime;
_audio.PlayPvs(comp.PortalSound, uid);
_audio.PlayPvs(comp.PortalSound, dest);
UpdateUserInterface(uid, comp);
UpdateAppearance(uid);
UpdateAppearance(dest);
}
private void ClosePortal(EntityUid uid, GatewayComponent comp)
{
RemComp<PortalComponent>(uid);
if (!GetDestination(uid, out var dest))
return;
if (TryComp<GatewayDestinationComponent>(dest, out var destComp))
{
// portals closed, put it on cooldown and let it eventually be opened again
destComp.NextReady = _timing.CurTime + destComp.Cooldown;
}
_audio.PlayPvs(comp.PortalSound, uid);
_audio.PlayPvs(comp.PortalSound, dest.Value);
_linkedEntity.TryUnlink(uid, dest.Value);
RemComp<PortalComponent>(dest.Value);
UpdateUserInterface(uid, comp);
UpdateAppearance(uid);
UpdateAppearance(dest.Value);
}
private bool GetDestination(EntityUid uid, [NotNullWhen(true)] out EntityUid? dest)
{
dest = null;
if (TryComp<LinkedEntityComponent>(uid, out var linked))
{
var first = linked.LinkedEntities.FirstOrDefault();
if (first != EntityUid.Invalid)
{
dest = first;
return true;
}
}
return false;
}
private void OnDestinationStartup(EntityUid uid, GatewayDestinationComponent comp, ComponentStartup args)
{
var query = EntityQueryEnumerator<GatewayComponent>();
while (query.MoveNext(out var gatewayUid, out var gateway))
{
gateway.Destinations.Add(uid);
UpdateUserInterface(gatewayUid, gateway);
}
UpdateAppearance(uid);
}
private void OnDestinationShutdown(EntityUid uid, GatewayDestinationComponent comp, ComponentShutdown args)
{
var query = EntityQueryEnumerator<GatewayComponent>();
while (query.MoveNext(out var gatewayUid, out var gateway))
{
gateway.Destinations.Remove(uid);
UpdateUserInterface(gatewayUid, gateway);
}
}
}

View File

@@ -0,0 +1,65 @@
using Robust.Shared.Serialization;
namespace Content.Shared.Gateway;
[Serializable, NetSerializable]
public enum GatewayVisuals : byte
{
Active
}
[Serializable, NetSerializable]
public enum GatewayVisualLayers : byte
{
Portal
}
[Serializable, NetSerializable]
public enum GatewayUiKey : byte
{
Key
}
[Serializable, NetSerializable]
public sealed class GatewayBoundUserInterfaceState : BoundUserInterfaceState
{
/// <summary>
/// List of enabled destinations and information about them.
/// </summary>
public readonly List<(EntityUid, string, TimeSpan, bool)> Destinations;
/// <summary>
/// Which destination it is currently linked to, if any.
/// </summary>
public readonly EntityUid? Current;
/// <summary>
/// Time the portal will close at.
/// </summary>
public readonly TimeSpan NextClose;
/// <summary>
/// Time the portal last opened at.
/// </summary>
public readonly TimeSpan LastOpen;
public GatewayBoundUserInterfaceState(List<(EntityUid, string, TimeSpan, bool)> destinations,
EntityUid? current, TimeSpan nextClose, TimeSpan lastOpen)
{
Destinations = destinations;
Current = current;
NextClose = nextClose;
LastOpen = lastOpen;
}
}
[Serializable, NetSerializable]
public sealed class GatewayOpenPortalMessage : BoundUserInterfaceMessage
{
public EntityUid Destination;
public GatewayOpenPortalMessage(EntityUid destination)
{
Destination = destination;
}
}

View File

@@ -0,0 +1,6 @@
gateway-window-title = Gateway
gateway-window-ready = Ready!
gateway-window-ready-in = Ready in: {$time}s
gateway-window-open-portal = Open Portal
gateway-window-no-destinations = No destinations found.
gateway-window-portal-closing = Portal closing

View File

@@ -0,0 +1,69 @@
- type: entity
abstract: true
parent: BaseStructure
id: BaseGateway
name: gateway
description: A mysterious gateway built by unknown hands, it allows for faster than light travel to far-flung locations.
components:
- type: Sprite
sprite: Structures/Machines/gateway.rsi
noRot: true
layers:
- state: frame
- state: portal
shader: unshaded
map: ["enum.GatewayVisualLayers.Portal"]
- state: on
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- type: GenericVisualizer
visuals:
enum.PowerDeviceVisuals.Powered:
enum.PowerDeviceVisualLayers.Powered:
True: { visible: true }
False: { visible: false }
enum.GatewayVisuals.Active:
enum.GatewayVisualLayers.Portal:
True: { visible: true }
False: { visible: false }
- type: InteractionOutline
- type: Physics
bodyType: Static
- type: Transform
noRot: true
- type: Fixtures
fixtures:
portalFixture:
shape:
!type:PhysShapeAabb
bounds: "-0.25,-0.48,0.25,0.48"
mask:
- FullTileMask
layer:
- WallLayer
hard: false
- type: Appearance
- type: entity
parent: BaseGateway
id: Gateway
components:
- type: ActivatableUI
key: enum.GatewayUiKey.Key
- type: ActivatableUIRequiresPower
- type: UserInterface
interfaces:
- key: enum.GatewayUiKey.Key
type: GatewayBoundUserInterface
- type: ApcPowerReceiver
powerLoad: 3000
- type: ExtensionCableReceiver
- type: Gateway
- type: entity
parent: BaseGateway
id: GatewayDestination
suffix: Destination
components:
- type: GatewayDestination
name: Unknown

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -0,0 +1,34 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "Taken from tgstation at https://github.com/tgstation/tgstation/blob/1a2b4c66e573cac0e9eb0699b42890b287662de4/icons/obj/machines/gateway.dmi",
"size": {
"x": 96,
"y": 96
},
"states": [
{
"name": "frame"
},
{
"name": "on",
"delays": [
[
0.2,
0.2,
0.2
]
]
},
{
"name": "portal",
"delays": [
[
0.3,
0.3,
0.3
]
]
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB