Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/GodmodeSystem.cs
DrSmugleaf 5c0cf1b1a0 Use 'new' expression in places where the type is evident for content (#2590)
* Content.Client

* Content.Benchmarks

* Content.IntegrationTests

* Content.Server

* Content.Server.Database

* Content.Shared

* Content.Tests

* Merge fixes

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2020-11-27 21:00:49 +11:00

101 lines
2.7 KiB
C#

#nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Atmos;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameTicking;
using JetBrains.Annotations;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.EntitySystems
{
[UsedImplicitly]
public class GodmodeSystem : EntitySystem, IResettingEntitySystem
{
private readonly Dictionary<IEntity, OldEntityInformation> _entities = new();
public void Reset()
{
_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 IDamageableComponent? damageable))
{
damageable.AddFlag(DamageFlag.Invulnerable);
}
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 IDamageableComponent? damageable))
{
damageable.RemoveFlag(DamageFlag.Invulnerable);
}
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();
}
public IEntity Entity { get; }
public bool MovedByPressure { get; }
}
}
}