Files
tbd-station-14/Content.Server/Spawners/EntitySystems/SpawnerSystem.cs
2023-07-26 22:37:52 +10:00

39 lines
1.2 KiB
C#

using System.Threading;
using Content.Server.Spawners.Components;
using Robust.Shared.Random;
namespace Content.Server.Spawners.EntitySystems;
public sealed class SpawnerSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TimedSpawnerComponent, ComponentInit>(OnSpawnerInit);
}
private void OnSpawnerInit(EntityUid uid, TimedSpawnerComponent component, ComponentInit args)
{
component.TokenSource?.Cancel();
component.TokenSource = new CancellationTokenSource();
uid.SpawnRepeatingTimer(TimeSpan.FromSeconds(component.IntervalSeconds), () => OnTimerFired(uid, component), component.TokenSource.Token);
}
private void OnTimerFired(EntityUid uid, TimedSpawnerComponent component)
{
if (!_random.Prob(component.Chance))
return;
var number = _random.Next(component.MinimumEntitiesSpawned, component.MaximumEntitiesSpawned);
var coordinates = Transform(uid).Coordinates;
for (var i = 0; i < number; i++)
{
var entity = _random.Pick(component.Prototypes);
Spawn(entity, coordinates);
}
}
}