CatStation/Content.Shared/Damage/Systems/DamageContactsSystem.cs
ArtisticRoomba 376f16504c
Content.Shared EntityQuery<T> dependency injection refactor (#43498)
* part 1

* second pass

* final fix

* last fix i swear

* an even bigger genius

* regex missed this

* should be all of em?

* oops

* yeah

* Revert "should be all of em?"

This reverts commit 4dd54d1b9a76977b4cf8d6008358e4dacdee6eb1.

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2026-04-11 21:30:36 +00:00

75 lines
2.4 KiB
C#

using Content.Shared.Damage.Components;
using Content.Shared.Whitelist;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
namespace Content.Shared.Damage.Systems;
public sealed class DamageContactsSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly EntityQuery<DamageContactsComponent> _damageQuery = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DamageContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<DamageContactsComponent, EndCollideEvent>(OnEntityExit);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<DamagedByContactComponent>();
while (query.MoveNext(out var ent, out var damaged))
{
if (_timing.CurTime < damaged.NextSecond)
continue;
damaged.NextSecond = _timing.CurTime + TimeSpan.FromSeconds(1);
if (damaged.Damage != null)
_damageable.TryChangeDamage(ent, damaged.Damage, interruptsDoAfters: false);
}
}
private void OnEntityExit(EntityUid uid, DamageContactsComponent component, ref EndCollideEvent args)
{
var otherUid = args.OtherEntity;
if (!TryComp<PhysicsComponent>(otherUid, out var body))
return;
foreach (var ent in _physics.GetContactingEntities(otherUid, body))
{
if (ent == uid)
continue;
if (_damageQuery.HasComponent(ent))
return;
}
RemComp<DamagedByContactComponent>(otherUid);
}
private void OnEntityEnter(EntityUid uid, DamageContactsComponent component, ref StartCollideEvent args)
{
var otherUid = args.OtherEntity;
if (HasComp<DamagedByContactComponent>(otherUid))
return;
if (_whitelistSystem.IsWhitelistPass(component.IgnoreWhitelist, otherUid))
return;
var damagedByContact = EnsureComp<DamagedByContactComponent>(otherUid);
damagedByContact.Damage = component.Damage;
}
}