Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using Content.Client.DoAfter.UI;
using Content.Shared.DoAfter;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Players;
using Robust.Shared.Timing;
namespace Content.Client.DoAfter
{
[RegisterComponent]
public sealed class DoAfterComponent : SharedDoAfterComponent
{
public override string Name => "DoAfter";
public IReadOnlyDictionary<byte, ClientDoAfter> DoAfters => _doAfters;
private readonly Dictionary<byte, ClientDoAfter> _doAfters = new();
public readonly List<(TimeSpan CancelTime, ClientDoAfter Message)> CancelledDoAfters = new();
public DoAfterGui? Gui { get; set; }
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null)
{
base.HandleNetworkMessage(message, netChannel, session);
switch (message)
{
case CancelledDoAfterMessage msg:
Cancel(msg.ID);
break;
}
}
public override void OnAdd()
{
base.OnAdd();
Enable();
}
public override void OnRemove()
{
base.OnRemove();
Disable();
}
/// <summary>
/// For handling PVS so we dispose of controls if they go out of range
/// </summary>
public void Enable()
{
if (Gui?.Disposed == false)
return;
Gui = new DoAfterGui {AttachedEntity = Owner};
foreach (var (_, doAfter) in _doAfters)
{
Gui.AddDoAfter(doAfter);
}
foreach (var (_, cancelled) in CancelledDoAfters)
{
Gui.CancelDoAfter(cancelled.ID);
}
}
public void Disable()
{
Gui?.Dispose();
Gui = null;
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not DoAfterComponentState state)
return;
var toRemove = new List<ClientDoAfter>();
foreach (var (id, doAfter) in _doAfters)
{
var found = false;
foreach (var clientdoAfter in state.DoAfters)
{
if (clientdoAfter.ID == id)
{
found = true;
break;
}
}
if (!found)
{
toRemove.Add(doAfter);
}
}
foreach (var doAfter in toRemove)
{
Remove(doAfter);
}
foreach (var doAfter in state.DoAfters)
{
if (_doAfters.ContainsKey(doAfter.ID))
continue;
_doAfters.Add(doAfter.ID, doAfter);
}
if (Gui == null || Gui.Disposed)
return;
foreach (var (_, doAfter) in _doAfters)
{
Gui.AddDoAfter(doAfter);
}
}
/// <summary>
/// Remove a DoAfter without showing a cancellation graphic.
/// </summary>
/// <param name="clientDoAfter"></param>
public void Remove(ClientDoAfter clientDoAfter)
{
_doAfters.Remove(clientDoAfter.ID);
var found = false;
for (var i = CancelledDoAfters.Count - 1; i >= 0; i--)
{
var cancelled = CancelledDoAfters[i];
if (cancelled.Message == clientDoAfter)
{
CancelledDoAfters.RemoveAt(i);
found = true;
break;
}
}
if (!found)
_doAfters.Remove(clientDoAfter.ID);
Gui?.RemoveDoAfter(clientDoAfter.ID);
}
/// <summary>
/// Mark a DoAfter as cancelled and show a cancellation graphic.
/// </summary>
/// Actual removal is handled by DoAfterEntitySystem.
/// <param name="id"></param>
/// <param name="currentTime"></param>
public void Cancel(byte id, TimeSpan? currentTime = null)
{
foreach (var (_, cancelled) in CancelledDoAfters)
{
if (cancelled.ID == id)
return;
}
if (!_doAfters.ContainsKey(id))
return;
var doAfterMessage = _doAfters[id];
currentTime ??= IoCManager.Resolve<IGameTiming>().CurTime;
CancelledDoAfters.Add((currentTime.Value, doAfterMessage));
Gui?.CancelDoAfter(id);
}
}
}

View File

