Remove component.Initialize calls (#18230)

This commit is contained in:
metalgearsloth
2023-07-26 22:37:52 +10:00
committed by GitHub
parent 32d8fd2cc7
commit b478d5326b
9 changed files with 91 additions and 153 deletions

View File

@@ -9,8 +9,6 @@ namespace Content.Server.Spawners.Components
[RegisterComponent]
public sealed class TimedSpawnerComponent : Component, ISerializationHooks
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("prototypes", customTypeSerializer:typeof(PrototypeIdListSerializer<EntityPrototype>))]
public List<string> Prototypes { get; set; } = new();
@@ -38,32 +36,5 @@ namespace Content.Server.Spawners.Components
if (MinimumEntitiesSpawned > MaximumEntitiesSpawned)
throw new ArgumentException("MaximumEntitiesSpawned can't be lower than MinimumEntitiesSpawned!");
}
protected override void Initialize()
{
base.Initialize();
SetupTimer();
}
private void SetupTimer()
{
TokenSource?.Cancel();
TokenSource = new CancellationTokenSource();
Owner.SpawnRepeatingTimer(TimeSpan.FromSeconds(IntervalSeconds), OnTimerFired, TokenSource.Token);
}
private void OnTimerFired()
{
if (!_robustRandom.Prob(Chance))
return;
var number = _robustRandom.Next(MinimumEntitiesSpawned, MaximumEntitiesSpawned);
for (int i = 0; i < number; i++)
{
var entity = _robustRandom.Pick(Prototypes);
IoCManager.Resolve<IEntityManager>().SpawnEntity(entity, IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates);
}
}
}
}

View File

@@ -0,0 +1,38 @@
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);
}
}
}