Files
OldThink/Content.Server/Spawners/EntitySystems/ConditionalSpawnerSystem.cs

113 lines
3.9 KiB
C#
Raw Normal View History

using System.Numerics;
using Content.Server.GameTicking;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
using Content.Server.Spawners.Components;
using JetBrains.Annotations;
2022-01-31 17:36:49 +11:00
using Robust.Shared.Random;
namespace Content.Server.Spawners.EntitySystems
{
[UsedImplicitly]
2022-01-31 17:36:49 +11:00
public sealed class ConditionalSpawnerSystem : EntitySystem
{
2022-01-31 17:36:49 +11:00
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly GameTicker _ticker = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GameRuleStartedEvent>(OnRuleStarted);
2022-01-31 17:36:49 +11:00
SubscribeLocalEvent<ConditionalSpawnerComponent, MapInitEvent>(OnCondSpawnMapInit);
SubscribeLocalEvent<RandomSpawnerComponent, MapInitEvent>(OnRandSpawnMapInit);
}
private void OnCondSpawnMapInit(EntityUid uid, ConditionalSpawnerComponent component, MapInitEvent args)
{
2023-04-25 20:23:14 -04:00
TrySpawn(uid, component);
2022-01-31 17:36:49 +11:00
}
private void OnRandSpawnMapInit(EntityUid uid, RandomSpawnerComponent component, MapInitEvent args)
{
2023-04-25 20:23:14 -04:00
Spawn(uid, component);
QueueDel(uid);
}
2023-04-25 20:23:14 -04:00
private void OnRuleStarted(ref GameRuleStartedEvent args)
{
2023-04-25 20:23:14 -04:00
var query = EntityQueryEnumerator<ConditionalSpawnerComponent>();
while (query.MoveNext(out var uid, out var spawner))
{
2023-04-25 20:23:14 -04:00
RuleStarted(uid, spawner, args);
2022-01-31 17:36:49 +11:00
}
}
2023-04-25 20:23:14 -04:00
public void RuleStarted(EntityUid uid, ConditionalSpawnerComponent component, GameRuleStartedEvent obj)
2022-01-31 17:36:49 +11:00
{
2023-04-25 20:23:14 -04:00
if (component.GameRules.Contains(obj.RuleId))
Spawn(uid, component);
2022-01-31 17:36:49 +11:00
}
2023-04-25 20:23:14 -04:00
private void TrySpawn(EntityUid uid, ConditionalSpawnerComponent component)
2022-01-31 17:36:49 +11:00
{
if (component.GameRules.Count == 0)
{
2023-04-25 20:23:14 -04:00
Spawn(uid, component);
2022-01-31 17:36:49 +11:00
return;
}
foreach (var rule in component.GameRules)
{
2023-04-25 20:23:14 -04:00
if (!_ticker.IsGameRuleActive(rule))
continue;
Spawn(uid, component);
2022-01-31 17:36:49 +11:00
return;
}
}
2023-04-25 20:23:14 -04:00
private void Spawn(EntityUid uid, ConditionalSpawnerComponent component)
2022-01-31 17:36:49 +11:00
{
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
2023-04-25 20:23:14 -04:00
Logger.Warning($"Prototype list in ConditionalSpawnComponent is empty! Entity: {ToPrettyString(uid)}");
2022-01-31 17:36:49 +11:00
return;
}
2022-01-31 17:36:49 +11:00
2023-04-25 20:23:14 -04:00
if (!Deleted(uid))
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), Transform(uid).Coordinates);
2022-01-31 17:36:49 +11:00
}
2023-04-25 20:23:14 -04:00
private void Spawn(EntityUid uid, RandomSpawnerComponent component)
2022-01-31 17:36:49 +11:00
{
if (component.RarePrototypes.Count > 0 && (component.RareChance == 1.0f || _robustRandom.Prob(component.RareChance)))
{
2023-04-25 20:23:14 -04:00
EntityManager.SpawnEntity(_robustRandom.Pick(component.RarePrototypes), Transform(uid).Coordinates);
2022-01-31 17:36:49 +11:00
return;
}
if (component.Chance != 1.0f && !_robustRandom.Prob(component.Chance))
return;
if (component.Prototypes.Count == 0)
{
2023-04-25 20:23:14 -04:00
Logger.Warning($"Prototype list in RandomSpawnerComponent is empty! Entity: {ToPrettyString(uid)}");
2022-01-31 17:36:49 +11:00
return;
}
2023-04-25 20:23:14 -04:00
if (Deleted(uid))
return;
2022-01-31 17:36:49 +11:00
var offset = component.Offset;
var xOffset = _robustRandom.NextFloat(-offset, offset);
var yOffset = _robustRandom.NextFloat(-offset, offset);
2023-04-25 20:23:14 -04:00
var coordinates = Transform(uid).Coordinates.Offset(new Vector2(xOffset, yOffset));
2022-01-31 17:36:49 +11:00
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), coordinates);
}
}
}