@@ -0,0 +1,148 @@
using System.Linq;
using Content.Shared.Examine;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
namespace Content.Client.DoAfter
{
/// <summary>
/// Handles events that need to happen after a certain amount of time where the event could be cancelled by factors
/// such as moving.
/// </summary>
[UsedImplicitly]
public sealed class DoAfterSystem : EntitySystem
{
/*
* How this is currently setup (client-side):
* DoAfterGui handles the actual bars displayed above heads. It also uses FrameUpdate to flash cancellations
* DoAfterEntitySystem handles checking predictions every tick as well as removing / cancelling DoAfters due to time elapsed.
* DoAfterComponent handles network messages inbound as well as storing the DoAfter data.
* It'll also handle overall cleanup when one is removed (i.e. removing it from DoAfterGui).
*/
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
/// <summary>
/// We'll use an excess time so stuff like finishing effects can show.
/// </summary>
public const float ExcessTime = 0.5f;
private IEntity? _attachedEntity;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerAttachSysMessage>(HandlePlayerAttached);
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeLocalEvent<PlayerAttachSysMessage>();
}
private void HandlePlayerAttached(PlayerAttachSysMessage message)
{
_attachedEntity = message.AttachedEntity;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var currentTime = _gameTiming.CurTime;
// Can't see any I guess?
if (_attachedEntity == null || _attachedEntity.Deleted)
return;
var viewbox = _eyeManager.GetWorldViewport().Enlarged(2.0f);
foreach (var comp in ComponentManager.EntityQuery<DoAfterComponent>(true))
{
var doAfters = comp.DoAfters.ToList();
var compPos = comp.Owner.Transform.WorldPosition;
if (doAfters.Count == 0 ||
comp.Owner.Transform.MapID != _attachedEntity.Transform.MapID ||
!viewbox.Contains(compPos))
{
comp.Disable();
continue;
}
var range = (compPos - _attachedEntity.Transform.WorldPosition).Length +
0.01f;
if (comp.Owner != _attachedEntity &&
!ExamineSystemShared.InRangeUnOccluded(
_attachedEntity.Transform.MapPosition,
comp.Owner.Transform.MapPosition, range,
entity => entity == comp.Owner || entity == _attachedEntity))
{
comp.Disable();
continue;
}
comp.Enable();
var userGrid = comp.Owner.Transform.Coordinates;
// Check cancellations / finishes
foreach (var (id, doAfter) in doAfters)
{
var elapsedTime = (currentTime - doAfter.StartTime).TotalSeconds;
// If we've passed the final time (after the excess to show completion graphic) then remove.
if (elapsedTime > doAfter.Delay + ExcessTime)
{
comp.Remove(doAfter);
continue;
}
// Don't predict cancellation if it's already finished.
if (elapsedTime > doAfter.Delay)
{
continue;
}
// Predictions
if (doAfter.BreakOnUserMove)
{
if (!userGrid.InRange(EntityManager, doAfter.UserGrid, doAfter.MovementThreshold))
{
comp.Cancel(id, currentTime);
continue;
}
}
if (doAfter.BreakOnTargetMove)
{
if (EntityManager.TryGetEntity(doAfter.TargetUid, out var targetEntity) &&
!targetEntity.Transform.Coordinates.InRange(EntityManager, doAfter.TargetGrid,
doAfter.MovementThreshold))
{
comp.Cancel(id, currentTime);
continue;
}
}
}
var count = comp.CancelledDoAfters.Count;
// Remove cancelled DoAfters after ExcessTime has elapsed
for (var i = count - 1; i >= 0; i--)
{
var cancelled = comp.CancelledDoAfters[i];
if ((currentTime - cancelled.CancelTime).TotalSeconds > ExcessTime)
{
comp.Remove(cancelled.Message);
}
}
}
}
}
}

View File

