Radiation rework (#10970)
This commit is contained in:
@@ -15,7 +15,7 @@ using Content.Client.MainMenu;
|
||||
using Content.Client.Parallax.Managers;
|
||||
using Content.Client.Players.PlayTimeTracking;
|
||||
using Content.Client.Preferences;
|
||||
using Content.Client.Radiation;
|
||||
using Content.Client.Radiation.Overlays;
|
||||
using Content.Client.Screenshot;
|
||||
using Content.Client.Singularity;
|
||||
using Content.Client.Stylesheets;
|
||||
|
||||
131
Content.Client/Radiation/Overlays/RadiationDebugOverlay.cs
Normal file
131
Content.Client/Radiation/Overlays/RadiationDebugOverlay.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System.Linq;
|
||||
using Content.Client.Radiation.Systems;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Client.Radiation.Overlays;
|
||||
|
||||
public sealed class RadiationDebugOverlay : Overlay
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
private readonly RadiationSystem _radiation;
|
||||
|
||||
private readonly Font _font;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpace | OverlaySpace.ScreenSpace;
|
||||
|
||||
public RadiationDebugOverlay()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
_radiation = _entityManager.System<RadiationSystem>();
|
||||
|
||||
var cache = IoCManager.Resolve<IResourceCache>();
|
||||
_font = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Regular.ttf"), 8);
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
switch (args.Space)
|
||||
{
|
||||
case OverlaySpace.ScreenSpace:
|
||||
DrawScreenRays(args);
|
||||
DrawScreenResistance(args);
|
||||
break;
|
||||
case OverlaySpace.WorldSpace:
|
||||
DrawWorld(args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawScreenRays(OverlayDrawArgs args)
|
||||
{
|
||||
var rays = _radiation.Rays;
|
||||
if (rays == null || args.ViewportControl == null)
|
||||
return;
|
||||
|
||||
var handle = args.ScreenHandle;
|
||||
foreach (var ray in rays)
|
||||
{
|
||||
if (ray.MapId != args.MapId)
|
||||
continue;
|
||||
|
||||
if (ray.ReachedDestination)
|
||||
{
|
||||
var screenCenter = args.ViewportControl.WorldToScreen(ray.Destination);
|
||||
handle.DrawString(_font, screenCenter, ray.Rads.ToString("F2"), 2f, Color.White);
|
||||
}
|
||||
|
||||
foreach (var (gridUid, blockers) in ray.Blockers)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(gridUid, out var grid))
|
||||
continue;
|
||||
|
||||
foreach (var (tile, rads) in blockers)
|
||||
{
|
||||
var worldPos = grid.GridTileToWorldPos(tile);
|
||||
var screenCenter = args.ViewportControl.WorldToScreen(worldPos);
|
||||
handle.DrawString(_font, screenCenter, rads.ToString("F2"), 1.5f, Color.White);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawScreenResistance(OverlayDrawArgs args)
|
||||
{
|
||||
var resistance = _radiation.ResistanceGrids;
|
||||
if (resistance == null || args.ViewportControl == null)
|
||||
return;
|
||||
|
||||
var handle = args.ScreenHandle;
|
||||
var query = _entityManager.GetEntityQuery<TransformComponent>();
|
||||
foreach (var (gridUid, resMap) in resistance)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(gridUid, out var grid))
|
||||
continue;
|
||||
if (query.TryGetComponent(gridUid, out var trs) && trs.MapID != args.MapId)
|
||||
continue;
|
||||
|
||||
var offset = new Vector2(grid.TileSize, -grid.TileSize) * 0.25f;
|
||||
foreach (var (tile, value) in resMap)
|
||||
{
|
||||
var localPos = grid.GridTileToLocal(tile).Position + offset;
|
||||
var worldPos = grid.LocalToWorld(localPos);
|
||||
var screenCenter = args.ViewportControl.WorldToScreen(worldPos);
|
||||
handle.DrawString(_font, screenCenter, value.ToString("F2"), color: Color.White);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawWorld(in OverlayDrawArgs args)
|
||||
{
|
||||
var rays = _radiation.Rays;
|
||||
if (rays == null)
|
||||
return;
|
||||
|
||||
var handle = args.WorldHandle;
|
||||
// draw lines for raycasts
|
||||
foreach (var ray in rays)
|
||||
{
|
||||
if (ray.MapId != args.MapId)
|
||||
continue;
|
||||
|
||||
if (ray.ReachedDestination)
|
||||
{
|
||||
handle.DrawLine(ray.Source, ray.Destination, Color.Red);
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var (gridUid, blockers) in ray.Blockers)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(gridUid, out var grid))
|
||||
continue;
|
||||
var (destTile, _) = blockers.Last();
|
||||
var destWorld = grid.GridTileToWorldPos(destTile);
|
||||
handle.DrawLine(ray.Source, destWorld, Color.Red);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Radiation
|
||||
namespace Content.Client.Radiation.Overlays
|
||||
{
|
||||
public sealed class RadiationPulseOverlay : Overlay
|
||||
{
|
||||
54
Content.Client/Radiation/Systems/RadiationSystem.cs
Normal file
54
Content.Client/Radiation/Systems/RadiationSystem.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Content.Client.Radiation.Overlays;
|
||||
using Content.Shared.Radiation.Events;
|
||||
using Content.Shared.Radiation.Systems;
|
||||
using Robust.Client.Graphics;
|
||||
|
||||
namespace Content.Client.Radiation.Systems;
|
||||
|
||||
public sealed class RadiationSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IOverlayManager _overlayMan = default!;
|
||||
|
||||
public List<RadiationRay>? Rays;
|
||||
public Dictionary<EntityUid, Dictionary<Vector2i, float>>? ResistanceGrids;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeNetworkEvent<OnRadiationOverlayToggledEvent>(OnOverlayToggled);
|
||||
SubscribeNetworkEvent<OnRadiationOverlayUpdateEvent>(OnOverlayUpdate);
|
||||
SubscribeNetworkEvent<OnRadiationOverlayResistanceUpdateEvent>(OnResistanceUpdate);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_overlayMan.RemoveOverlay<RadiationDebugOverlay>();
|
||||
}
|
||||
|
||||
private void OnOverlayToggled(OnRadiationOverlayToggledEvent ev)
|
||||
{
|
||||
if (ev.IsEnabled)
|
||||
_overlayMan.AddOverlay(new RadiationDebugOverlay());
|
||||
else
|
||||
_overlayMan.RemoveOverlay<RadiationDebugOverlay>();
|
||||
}
|
||||
|
||||
private void OnOverlayUpdate(OnRadiationOverlayUpdateEvent ev)
|
||||
{
|
||||
if (!_overlayMan.TryGetOverlay(out RadiationDebugOverlay? overlay))
|
||||
return;
|
||||
|
||||
var str = $"Radiation update: {ev.ElapsedTimeMs}ms with. Receivers: {ev.ReceiversCount}, " +
|
||||
$"Sources: {ev.SourcesCount}, Rays: {ev.Rays.Count}";
|
||||
Logger.Info(str);
|
||||
|
||||
Rays = ev.Rays;
|
||||
}
|
||||
|
||||
private void OnResistanceUpdate(OnRadiationOverlayResistanceUpdateEvent ev)
|
||||
{
|
||||
if (!_overlayMan.TryGetOverlay(out RadiationDebugOverlay? overlay))
|
||||
return;
|
||||
ResistanceGrids = ev.Grids;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user