Files
RobustToolbox/Robust.Client/Debugging/DebugDrawingManager.cs
Víctor Aguilera Puerto fbaf3297b9 Fix compile
2020-07-15 11:44:36 +02:00

113 lines
3.5 KiB
C#

using Robust.Client.Interfaces.Debugging;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using System;
using System.Collections.Generic;
using Robust.Client.Graphics.Overlays;
using Robust.Client.Graphics.Drawing;
using Robust.Shared.Maths;
using Robust.Client.Interfaces.Graphics.Overlays;
using Robust.Shared.Timing;
using Robust.Shared.Interfaces.Timing;
namespace Robust.Client.Debugging
{
internal class DebugDrawingManager : IDebugDrawingManager
{
[Dependency] private readonly IClientNetManager _net = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IGameTiming _gameTimer = default!;
private readonly List<RayWithLifetime> raysWithLifeTime = new List<RayWithLifetime>();
private bool _debugDrawRays;
private struct RayWithLifetime
{
public Vector2 RayOrigin;
public Vector2 RayHit;
public TimeSpan LifeTime;
public bool DidActuallyHit;
}
public bool DebugDrawRays
{
get => _debugDrawRays;
set
{
if (value == DebugDrawRays)
{
return;
}
_debugDrawRays = value;
if (value)
{
_overlayManager.AddOverlay(new DebugDrawRayOverlay(this));
}
else
{
_overlayManager.RemoveOverlay(nameof(DebugDrawRayOverlay));
}
}
}
public TimeSpan DebugRayLifetime { get; set; } = TimeSpan.FromSeconds(5);
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME, HandleDrawRay);
}
private void HandleDrawRay(MsgRay msg)
{
// Rays are disposed with DebugDrawRayOverlay.FrameUpdate.
// We don't accept incoming rays when that's off because else they'd pile up constantly.
if (!_debugDrawRays)
{
return;
}
var newRayWithLifetime = new RayWithLifetime
{
DidActuallyHit = msg.DidHit,
RayOrigin = msg.RayOrigin,
RayHit = msg.RayHit,
LifeTime = _gameTimer.RealTime + DebugRayLifetime
};
raysWithLifeTime.Add(newRayWithLifetime);
}
private sealed class DebugDrawRayOverlay : Overlay
{
private readonly DebugDrawingManager _owner;
public override OverlaySpace Space => OverlaySpace.WorldSpace;
public DebugDrawRayOverlay(DebugDrawingManager owner) : base(nameof(DebugDrawRayOverlay))
{
_owner = owner;
}
protected override void Draw(DrawingHandleBase handle)
{
foreach (var ray in _owner.raysWithLifeTime)
{
handle.DrawLine(
ray.RayOrigin,
ray.RayHit,
ray.DidActuallyHit ? Color.Yellow : Color.Magenta);
}
}
protected internal override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
_owner.raysWithLifeTime.RemoveAll(r => r.LifeTime < _owner._gameTimer.RealTime);
}
}
}
}