Radiation pulse ECS (#10641)

This commit is contained in:
Alex Evgrashin
2022-08-31 12:24:21 +02:00
committed by GitHub
parent ccb240ccca
commit cad6c760ad
18 changed files with 146 additions and 258 deletions

View File

@@ -1,103 +0,0 @@
using Content.Shared.Radiation;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Radiation
{
[RegisterComponent]
[ComponentReference(typeof(SharedRadiationPulseComponent))]
public sealed class RadiationPulseComponent : SharedRadiationPulseComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private float _duration;
private float _range = 5f;
private TimeSpan _startTime;
private TimeSpan _endTime;
private bool _draw = true;
private bool _decay = true;
/// <summary>
/// Whether the entity will delete itself after a certain duration defined by
/// <see cref="MinPulseLifespan"/> and <see cref="MaxPulseLifespan"/>
/// </summary>
[DataField("decay")]
public override bool Decay
{
get => _decay;
set
{
_decay = value;
Dirty();
}
}
[DataField("minPulseLifespan")]
public float MinPulseLifespan { get; set; } = 0.8f;
[DataField("maxPulseLifespan")]
public float MaxPulseLifespan { get; set; } = 2.5f;
[DataField("sound")] public SoundSpecifier Sound { get; set; } = new SoundCollectionSpecifier("RadiationPulse");
[DataField("range")]
public override float Range
{
get => _range;
set
{
_range = value;
Dirty();
}
}
[DataField("draw")]
public override bool Draw
{
get => _draw;
set
{
_draw = value;
Dirty();
}
}
public override TimeSpan StartTime => _startTime;
public override TimeSpan EndTime => _endTime;
public void DoPulse()
{
if (Decay)
{
var currentTime = _gameTiming.CurTime;
_startTime = currentTime;
_duration = _random.NextFloat() * (MaxPulseLifespan - MinPulseLifespan) + MinPulseLifespan;
_endTime = currentTime + TimeSpan.FromSeconds(_duration);
}
SoundSystem.Play(Sound.GetSound(), Filter.Pvs(Owner), Owner);
Dirty();
}
public override ComponentState GetComponentState()
{
return new RadiationPulseState(_range, Draw, Decay, _startTime, _endTime);
}
public void Update(float frameTime)
{
if (!Decay || _entMan.Deleted(Owner))
return;
if (_duration <= 0f)
_entMan.QueueDeleteEntity(Owner);
_duration -= frameTime;
}
}
}

View File

@@ -1,38 +0,0 @@
using Content.Server.Radiation.Systems;
using JetBrains.Annotations;
namespace Content.Server.Radiation
{
[UsedImplicitly]
public sealed class RadiationPulseSystem : EntitySystem
{
[Dependency] private readonly RadiationSystem _radiation = default!;
private const float RadiationCooldown = 1.0f;
private float _accumulator;
public override void Update(float frameTime)
{
base.Update(frameTime);
_accumulator += frameTime;
while (_accumulator > RadiationCooldown)
{
_accumulator -= RadiationCooldown;
// All code here runs effectively every RadiationCooldown seconds, so use that as the "frame time".
foreach (var comp in EntityManager.EntityQuery<RadiationPulseComponent>())
{
comp.Update(RadiationCooldown);
var ent = comp.Owner;
if (Deleted(ent)) continue;
var cords = Transform(ent).MapPosition;
_radiation.IrradiateRange(cords, comp.Range, comp.RadsPerSecond, RadiationCooldown);
}
}
}
}
}

View File

@@ -7,6 +7,31 @@ public sealed class RadiationSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
private const float RadiationCooldown = 1.0f;
private float _accumulator;
public override void Update(float frameTime)
{
base.Update(frameTime);
_accumulator += frameTime;
while (_accumulator > RadiationCooldown)
{
_accumulator -= RadiationCooldown;
// All code here runs effectively every RadiationCooldown seconds, so use that as the "frame time".
foreach (var comp in EntityManager.EntityQuery<RadiationSourceComponent>())
{
var ent = comp.Owner;
if (Deleted(ent))
continue;
var cords = Transform(ent).MapPosition;
IrradiateRange(cords, comp.Range, comp.RadsPerSecond, RadiationCooldown);
}
}
}
public void IrradiateRange(MapCoordinates coordinates, float range, float radsPerSecond, float time)
{
var lookUp = _lookup.GetEntitiesInRange(coordinates, range);