Files
tbd-station-14/Content.Client/HealthOverlay/HealthOverlaySystem.cs
DrSmugleaf ab9d0cc6d8 Fix errors
2021-12-05 21:02:04 +01:00

110 lines
3.2 KiB
C#

using System.Collections.Generic;
using Content.Client.HealthOverlay.UI;
using Content.Shared.Damage;
using Content.Shared.GameTicking;
using Content.Shared.MobState.Components;
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
{
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IEntityManager _entities = default!;
private readonly Dictionary<EntityUid, HealthOverlayGui> _guis = new();
private EntityUid? _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();
SubscribeNetworkEvent<RoundRestartCleanupEvent>(Reset);
SubscribeLocalEvent<PlayerAttachSysMessage>(HandlePlayerAttached);
}
public void Reset(RoundRestartCleanupEvent ev)
{
foreach (var gui in _guis.Values)
{
gui.Dispose();
}
_guis.Clear();
_attachedEntity = default;
}
private void HandlePlayerAttached(PlayerAttachSysMessage message)
{
_attachedEntity = message.AttachedEntity;
}
public override void FrameUpdate(float frameTime)
{
base.Update(frameTime);
if (!_enabled)
{
return;
}
if (_attachedEntity == null || (!_entities.EntityExists(_attachedEntity.Value) ? EntityLifeStage.Deleted : _entities.GetComponent<MetaDataComponent>(_attachedEntity.Value).EntityLifeStage) >= EntityLifeStage.Deleted)
{
return;
}
var viewBox = _eyeManager.GetWorldViewport().Enlarged(2.0f);
foreach (var (mobState, _) in EntityManager.EntityQuery<MobStateComponent, DamageableComponent>())
{
var entity = mobState.Owner;
if (_entities.GetComponent<TransformComponent>(_attachedEntity.Value).MapID != _entities.GetComponent<TransformComponent>(entity).MapID ||
!viewBox.Contains(_entities.GetComponent<TransformComponent>(entity).WorldPosition))
{
if (_guis.TryGetValue(entity, out var oldGui))
{
_guis.Remove(entity);
oldGui.Dispose();
}
continue;
}
if (_guis.ContainsKey(entity))
{
continue;
}
var gui = new HealthOverlayGui(entity);
_guis.Add(entity, gui);
}
}
}
}