[feat]Med huds
This commit is contained in:
44
Content.Client/Commands/ToggleHealthBarsCommand.cs
Normal file
44
Content.Client/Commands/ToggleHealthBarsCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.Console;
|
||||
using Content.Shared.EntityHealthBar;
|
||||
|
||||
namespace Content.Client.Commands
|
||||
{
|
||||
public sealed class ToggleHealthBarsCommand : IConsoleCommand
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
public string Command => "togglehealthbars";
|
||||
public string Description => "Toggles a health bar above mobs.";
|
||||
public string Help => $"Usage: {Command}";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var player = _playerManager.LocalPlayer;
|
||||
if (player == null)
|
||||
{
|
||||
shell.WriteLine("You aren't a player.");
|
||||
return;
|
||||
}
|
||||
|
||||
var playerEntity = player?.ControlledEntity;
|
||||
if (playerEntity == null)
|
||||
{
|
||||
shell.WriteLine("You do not have an attached entity.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_entityManager.TryGetComponent<ShowHealthBarsComponent>(playerEntity, out var glassComp))
|
||||
{
|
||||
_entityManager.AddComponent<ShowHealthBarsComponent>((EntityUid) playerEntity);
|
||||
shell.WriteLine("Enabled health overlay.");
|
||||
return;
|
||||
}
|
||||
|
||||
_entityManager.RemoveComponent<ShowHealthBarsComponent>((EntityUid) playerEntity);
|
||||
shell.WriteLine("Disabled health overlay.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
163
Content.Client/White/EntityHealthBar/EntityHealthBarOverlay.cs
Normal file
163
Content.Client/White/EntityHealthBar/EntityHealthBarOverlay.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Mobs;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.EntityHealthBar;
|
||||
|
||||
/// <summary>
|
||||
/// Yeah a lot of this is duplicated from doafters.
|
||||
/// Not much to be done until there's a generic HUD system
|
||||
/// </summary>
|
||||
public sealed class EntityHealthBarOverlay : Overlay
|
||||
{
|
||||
private readonly IEntityManager _entManager;
|
||||
private readonly SharedTransformSystem _transform;
|
||||
private readonly MobStateSystem _mobStateSystem;
|
||||
private readonly MobThresholdSystem _mobThresholdSystem;
|
||||
private readonly Texture _barTexture;
|
||||
private readonly ShaderInstance _shader;
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowFOV;
|
||||
public List<string> DamageContainers = new();
|
||||
|
||||
public EntityHealthBarOverlay(IEntityManager entManager, IPrototypeManager protoManager)
|
||||
{
|
||||
_entManager = entManager;
|
||||
_transform = _entManager.EntitySysManager.GetEntitySystem<SharedTransformSystem>();
|
||||
_mobStateSystem = _entManager.EntitySysManager.GetEntitySystem<MobStateSystem>();
|
||||
_mobThresholdSystem = _entManager.EntitySysManager.GetEntitySystem<MobThresholdSystem>();
|
||||
|
||||
var sprite = new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/health_bar.rsi"), "icon");
|
||||
_barTexture = _entManager.EntitySysManager.GetEntitySystem<SpriteSystem>().Frame0(sprite);
|
||||
|
||||
_shader = protoManager.Index<ShaderPrototype>("shaded").Instance();
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
var handle = args.WorldHandle;
|
||||
var rotation = args.Viewport.Eye?.Rotation ?? Angle.Zero;
|
||||
var spriteQuery = _entManager.GetEntityQuery<SpriteComponent>();
|
||||
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
|
||||
|
||||
const float scale = 1f;
|
||||
var scaleMatrix = Matrix3.CreateScale(new Vector2(scale, scale));
|
||||
var rotationMatrix = Matrix3.CreateRotation(-rotation);
|
||||
handle.UseShader(_shader);
|
||||
|
||||
foreach (var (mob, dmg) in _entManager.EntityQuery<MobStateComponent, DamageableComponent>(true))
|
||||
{
|
||||
if (!xformQuery.TryGetComponent(mob.Owner, out var xform) ||
|
||||
xform.MapID != args.MapId)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dmg.DamageContainerID == null || !DamageContainers.Contains(dmg.DamageContainerID))
|
||||
continue;
|
||||
|
||||
var worldPosition = _transform.GetWorldPosition(xform);
|
||||
var worldMatrix = Matrix3.CreateTranslation(worldPosition);
|
||||
|
||||
Matrix3.Multiply(scaleMatrix, worldMatrix, out var scaledWorld);
|
||||
Matrix3.Multiply(rotationMatrix, scaledWorld, out var matty);
|
||||
|
||||
handle.SetTransform(matty);
|
||||
|
||||
// Use the sprite itself if we know its bounds. This means short or tall sprites don't get overlapped
|
||||
// by the bar.
|
||||
float yOffset;
|
||||
if (spriteQuery.TryGetComponent(mob.Owner, out var sprite))
|
||||
{
|
||||
yOffset = sprite.Bounds.Height + 15f;
|
||||
}
|
||||
else
|
||||
{
|
||||
yOffset = 1f;
|
||||
}
|
||||
|
||||
// Position above the entity (we've already applied the matrix transform to the entity itself)
|
||||
// Offset by the texture size for every do_after we have.
|
||||
var position = new Vector2(-_barTexture.Width / 2f / EyeManager.PixelsPerMeter,
|
||||
yOffset / EyeManager.PixelsPerMeter);
|
||||
|
||||
// Draw the underlying bar texture
|
||||
handle.DrawTexture(_barTexture, position);
|
||||
|
||||
// we are all progressing towards death every day
|
||||
(float ratio, bool inCrit) deathProgress = CalcProgress(mob.Owner, mob, dmg);
|
||||
|
||||
var color = GetProgressColor(deathProgress.ratio, deathProgress.inCrit);
|
||||
|
||||
// Hardcoded width of the progress bar because it doesn't match the texture.
|
||||
const float startX = 2f;
|
||||
const float endX = 22f;
|
||||
|
||||
var xProgress = (endX - startX) * deathProgress.ratio + startX;
|
||||
|
||||
var box = new Box2(new Vector2(startX, 3f) / EyeManager.PixelsPerMeter, new Vector2(xProgress, 4f) / EyeManager.PixelsPerMeter);
|
||||
box = box.Translated(position);
|
||||
handle.DrawRect(box, color);
|
||||
}
|
||||
|
||||
handle.UseShader(null);
|
||||
handle.SetTransform(Matrix3.Identity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a ratio between 0 and 1, and whether the entity is in crit.
|
||||
/// </summary>
|
||||
private (float, bool) CalcProgress(EntityUid uid, MobStateComponent component, DamageableComponent dmg)
|
||||
{
|
||||
if (_mobStateSystem.IsAlive(uid, component))
|
||||
{
|
||||
if (!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Critical, out var threshold))
|
||||
return (1, false);
|
||||
|
||||
var ratio = 1 - ((FixedPoint2)(dmg.TotalDamage / threshold)).Float();
|
||||
return (ratio, false);
|
||||
}
|
||||
|
||||
if (_mobStateSystem.IsCritical(uid, component))
|
||||
{
|
||||
if (!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Critical, out var critThreshold) ||
|
||||
!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Dead, out var deadThreshold))
|
||||
{
|
||||
return (1, true);
|
||||
}
|
||||
|
||||
var ratio = 1 -
|
||||
((dmg.TotalDamage - critThreshold) /
|
||||
(deadThreshold - critThreshold)).Value.Float();
|
||||
|
||||
return (ratio, true);
|
||||
}
|
||||
|
||||
return (0, true);
|
||||
}
|
||||
|
||||
public static Color GetProgressColor(float progress, bool crit)
|
||||
{
|
||||
if (progress >= 1.0f)
|
||||
{
|
||||
return new Color(0f, 1f, 0f);
|
||||
}
|
||||
// lerp
|
||||
if (!crit)
|
||||
{
|
||||
var hue = (5f / 18f) * progress;
|
||||
return Color.FromHsv((hue, 1f, 0.75f, 1f));
|
||||
}
|
||||
else
|
||||
{
|
||||
return Color.Red;
|
||||
}
|
||||
}
|
||||
}
|
||||
64
Content.Client/White/EntityHealthBar/ShowHealthBarsSystem.cs
Normal file
64
Content.Client/White/EntityHealthBar/ShowHealthBarsSystem.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Content.Shared.EntityHealthBar;
|
||||
using Content.Shared.GameTicking;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client.EntityHealthBar
|
||||
{
|
||||
public sealed class ShowHealthBarsSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _player = default!;
|
||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||
[Dependency] private readonly IOverlayManager _overlayMan = default!;
|
||||
|
||||
private EntityHealthBarOverlay _overlay = default!;
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ShowHealthBarsComponent, ComponentInit>(OnInit);
|
||||
SubscribeLocalEvent<ShowHealthBarsComponent, ComponentRemove>(OnRemove);
|
||||
SubscribeLocalEvent<ShowHealthBarsComponent, PlayerAttachedEvent>(OnPlayerAttached);
|
||||
SubscribeLocalEvent<ShowHealthBarsComponent, PlayerDetachedEvent>(OnPlayerDetached);
|
||||
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
|
||||
|
||||
_overlay = new(EntityManager, _protoMan);
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, ShowHealthBarsComponent component, ComponentInit args)
|
||||
{
|
||||
if (_player.LocalPlayer?.ControlledEntity == uid)
|
||||
{
|
||||
_overlayMan.AddOverlay(_overlay);
|
||||
_overlay.DamageContainers = component.DamageContainers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
private void OnRemove(EntityUid uid, ShowHealthBarsComponent component, ComponentRemove args)
|
||||
{
|
||||
if (_player.LocalPlayer?.ControlledEntity == uid)
|
||||
{
|
||||
_overlayMan.RemoveOverlay(_overlay);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerAttached(EntityUid uid, ShowHealthBarsComponent component, PlayerAttachedEvent args)
|
||||
{
|
||||
_overlayMan.AddOverlay(_overlay);
|
||||
_overlay.DamageContainers = component.DamageContainers;
|
||||
}
|
||||
|
||||
private void OnPlayerDetached(EntityUid uid, ShowHealthBarsComponent component, PlayerDetachedEvent args)
|
||||
{
|
||||
_overlayMan.RemoveOverlay(_overlay);
|
||||
}
|
||||
|
||||
private void OnRoundRestart(RoundRestartCleanupEvent args)
|
||||
{
|
||||
_overlayMan.RemoveOverlay(_overlay);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user