Re-organize all projects (#4166)
This commit is contained in:
114
Content.Client/HealthOverlay/HealthOverlaySystem.cs
Normal file
114
Content.Client/HealthOverlay/HealthOverlaySystem.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Client.HealthOverlay.UI;
|
||||
using Content.Shared.Damage.Components;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.MobState;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Client.HealthOverlay
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class HealthOverlaySystem : EntitySystem, IResettingEntitySystem
|
||||
{
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
|
||||
private readonly Dictionary<EntityUid, HealthOverlayGui> _guis = new();
|
||||
private IEntity? _attachedEntity;
|
||||
private bool _enabled;
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get => _enabled;
|
||||
set
|
||||
{
|
||||
if (_enabled == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_enabled = value;
|
||||
|
||||
foreach (var gui in _guis.Values)
|
||||
{
|
||||
gui.SetVisibility(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PlayerAttachSysMessage>(HandlePlayerAttached);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
UnsubscribeLocalEvent<PlayerAttachSysMessage>();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (var gui in _guis.Values)
|
||||
{
|
||||
gui.Dispose();
|
||||
}
|
||||
|
||||
_guis.Clear();
|
||||
_attachedEntity = null;
|
||||
}
|
||||
|
||||
private void HandlePlayerAttached(PlayerAttachSysMessage message)
|
||||
{
|
||||
_attachedEntity = message.AttachedEntity;
|
||||
}
|
||||
|
||||
public override void FrameUpdate(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_attachedEntity == null || _attachedEntity.Deleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var viewBox = _eyeManager.GetWorldViewport().Enlarged(2.0f);
|
||||
|
||||
foreach (var (mobState, _) in ComponentManager.EntityQuery<IMobStateComponent, IDamageableComponent>())
|
||||
{
|
||||
var entity = mobState.Owner;
|
||||
|
||||
if (_attachedEntity.Transform.MapID != entity.Transform.MapID ||
|
||||
!viewBox.Contains(entity.Transform.WorldPosition))
|
||||
{
|
||||
if (_guis.TryGetValue(entity.Uid, out var oldGui))
|
||||
{
|
||||
oldGui.Dispose();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_guis.ContainsKey(entity.Uid))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var gui = new HealthOverlayGui(entity);
|
||||
_guis.Add(entity.Uid, gui);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Content.Client/HealthOverlay/UI/HealthOverlayBar.cs
Normal file
46
Content.Client/HealthOverlay/UI/HealthOverlayBar.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client.HealthOverlay.UI
|
||||
{
|
||||
public class HealthOverlayBar : Control
|
||||
{
|
||||
public const byte HealthBarScale = 2;
|
||||
|
||||
private const int XPixelDiff = 20 * HealthBarScale;
|
||||
|
||||
public HealthOverlayBar()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
Shader = IoCManager.Resolve<IPrototypeManager>().Index<ShaderPrototype>("unshaded").Instance();
|
||||
}
|
||||
|
||||
private ShaderInstance Shader { get; }
|
||||
|
||||
/// <summary>
|
||||
/// From -1 (dead) to 0 (crit) and 1 (alive)
|
||||
/// </summary>
|
||||
public float Ratio { get; set; }
|
||||
|
||||
public Color Color { get; set; }
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
base.Draw(handle);
|
||||
|
||||
handle.UseShader(Shader);
|
||||
|
||||
var leftOffset = 2 * HealthBarScale;
|
||||
var box = new UIBox2i(
|
||||
leftOffset,
|
||||
-2 + 2 * HealthBarScale,
|
||||
leftOffset + (int) (XPixelDiff * Ratio * UIScale),
|
||||
-2);
|
||||
|
||||
handle.DrawRect(box, Color);
|
||||
}
|
||||
}
|
||||
}
|
||||
163
Content.Client/HealthOverlay/UI/HealthOverlayGui.cs
Normal file
163
Content.Client/HealthOverlay/UI/HealthOverlayGui.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
#nullable enable
|
||||
using Content.Client.IoC;
|
||||
using Content.Client.Resources;
|
||||
using Content.Shared.Damage.Components;
|
||||
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 IDamageableComponent? 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user