Files
crystall-punk-14/Content.Client/HealthOverlay/UI/HealthOverlayGui.cs

165 lines
5.4 KiB
C#
Raw Normal View History

using System.Numerics;
2021-06-09 22:19:39 +02:00
using Content.Client.IoC;
using Content.Client.Resources;
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-15 03:07:37 +10:00
using Content.Shared.Damage;
using Content.Shared.FixedPoint;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Timing;
2021-06-09 22:19:39 +02:00
namespace Content.Client.HealthOverlay.UI
{
public sealed class HealthOverlayGui : BoxContainer
{
[Dependency] private readonly IEyeManager _eyeManager = default!;
2021-12-05 18:09:01 +01:00
[Dependency] private readonly IEntityManager _entities = default!;
2021-12-05 18:09:01 +01:00
public HealthOverlayGui(EntityUid entity)
{
IoCManager.InjectDependencies(this);
UserInterfaceManager.WindowRoot.AddChild(this);
SeparationOverride = 0;
Orientation = LayoutOrientation.Vertical;
CritBar = new HealthOverlayBar
{
Visible = false,
2021-02-21 12:38:56 +01:00
VerticalAlignment = VAlignment.Center,
Color = Color.Red
};
HealthBar = new HealthOverlayBar
{
Visible = false,
2021-02-21 12:38:56 +01:00
VerticalAlignment = VAlignment.Center,
Color = Color.LimeGreen
};
AddChild(Panel = new PanelContainer
{
Children =
{
new TextureRect
{
Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/Misc/health_bar.rsi/icon.png"),
TextureScale = Vector2.One * HealthOverlayBar.HealthBarScale,
2021-02-21 12:38:56 +01:00
VerticalAlignment = VAlignment.Center,
},
CritBar,
HealthBar
}
});
Entity = entity;
}
public PanelContainer Panel { get; }
public HealthOverlayBar HealthBar { get; }
public HealthOverlayBar CritBar { get; }
2021-12-05 18:09:01 +01:00
public EntityUid Entity { get; }
public void SetVisibility(bool val)
{
Visible = val;
Panel.Visible = val;
}
private void MoreFrameUpdate(FrameEventArgs args)
{
2021-12-09 12:29:27 +01:00
if (_entities.Deleted(Entity))
{
return;
}
2021-12-05 18:09:01 +01:00
if (!_entities.TryGetComponent(Entity, out MobStateComponent? mobState) ||
!_entities.TryGetComponent(Entity, out DamageableComponent? damageable))
{
CritBar.Visible = false;
HealthBar.Visible = false;
return;
}
2022-07-06 17:58:14 +10:00
var mobStateSystem = _entities.EntitySysManager.GetEntitySystem<MobStateSystem>();
var mobThresholdSystem = _entities.EntitySysManager.GetEntitySystem<MobThresholdSystem>();
if (mobStateSystem.IsAlive(mobState.Owner, mobState))
{
if (!mobThresholdSystem.TryGetThresholdForState(Entity,MobState.Critical, out var threshold))
{
CritBar.Visible = false;
HealthBar.Visible = false;
return;
}
CritBar.Ratio = 1;
CritBar.Visible = true;
HealthBar.Ratio = 1 - ((FixedPoint2)(damageable.TotalDamage / threshold)).Float();
HealthBar.Visible = true;
}
else if (mobStateSystem.IsCritical(mobState.Owner, mobState))
{
HealthBar.Ratio = 0;
HealthBar.Visible = false;
if (!mobThresholdSystem.TryGetThresholdForState(Entity, MobState.Critical, out var critThreshold) ||
!mobThresholdSystem.TryGetThresholdForState(Entity, MobState.Dead, out var deadThreshold))
{
CritBar.Visible = false;
return;
}
CritBar.Visible = true;
CritBar.Ratio = 1 -
((damageable.TotalDamage - critThreshold) /
(deadThreshold - critThreshold)).Value.Float();
}
else if (mobStateSystem.IsDead(mobState.Owner, mobState))
{
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);
2021-12-09 12:29:27 +01:00
if (_entities.Deleted(Entity) || _eyeManager.CurrentMap != _entities.GetComponent<TransformComponent>(Entity).MapID)
{
Visible = false;
return;
}
Visible = true;
2021-12-05 18:09:01 +01:00
var screenCoordinates = _eyeManager.CoordinatesToScreen(_entities.GetComponent<TransformComponent>(Entity).Coordinates);
2021-05-02 20:48:00 +02:00
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();
}
}
}