@@ -0,0 +1,136 @@
using System;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Client.DoAfter.UI
{
public sealed class DoAfterBar : Control
{
private IGameTiming _gameTiming = default!;
private readonly ShaderInstance _shader;
/// <summary>
/// Set from 0.0f to 1.0f to reflect bar progress
/// </summary>
public float Ratio
{
get => _ratio;
set => _ratio = value;
}
private float _ratio = 1.0f;
/// <summary>
/// Flash red until removed
/// </summary>
public bool Cancelled
{
get => _cancelled;
set
{
if (_cancelled == value)
{
return;
}
_cancelled = value;
if (_cancelled)
{
_gameTiming = IoCManager.Resolve<IGameTiming>();
_lastFlash = _gameTiming.CurTime;
}
}
}
private bool _cancelled;
/// <summary>
/// Is the cancellation bar red?
/// </summary>
private bool _flash = true;
/// <summary>
/// Last time we swapped the flash.
/// </summary>
private TimeSpan _lastFlash;
/// <summary>
/// How long each cancellation bar flash lasts in seconds.
/// </summary>
private const float FlashTime = 0.125f;
private const int XPixelDiff = 20 * DoAfterBarScale;
public const byte DoAfterBarScale = 2;
public DoAfterBar()
{
IoCManager.InjectDependencies(this);
_shader = IoCManager.Resolve<IPrototypeManager>().Index<ShaderPrototype>("unshaded").Instance();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (Cancelled)
{
if ((_gameTiming.CurTime - _lastFlash).TotalSeconds > FlashTime)
{
_lastFlash = _gameTiming.CurTime;
_flash = !_flash;
}
}
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
Color color;
if (Cancelled)
{
if ((_gameTiming.CurTime - _lastFlash).TotalSeconds > FlashTime)
{
_lastFlash = _gameTiming.CurTime;
_flash = !_flash;
}
color = new Color(1.0f, 0.0f, 0.0f, _flash ? 1.0f : 0.0f);
}
else
{
color = DoAfterHelpers.GetProgressColor(Ratio);
}
handle.UseShader(_shader);
// If you want to make this less hard-coded be my guest
var leftOffset = 2 * DoAfterBarScale;
var box = new UIBox2i(
leftOffset,
-2 + 2 * DoAfterBarScale,
leftOffset + (int) (XPixelDiff * Ratio * UIScale),
-2);
handle.DrawRect(box, color);
}
}
public static class DoAfterHelpers
{
public static Color GetProgressColor(float progress)
{
if (progress >= 1.0f)
{
return new Color(0f, 1f, 0f);
}
// lerp
var hue = (5f / 18f) * progress;
return Color.FromHsv((hue, 1f, 0.75f, 1f));
}
}
}

View File

