[feat]Med huds

This commit is contained in:
rhailrake
2023-04-27 23:54:17 +06:00
committed by Aviu00
parent 518b730864
commit cb5f49f2b3
14 changed files with 508 additions and 43 deletions

View 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;
}
}
}

View 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;
}
}
}

View 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);
}
}
}

View File

@@ -8,7 +8,7 @@ namespace Content.Shared.Overlays;
/// This component allows you to see health bars above damageable mobs.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ShowHealthBarsComponent : Component
public sealed partial class ShowWhiteHealthBarsComponent : Component
{
/// <summary>
/// Displays health bars of the damage containers.

View File

@@ -0,0 +1,15 @@
using Robust.Shared.Prototypes;
namespace Content.Shared.Clothing
{
[RegisterComponent]
public sealed partial class ClothingGrantComponentComponent : Component
{
[DataField("component", required: true)]
[AlwaysPushInheritance]
public ComponentRegistry Components { get; } = new();
[ViewVariables(VVAccess.ReadWrite)]
public bool IsActive = false;
}
}

View File

@@ -0,0 +1,11 @@
namespace Content.Shared.Clothing
{
[RegisterComponent]
public sealed partial class ClothingGrantTagComponent : Component
{
[DataField("tag", required: true), ViewVariables(VVAccess.ReadWrite)]
public string Tag = "";
[ViewVariables(VVAccess.ReadWrite)] public bool IsActive = false;
}
}

View File

@@ -0,0 +1,86 @@
using Content.Shared.Clothing.Components;
using Content.Shared.Inventory.Events;
using Robust.Shared.Serialization.Manager;
using Content.Shared.Tag;
namespace Content.Shared.Clothing;
public sealed class ClothingGrantingSystem : EntitySystem
{
[Dependency] private readonly IComponentFactory _componentFactory = default!;
[Dependency] private readonly ISerializationManager _serializationManager = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ClothingGrantComponentComponent, GotEquippedEvent>(OnCompEquip);
SubscribeLocalEvent<ClothingGrantComponentComponent, GotUnequippedEvent>(OnCompUnequip);
SubscribeLocalEvent<ClothingGrantTagComponent, GotEquippedEvent>(OnTagEquip);
SubscribeLocalEvent<ClothingGrantTagComponent, GotUnequippedEvent>(OnTagUnequip);
}
private void OnCompEquip(EntityUid uid, ClothingGrantComponentComponent component, GotEquippedEvent args)
{
if (!TryComp<ClothingComponent>(uid, out var clothing)) return;
if (!clothing.Slots.HasFlag(args.SlotFlags)) return;
if (component.Components.Count > 1)
{
Logger.Error("Although a component registry supports multiple components, we cannot bookkeep more than 1 component for ClothingGrantComponent at this time.");
return;
}
foreach (var (name, data) in component.Components)
{
var newComp = (Component) _componentFactory.GetComponent(name);
if (HasComp(args.Equipee, newComp.GetType()))
continue;
newComp.Owner = args.Equipee;
var temp = (object) newComp;
_serializationManager.CopyTo(data.Component, ref temp);
EntityManager.AddComponent(args.Equipee, (Component)temp!);
component.IsActive = true;
}
}
private void OnCompUnequip(EntityUid uid, ClothingGrantComponentComponent component, GotUnequippedEvent args)
{
if (!component.IsActive) return;
foreach (var (name, data) in component.Components)
{
var newComp = (Component) _componentFactory.GetComponent(name);
RemComp(args.Equipee, newComp.GetType());
}
component.IsActive = false;
}
private void OnTagEquip(EntityUid uid, ClothingGrantTagComponent component, GotEquippedEvent args)
{
if (!TryComp<ClothingComponent>(uid, out var clothing)) return;
if (!clothing.Slots.HasFlag(args.SlotFlags)) return;
EnsureComp<TagComponent>(args.Equipee);
_tagSystem.AddTag(args.Equipee, component.Tag);
component.IsActive = true;
}
private void OnTagUnequip(EntityUid uid, ClothingGrantTagComponent component, GotUnequippedEvent args)
{
if (!component.IsActive) return;
_tagSystem.RemoveTag(args.Equipee, component.Tag);
component.IsActive = false;
}
}

View File

