Files
crystall-punk-14/Content.Client/HealthOverlay/UI/HealthOverlayGui.cs
Leon Friedrich df584ad446 ECS damageable (#4529)
* ECS and damage Data

* Comments and newlines

* Added Comments

* Make TryChangeDamageEvent immutable

* Remove SetAllDamage event

Use public SetAllDamage function instead

* Undo destructible mistakes

That was some shit code.

* Rename DamageData to DamageSpecifier

And misc small edits

misc

* Cache trigger prototypes.

* Renaming destructible classes & functions

* Revert "Cache trigger prototypes."

This reverts commit 86bae15ba6616884dba75f552dfdfbe2d1fb6586.

* Replace prototypes with prototype IDs.

* Split damage.yml into individual files

* move get/handle component state to system

* Update HealthChange doc

* Make godmode call Dirty() on damageable component

* Add Initialize() to fix damage test

* Make non-static

* uncache resistance set prototype and trim DamageableComponentState

* Remove unnecessary Dirty() calls during initialization

* RemoveTryChangeDamageEvent

* revert Dirty()

* Fix MobState relying on DamageableComponent.Dirty()

* Fix DisposalUnit Tests.

These were previously failing, but because the async was not await-ed, this never raised the exception.

After I fixed MobState component, this exception stopped happening and instead the assertions started being tested & failing

* Disposal test 2: electric boogaloo

* Fix typos/mistakes

also add comments and fix spacing.

* Use Uids instead of IEntity

* fix merge

* Comments, a merge issue, and making some damage ignore resistances

* Extend DamageSpecifier and use it for DamageableComponent

* fix master merge

* Fix Disposal unit test. Again.

Snapgrids were removed in master

* Execute Exectute
2021-09-14 10:07:37 -07:00

163 lines
4.8 KiB
C#

using Content.Client.IoC;
using Content.Client.Resources;
using Content.Shared.Damage;
using Content.Shared.MobState;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
namespace Content.Client.HealthOverlay.UI
{
public class HealthOverlayGui : VBoxContainer
{
[Dependency] private readonly IEyeManager _eyeManager = default!;
public HealthOverlayGui(IEntity entity)
{
IoCManager.InjectDependencies(this);
IoCManager.Resolve<IUserInterfaceManager>().StateRoot.AddChild(this);
SeparationOverride = 0;
CritBar = new HealthOverlayBar
{
Visible = false,
VerticalAlignment = VAlignment.Center,
Color = Color.Red
};
HealthBar = new HealthOverlayBar
{
Visible = false,
VerticalAlignment = VAlignment.Center,
Color = Color.Green
};
AddChild(Panel = new PanelContainer
{
Children =
{
new TextureRect
{
Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/Misc/health_bar.rsi/icon.png"),
TextureScale = Vector2.One * HealthOverlayBar.HealthBarScale,
VerticalAlignment = VAlignment.Center,
},
CritBar,
HealthBar
}
});
Entity = entity;
}
public PanelContainer Panel { get; }
public HealthOverlayBar HealthBar { get; }
public HealthOverlayBar CritBar { get; }
public IEntity Entity { get; }
public void SetVisibility(bool val)
{
Visible = val;
Panel.Visible = val;
}
private void MoreFrameUpdate(FrameEventArgs args)
{
if (Entity.Deleted)
{
return;
}
if (!Entity.TryGetComponent(out IMobStateComponent? mobState) ||
!Entity.TryGetComponent(out DamageableComponent? damageable))
{
CritBar.Visible = false;
HealthBar.Visible = false;
return;
}
int threshold;
if (mobState.IsAlive())
{
if (!mobState.TryGetEarliestCriticalState(damageable.TotalDamage, out _, out threshold))
{
CritBar.Visible = false;
HealthBar.Visible = false;
return;
}
CritBar.Ratio = 1;
CritBar.Visible = true;
HealthBar.Ratio = 1 - (float) damageable.TotalDamage / threshold;
HealthBar.Visible = true;
}
else if (mobState.IsCritical())
{
HealthBar.Ratio = 0;
HealthBar.Visible = false;
if (!mobState.TryGetPreviousCriticalState(damageable.TotalDamage, out _, out var critThreshold) ||
!mobState.TryGetEarliestDeadState(damageable.TotalDamage, out _, out var deadThreshold))
{
CritBar.Visible = false;
return;
}
CritBar.Visible = true;
CritBar.Ratio = 1 - (float)
(damageable.TotalDamage - critThreshold) /
(deadThreshold - critThreshold);
}
else if (mobState.IsDead())
{
CritBar.Ratio = 0;
CritBar.Visible = false;
HealthBar.Ratio = 0;
HealthBar.Visible = true;
}
else
{
CritBar.Visible = false;
HealthBar.Visible = false;
}
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
MoreFrameUpdate(args);
if (Entity.Deleted ||
_eyeManager.CurrentMap != Entity.Transform.MapID)
{
Visible = false;
return;
}
Visible = true;
var screenCoordinates = _eyeManager.CoordinatesToScreen(Entity.Transform.Coordinates);
var playerPosition = UserInterfaceManager.ScreenToUIPosition(screenCoordinates);
LayoutContainer.SetPosition(this, new Vector2(playerPosition.X - Width / 2, playerPosition.Y - Height - 30.0f));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
HealthBar.Dispose();
}
}
}