@@ -0,0 +1,217 @@
using System;
using System.Collections.Generic;
using Content.Client.IoC;
using Content.Client.Resources;
using Content.Shared.DoAfter;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.DoAfter.UI
{
public sealed class DoAfterGui : VBoxContainer
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
private readonly Dictionary<byte, PanelContainer> _doAfterControls = new();
private readonly Dictionary<byte, DoAfterBar> _doAfterBars = new();
// We'll store cancellations for a little bit just so we can flash the graphic to indicate it's cancelled
private readonly Dictionary<byte, TimeSpan> _cancelledDoAfters = new();
public IEntity? AttachedEntity { get; set; }
private ScreenCoordinates _playerPosition;
public DoAfterGui()
{
IoCManager.InjectDependencies(this);
IoCManager.Resolve<IUserInterfaceManager>().StateRoot.AddChild(this);
SeparationOverride = 0;
LayoutContainer.SetGrowVertical(this, LayoutContainer.GrowDirection.Begin);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (Disposed)
return;
foreach (var (_, control) in _doAfterControls)
{
control.Dispose();
}
_doAfterControls.Clear();
_doAfterBars.Clear();
_cancelledDoAfters.Clear();
}
/// <summary>
/// Add the necessary control for a DoAfter progress bar.
/// </summary>
/// <param name="message"></param>
public void AddDoAfter(ClientDoAfter message)
{
if (_doAfterControls.ContainsKey(message.ID))
return;
var doAfterBar = new DoAfterBar
{
VerticalAlignment = VAlignment.Center
};
_doAfterBars[message.ID] = doAfterBar;
var control = new PanelContainer
{
Children =
{
new TextureRect
{
Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/Misc/progress_bar.rsi/icon.png"),
TextureScale = Vector2.One * DoAfterBar.DoAfterBarScale,
VerticalAlignment = VAlignment.Center,
},
doAfterBar
}
};
AddChild(control);
_doAfterControls.Add(message.ID, control);
}
// NOTE THAT THE BELOW ONLY HANDLES THE UI SIDE
/// <summary>
/// Removes a DoAfter without showing a cancel graphic.
/// </summary>
/// <param name="id"></param>
public void RemoveDoAfter(byte id)
{
if (!_doAfterControls.ContainsKey(id))
return;
var control = _doAfterControls[id];
RemoveChild(control);
control.DisposeAllChildren();
_doAfterControls.Remove(id);
_doAfterBars.Remove(id);
_cancelledDoAfters.Remove(id);
}
/// <summary>
/// Cancels a DoAfter and shows a graphic indicating it has been cancelled to the player.
/// </summary>
/// Can be called multiple times on the 1 DoAfter because of the client predicting the cancellation.
/// <param name="id"></param>
public void CancelDoAfter(byte id)
{
if (_cancelledDoAfters.ContainsKey(id))
return;
DoAfterBar doAfterBar;
if (!_doAfterControls.TryGetValue(id, out var doAfterControl))
{
doAfterControl = new PanelContainer();
AddChild(doAfterControl);
DebugTools.Assert(!_doAfterBars.ContainsKey(id));
doAfterBar = new DoAfterBar();
doAfterControl.AddChild(doAfterBar);
_doAfterBars[id] = doAfterBar;
}
else
{
doAfterBar = _doAfterBars[id];
}
doAfterBar.Cancelled = true;
_cancelledDoAfters.Add(id, _gameTiming.CurTime);
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (AttachedEntity?.IsValid() != true ||
!AttachedEntity.TryGetComponent(out DoAfterComponent? doAfterComponent))
{
Visible = false;
return;
}
var doAfters = doAfterComponent.DoAfters;
if (doAfters.Count == 0)
{
Visible = false;
return;
}
if (_eyeManager.CurrentMap != AttachedEntity.Transform.MapID ||
!AttachedEntity.Transform.Coordinates.IsValid(_entityManager))
{
Visible = false;
return;
}
else
{
Visible = true;
}
var currentTime = _gameTiming.CurTime;
var toRemove = new List<byte>();
// Cleanup cancelled DoAfters
foreach (var (id, cancelTime) in _cancelledDoAfters)
{
if ((currentTime - cancelTime).TotalSeconds > DoAfterSystem.ExcessTime)
toRemove.Add(id);
}
foreach (var id in toRemove)
{
RemoveDoAfter(id);
}
toRemove.Clear();
// Update 0 -> 1.0f of the things
foreach (var (id, message) in doAfters)
{
if (_cancelledDoAfters.ContainsKey(id) || !_doAfterControls.ContainsKey(id))
continue;
var doAfterBar = _doAfterBars[id];
var ratio = (currentTime - message.StartTime).TotalSeconds;
doAfterBar.Ratio = MathF.Min(1.0f,
(float) ratio / message.Delay);
// Just in case it doesn't get cleaned up by the system for whatever reason.
if (ratio > message.Delay + DoAfterSystem.ExcessTime)
{
toRemove.Add(id);
}
}
foreach (var id in toRemove)
{
RemoveDoAfter(id);
}
var screenCoordinates = _eyeManager.CoordinatesToScreen(AttachedEntity.Transform.Coordinates);
_playerPosition = new ScreenCoordinates(screenCoordinates.Position / UIScale, screenCoordinates.Window);
LayoutContainer.SetPosition(this, new Vector2(_playerPosition.X - Width / 2, _playerPosition.Y - Height - 30.0f));
}
}
}