Files
tbd-station-14/Content.Server/Damage/Systems/GodmodeSystem.cs
Leon Friedrich df584ad446 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-14 10:07:37 -07:00

119 lines
3.2 KiB
C#

using System.Collections.Generic;
using Content.Server.Atmos.Components;
using Content.Shared.Damage;
using Content.Shared.GameTicking;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Damage.Systems
{
[UsedImplicitly]
public class GodmodeSystem : EntitySystem
{
private readonly Dictionary<IEntity, OldEntityInformation> _entities = new();
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
}
public void Reset(RoundRestartCleanupEvent ev)
{
_entities.Clear();
}
public bool EnableGodmode(IEntity entity)
{
if (_entities.ContainsKey(entity))
{
return false;
}
_entities[entity] = new OldEntityInformation(entity);
if (entity.TryGetComponent(out MovedByPressureComponent? moved))
{
moved.Enabled = false;
}
if (entity.TryGetComponent(out DamageableComponent? damageable))
{
_damageableSystem.SetDamage(damageable, new DamageSpecifier());
}
return true;
}
public bool HasGodmode(IEntity entity)
{
return _entities.ContainsKey(entity);
}
public bool DisableGodmode(IEntity entity)
{
if (!_entities.Remove(entity, out var old))
{
return false;
}
if (entity.TryGetComponent(out MovedByPressureComponent? moved))
{
moved.Enabled = old.MovedByPressure;
}
if (entity.TryGetComponent(out DamageableComponent? damageable))
{
if (old.Damage != null)
{
_damageableSystem.SetDamage(damageable, old.Damage);
}
}
return true;
}
/// <summary>
/// Toggles godmode for a given entity.
/// </summary>
/// <param name="entity">The entity to toggle godmode for.</param>
/// <returns>true if enabled, false if disabled.</returns>
public bool ToggleGodmode(IEntity entity)
{
if (HasGodmode(entity))
{
DisableGodmode(entity);
return false;
}
else
{
EnableGodmode(entity);
return true;
}
}
public class OldEntityInformation
{
public OldEntityInformation(IEntity entity)
{
Entity = entity;
MovedByPressure = entity.IsMovedByPressure();
if (entity.TryGetComponent(out DamageableComponent? damageable))
{
Damage = damageable.Damage;
}
}
public IEntity Entity { get; }
public bool MovedByPressure { get; }
public DamageSpecifier? Damage { get; }
}
}
}