NPC refactor (#10122)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
7
Content.Client/NPC/HTN/HTNComponent.cs
Normal file
7
Content.Client/NPC/HTN/HTNComponent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Content.Client.NPC.HTN;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class HTNComponent : NPCComponent
|
||||
{
|
||||
public string DebugText = string.Empty;
|
||||
}
|
||||
41
Content.Client/NPC/HTN/HTNOverlay.cs
Normal file
41
Content.Client/NPC/HTN/HTNOverlay.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Shared.Enums;
|
||||
|
||||
namespace Content.Client.NPC.HTN;
|
||||
|
||||
public sealed class HTNOverlay : Overlay
|
||||
{
|
||||
private readonly IEntityManager _entManager = default!;
|
||||
private readonly Font _font = default!;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.ScreenSpace;
|
||||
|
||||
public HTNOverlay(IEntityManager entManager, IResourceCache resourceCache)
|
||||
{
|
||||
_entManager = entManager;
|
||||
_font = new VectorFont(resourceCache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Regular.ttf"), 10);
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
if (args.ViewportControl == null)
|
||||
return;
|
||||
|
||||
var handle = args.ScreenHandle;
|
||||
|
||||
foreach (var (comp, xform) in _entManager.EntityQuery<HTNComponent, TransformComponent>(true))
|
||||
{
|
||||
if (string.IsNullOrEmpty(comp.DebugText) || xform.MapID != args.MapId)
|
||||
continue;
|
||||
|
||||
var worldPos = xform.WorldPosition;
|
||||
|
||||
if (!args.WorldAABB.Contains(worldPos))
|
||||
continue;
|
||||
|
||||
var screenPos = args.ViewportControl.WorldToScreen(worldPos);
|
||||
handle.DrawString(_font, screenPos + new Vector2(0, 10f), comp.DebugText);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Content.Client/NPC/HTN/HTNSystem.cs
Normal file
54
Content.Client/NPC/HTN/HTNSystem.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Content.Shared.NPC;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
|
||||
namespace Content.Client.NPC.HTN;
|
||||
|
||||
public sealed class HTNSystem : EntitySystem
|
||||
{
|
||||
/*
|
||||
* Mainly handles clientside debugging for HTN NPCs.
|
||||
*/
|
||||
public bool EnableOverlay
|
||||
{
|
||||
get => _enableOverlay;
|
||||
set
|
||||
{
|
||||
var overlayManager = IoCManager.Resolve<IOverlayManager>();
|
||||
_enableOverlay = value;
|
||||
|
||||
if (_enableOverlay)
|
||||
{
|
||||
overlayManager.AddOverlay(new HTNOverlay(EntityManager, IoCManager.Resolve<IResourceCache>()));
|
||||
RaiseNetworkEvent(new RequestHTNMessage()
|
||||
{
|
||||
Enabled = true,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
overlayManager.RemoveOverlay<HTNOverlay>();
|
||||
RaiseNetworkEvent(new RequestHTNMessage()
|
||||
{
|
||||
Enabled = false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool _enableOverlay;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeNetworkEvent<HTNMessage>(OnHTNMessage);
|
||||
}
|
||||
|
||||
private void OnHTNMessage(HTNMessage ev)
|
||||
{
|
||||
if (!TryComp<HTNComponent>(ev.Uid, out var htn))
|
||||
return;
|
||||
|
||||
htn.DebugText = ev.Text;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user