Files
OldThink/Content.Client/HealthOverlay/HealthOverlaySystem.cs

110 lines
3.0 KiB
C#
Raw Normal View History

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 System.Collections.Generic;
2021-06-09 22:19:39 +02:00
using Content.Client.HealthOverlay.UI;
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.GameTicking;
2021-11-08 15:11:58 +01:00
using Content.Shared.MobState.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
2021-06-09 22:19:39 +02:00
namespace Content.Client.HealthOverlay
{
[UsedImplicitly]
public sealed class HealthOverlaySystem : EntitySystem
{
[Dependency] private readonly IEyeManager _eyeManager = default!;
2021-12-05 21:02:04 +01:00
[Dependency] private readonly IEntityManager _entities = default!;
private readonly Dictionary<EntityUid, HealthOverlayGui> _guis = new();
2021-12-05 21:02:04 +01:00
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();
2021-12-05 18:09:01 +01:00
_attachedEntity = default;
}
private void HandlePlayerAttached(PlayerAttachSysMessage message)
{
_attachedEntity = message.AttachedEntity;
}
public override void FrameUpdate(float frameTime)
{
base.Update(frameTime);
if (!_enabled)
{
return;
}
2021-12-09 12:29:27 +01:00
if (_attachedEntity is not {} ent || Deleted(ent))
{
return;
}
var viewBox = _eyeManager.GetWorldViewport().Enlarged(2.0f);
2021-11-08 15:11:58 +01:00
foreach (var (mobState, _) in EntityManager.EntityQuery<MobStateComponent, DamageableComponent>())
{
var entity = mobState.Owner;
2021-12-09 12:29:27 +01:00
if (_entities.GetComponent<TransformComponent>(ent).MapID != _entities.GetComponent<TransformComponent>(entity).MapID ||
2021-12-05 21:02:04 +01:00
!viewBox.Contains(_entities.GetComponent<TransformComponent>(entity).WorldPosition))
{
2021-12-03 15:53:09 +01:00
if (_guis.TryGetValue(entity, out var oldGui))
{
2021-12-03 15:53:09 +01:00
_guis.Remove(entity);
2021-12-03 14:20:34 +01:00
oldGui.Dispose();
}
continue;
}
2021-12-03 15:53:09 +01:00
if (_guis.ContainsKey(entity))
{
continue;
}
var gui = new HealthOverlayGui(entity);
2021-12-03 15:53:09 +01:00
_guis.Add(entity, gui);
}
}
}
}