@@ -0,0 +1,21 @@
using Content.Shared.Damage.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.EntityHealthBar
{
/// <summary>
/// This component allows you to see health bars above damageable mobs.
/// </summary>
[RegisterComponent]
public sealed partial class ShowHealthBarsComponent : Component
{
/// <summary>
/// If null, displays all health bars.
/// If not null, displays health bars of only that damage container.
/// </summary>
[DataField("damageContainers", customTypeSerializer: typeof(PrototypeIdListSerializer<DamageContainerPrototype>))]
public List<string> DamageContainers = new();
}
}

View File

@@ -8,10 +8,16 @@
sprite: Clothing/Eyes/Hud/diag.rsi
- type: Clothing
sprite: Clothing/Eyes/Hud/diag.rsi
- type: ShowHealthBars
damageContainers:
- Inorganic
- Silicon
# - type: ShowHealthBars
# damageContainers:
# - Inorganic
# - Silicon
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Inorganic
- Silicon
- type: entity
parent: ClothingEyesBase
@@ -23,12 +29,17 @@
sprite: Clothing/Eyes/Hud/med.rsi
- type: Clothing
sprite: Clothing/Eyes/Hud/med.rsi
- type: ShowHealthBars
damageContainers:
- Biological
- type: ShowHealthIcons
damageContainers:
- Biological
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- type: entity
parent: ClothingEyesBase
@@ -104,12 +115,17 @@
sprite: Clothing/Eyes/Hud/medonion.rsi
- type: Clothing
sprite: Clothing/Eyes/Hud/medonion.rsi
- type: ShowHealthBars
damageContainers:
- Biological
- type: ShowHealthIcons
damageContainers:
- Biological
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- type: ShowHungerIcons
- type: entity
@@ -122,12 +138,17 @@
sprite: Clothing/Eyes/Hud/medonionbeer.rsi
- type: Clothing
sprite: Clothing/Eyes/Hud/medonionbeer.rsi
- type: ShowHealthBars
damageContainers:
- Biological
- type: ShowHealthIcons
damageContainers:
- Biological
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- type: ShowHungerIcons
- type: ShowThirstIcons
@@ -154,13 +175,19 @@
- type: Clothing
sprite: Clothing/Eyes/Hud/medsecengi.rsi
- type: ShowSecurityIcons
- type: ShowHealthBars
damageContainers:
- Biological
- Inorganic
- type: ShowHealthIcons
damageContainers:
- Biological
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- Inorganic
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - Inorganic
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ShowSyndicateIcons
- type: entity
@@ -174,13 +201,19 @@
- type: Clothing
sprite: Clothing/Eyes/Hud/omni.rsi
- type: ShowSecurityIcons
- type: ShowHealthBars
damageContainers:
- Biological
- Inorganic
- type: ShowHealthIcons
damageContainers:
- Biological
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - Inorganic
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ClothingGrantComponent
component:
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- Inorganic
- type: ShowHungerIcons
- type: ShowThirstIcons
- type: ShowSyndicateIcons

View File

@@ -349,13 +349,16 @@
interactFailureString: petting-failure-medibot
interactSuccessSound:
path: /Audio/Ambience/Objects/periodic_beep.ogg
- type: ShowHealthBars
damageContainers:
# - type: ShowHealthBars
# damageContainers:
# - Biological
# - type: ShowHealthIcons
# damageContainers:
# - Biological
- type: ShowWhiteHealthBars
damageContainers:
- Biological
- type: ShowHealthIcons
damageContainers:
- Biological
- type: entity
parent: MobSiliconBase
id: MobMimeBot

View File

@@ -801,6 +801,7 @@
- MedkitAdvanced
- MedkitRadiation
- MedkitCombat
- ClothingEyesHudMedical
dynamicRecipes:
- HandheldCrewMonitor
- ClothingHandsGlovesNitrile

View File

@@ -781,3 +781,13 @@
Steel: 100
Glass: 900
Gold: 100
- type: latheRecipe
id: ClothingEyesHudMedical
icon: { sprite: Clothing/Eyes/Hud/med.rsi, state: icon }
result: ClothingEyesHudMedical
completetime: 4
materials:
Steel: 100
Glass: 300
Plasma: 200

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

View File

@@ -0,0 +1,14 @@
{
"version": 1,
"size": {
"y": 7,
"x": 24
},
"license": "CC-BY-SA-3.0",
"copyright": "https://github.com/tgstation/tgstation/blob/886ca0f8dddf83ecaf10c92ff106172722352192/icons/effects/progessbar.dmi",
"states": [
{
"name": "icon"
}
]
}