mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-06-09 10:06:34 +02:00
83c2a1be11
* [Dependency] source generator No more reflection, no more codegen at runtime Also various changes to Roslyn helpers to make this easier to write. Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback. No fallback is available for [field: Dependency] properties, due to a Roslyn bug. Code Fixes exist. We love Roslyn * Apply dependencies generator changes to all code * Release notes * Preprocessor got hands * Handle nullable dependencies These are bad but gotta deal with it. * Apply suggestions from code review Co-authored-by: Moony <moony@hellomouse.net> * Fine, let's not use collection expressions --------- Co-authored-by: Moony <moony@hellomouse.net>
150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Shared.Console;
|
|
using Robust.Shared.Enums;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Physics;
|
|
using Robust.Shared.Physics.Components;
|
|
using Robust.Shared.Physics.Systems;
|
|
using Robust.Shared.Timing;
|
|
using TerraFX.Interop.DirectX;
|
|
|
|
namespace Robust.Client.Physics
|
|
{
|
|
internal sealed partial class PhysicsIslandCommand : LocalizedCommands
|
|
{
|
|
[Dependency] private IEntitySystemManager _entitySystemManager = default!;
|
|
|
|
public override string Command => "showislands";
|
|
|
|
public override void Execute(IConsoleShell shell, string argStr, string[] args)
|
|
{
|
|
if (args.Length != 0)
|
|
{
|
|
shell.WriteLine("This command doesn't take args!");
|
|
return;
|
|
}
|
|
|
|
var system = _entitySystemManager.GetEntitySystem<DebugPhysicsIslandSystem>();
|
|
system.Mode ^= DebugPhysicsIslandMode.Solve;
|
|
}
|
|
}
|
|
|
|
internal sealed partial class DebugPhysicsIslandSystem : EntitySystem
|
|
{
|
|
[Dependency] private IGameTiming _gameTiming = default!;
|
|
[Dependency] private IEyeManager _eyeManager = default!;
|
|
[Dependency] private IOverlayManager _overlayManager = default!;
|
|
[Dependency] private EntityLookupSystem _lookup = default!;
|
|
|
|
public DebugPhysicsIslandMode Mode { get; set; } = DebugPhysicsIslandMode.None;
|
|
|
|
/*
|
|
* Island solve debug:
|
|
* This will draw above every body involved in a particular island solve.
|
|
*/
|
|
|
|
public readonly Queue<(TimeSpan Time, List<Entity<PhysicsComponent, TransformComponent>> Bodies)> IslandSolve = new();
|
|
public const float SolveDuration = 0.1f;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<IslandSolveMessage>(HandleIslandSolveMessage);
|
|
|
|
var overlay = new PhysicsIslandOverlay(this, _lookup, _gameTiming, _eyeManager);
|
|
_overlayManager.AddOverlay(overlay);
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
|
|
_overlayManager.RemoveOverlay(typeof(PhysicsIslandOverlay));
|
|
}
|
|
|
|
public override void FrameUpdate(float frameTime)
|
|
{
|
|
base.FrameUpdate(frameTime);
|
|
while (IslandSolve.TryPeek(out var solve))
|
|
{
|
|
if (solve.Time.TotalSeconds + SolveDuration > _gameTiming.CurTime.TotalSeconds)
|
|
{
|
|
IslandSolve.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleIslandSolveMessage(IslandSolveMessage message)
|
|
{
|
|
if ((Mode & DebugPhysicsIslandMode.Solve) == 0x0) return;
|
|
IslandSolve.Enqueue((_gameTiming.CurTime, message.Bodies));
|
|
}
|
|
}
|
|
|
|
[Flags]
|
|
internal enum DebugPhysicsIslandMode : ushort
|
|
{
|
|
None = 0,
|
|
Solve = 1 << 0,
|
|
Contacts = 1 << 1,
|
|
}
|
|
|
|
internal sealed class PhysicsIslandOverlay : Overlay
|
|
{
|
|
private readonly IEyeManager _eyeManager;
|
|
private readonly IGameTiming _gameTiming;
|
|
private readonly DebugPhysicsIslandSystem _islandSystem;
|
|
private readonly EntityLookupSystem _lookup;
|
|
|
|
public override OverlaySpace Space => OverlaySpace.WorldSpace;
|
|
|
|
public PhysicsIslandOverlay(DebugPhysicsIslandSystem system, EntityLookupSystem lookup, IGameTiming gameTiming, IEyeManager eyeManager)
|
|
{
|
|
_islandSystem = system;
|
|
_lookup = lookup;
|
|
_gameTiming = gameTiming;
|
|
_eyeManager = eyeManager;
|
|
}
|
|
|
|
protected internal override void Draw(in OverlayDrawArgs args)
|
|
{
|
|
var worldHandle = (DrawingHandleWorld) args.DrawingHandle;
|
|
|
|
DrawIslandSolve(worldHandle);
|
|
}
|
|
|
|
private void DrawIslandSolve(DrawingHandleWorld handle)
|
|
{
|
|
if ((_islandSystem.Mode & DebugPhysicsIslandMode.Solve) == 0x0) return;
|
|
|
|
var viewport = _eyeManager.GetWorldViewport();
|
|
|
|
foreach (var solve in _islandSystem.IslandSolve)
|
|
{
|
|
var ratio = (float) Math.Max(
|
|
(solve.Time.TotalSeconds + DebugPhysicsIslandSystem.SolveDuration -
|
|
_gameTiming.CurTime.TotalSeconds) / DebugPhysicsIslandSystem.SolveDuration, 0.0f);
|
|
|
|
if (ratio <= 0.0f) continue;
|
|
|
|
foreach (var body in solve.Bodies)
|
|
{
|
|
var worldAABB = _lookup.GetWorldAABB(body.Owner);
|
|
if (!viewport.Intersects(worldAABB)) continue;
|
|
|
|
handle.DrawRect(worldAABB, Color.Green.WithAlpha(ratio * 0.5f));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|