-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3028e7
commit 1265932
Showing
5 changed files
with
301 additions
and
12 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
Content.Client/EntityHealthBar/EntityHealthBarOverlay.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
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 Content.Shared.Interaction; | ||
using Content.Shared.Physics; | ||
using Robust.Client.GameObjects; | ||
using Robust.Client.Graphics; | ||
using Robust.Client.Player; | ||
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 | ||
{ | ||
[Dependency] | ||
private readonly IEntityManager _entManager = default!; | ||
[Dependency] | ||
private readonly IPrototypeManager _protoManager = default!; | ||
|
||
private readonly SharedTransformSystem _transform; | ||
private readonly MobStateSystem _mobStateSystem; | ||
private readonly MobThresholdSystem _mobThresholdSystem; | ||
private readonly Texture _barTexture; | ||
private readonly ShaderInstance _shader; | ||
private readonly SharedInteractionSystem _interaction; | ||
|
||
[Dependency] | ||
private readonly IPlayerManager _playerManager = default!; | ||
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowFOV; | ||
public List<string> DamageContainers = new(); | ||
|
||
public EntityHealthBarOverlay() | ||
{ | ||
IoCManager.InjectDependencies(this); | ||
|
||
_transform = _entManager.System<SharedTransformSystem>(); | ||
_mobStateSystem = _entManager.System<MobStateSystem>(); | ||
_mobThresholdSystem = _entManager.System<MobThresholdSystem>(); | ||
_interaction = _entManager.System<SharedInteractionSystem>(); | ||
|
||
var sprite = new SpriteSpecifier.Rsi(new ("/Textures/Interface/Misc/health_bar.rsi"), "icon"); | ||
_barTexture = _entManager.EntitySysManager.GetEntitySystem<SpriteSystem>().Frame0(sprite); | ||
|
||
_shader = _protoManager.Index<ShaderPrototype>("unshaded").Instance(); | ||
} | ||
|
||
protected override void Draw(in OverlayDrawArgs args) | ||
{ | ||
if (_playerManager.LocalEntity == null) | ||
{ | ||
return; | ||
} | ||
|
||
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); | ||
|
||
var q = _entManager.AllEntityQueryEnumerator<MobThresholdsComponent, MobStateComponent, DamageableComponent>(); | ||
while (q.MoveNext(out var owner, out var thresholds, out var mob, out var dmg)) | ||
{ | ||
if (!xformQuery.TryGetComponent(owner, out var xform) || | ||
xform.MapID != args.MapId) | ||
{ | ||
continue; | ||
} | ||
|
||
if (dmg.DamageContainerID == null || !DamageContainers.Contains(dmg.DamageContainerID)) | ||
continue; | ||
|
||
if (!_interaction.InRangeUnobstructed(_playerManager.LocalEntity.Value, owner, range: 30f, collisionMask: CollisionGroup.Opaque)) | ||
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); | ||
|
||
float yOffset; | ||
if (spriteQuery.TryGetComponent(owner, out var sprite)) | ||
{ | ||
yOffset = sprite.Bounds.Height + 15f; | ||
} | ||
else | ||
{ | ||
yOffset = 1f; | ||
} | ||
|
||
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(owner, mob, dmg, thresholds); | ||
|
||
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, MobThresholdsComponent thresholds) | ||
{ | ||
if (_mobStateSystem.IsAlive(uid, component)) | ||
{ | ||
if (!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Critical, out var threshold, thresholds)) | ||
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, thresholds) || | ||
!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Dead, out var deadThreshold, thresholds)) | ||
{ | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Content.Shared.EntityHealthBar; | ||
using Content.Shared.GameTicking; | ||
using Robust.Client.Graphics; | ||
using Robust.Client.Player; | ||
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 IOverlayManager _overlayMan = default!; | ||
|
||
private EntityHealthBarOverlay _overlay = default!; | ||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
SubscribeLocalEvent<ShowHealthBarsComponent, ComponentInit>(OnInit); | ||
SubscribeLocalEvent<ShowHealthBarsComponent, ComponentRemove>(OnRemove); | ||
SubscribeLocalEvent<ShowHealthBarsComponent, AfterAutoHandleStateEvent>(OnUpdate); | ||
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart); | ||
|
||
_player.LocalPlayerAttached += OnPlayerAttached; | ||
_player.LocalPlayerDetached += OnPlayerDetached; | ||
|
||
_overlay = new(); | ||
} | ||
|
||
public override void Shutdown() | ||
{ | ||
base.Shutdown(); | ||
|
||
_player.LocalPlayerAttached -= OnPlayerAttached; | ||
_player.LocalPlayerDetached -= OnPlayerDetached; | ||
} | ||
|
||
private void OnUpdate(Entity<ShowHealthBarsComponent> ent, ref AfterAutoHandleStateEvent args) | ||
{ | ||
_overlay.DamageContainers.Clear(); | ||
_overlay.DamageContainers.AddRange(ent.Comp.DamageContainers); | ||
} | ||
|
||
private void OnInit(EntityUid uid, ShowHealthBarsComponent component, ComponentInit args) | ||
{ | ||
if (_player.LocalSession?.AttachedEntity == uid) | ||
{ | ||
ApplyOverlays(component); | ||
} | ||
} | ||
|
||
private void OnRemove(EntityUid uid, ShowHealthBarsComponent component, ComponentRemove args) | ||
{ | ||
if (_player.LocalSession?.AttachedEntity == uid) | ||
{ | ||
_overlayMan.RemoveOverlay(_overlay); | ||
} | ||
} | ||
|
||
private void OnPlayerAttached(EntityUid uid) | ||
{ | ||
if (TryComp<ShowHealthBarsComponent>(uid, out var comp)) | ||
{ | ||
ApplyOverlays(comp); | ||
} | ||
} | ||
|
||
private void ApplyOverlays(ShowHealthBarsComponent component) | ||
{ | ||
_overlayMan.AddOverlay(_overlay); | ||
_overlay.DamageContainers.Clear(); | ||
_overlay.DamageContainers.AddRange(component.DamageContainers); | ||
} | ||
|
||
private void OnPlayerDetached(EntityUid uid) | ||
{ | ||
_overlayMan.RemoveOverlay(_overlay); | ||
} | ||
|
||
private void OnRoundRestart(RoundRestartCleanupEvent args) | ||
{ | ||
_overlayMan.RemoveOverlay(_overlay); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Content.Shared.Damage.Prototypes; | ||
using Robust.Shared.GameStates; | ||
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, NetworkedComponent, AutoGenerateComponentState(true)] | ||
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> | ||
|
||
[AutoNetworkedField] | ||
[DataField("damageContainers", customTypeSerializer: typeof(PrototypeIdListSerializer<DamageContainerPrototype>))] | ||
public List<string> DamageContainers = new(); | ||
|
||
public override bool SendOnlyToOwner => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters