2020-06-05 19:42:43 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Content.Server.GameTicking;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Robust.Shared.Log;
|
2020-06-05 19:42:43 +02:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-06-05 19:42:43 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Spawners.Components
|
2020-06-05 19:42:43 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
public class ConditionalSpawnerComponent : Component, IMapInit
|
|
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
2020-06-05 19:42:43 +02:00
|
|
|
|
2020-08-24 14:10:28 +02:00
|
|
|
public override string Name => "ConditionalSpawner";
|
2020-06-05 19:42:43 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("prototypes")]
|
2020-11-27 11:00:49 +01:00
|
|
|
public List<string> Prototypes { get; set; } = new();
|
2020-06-05 19:42:43 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("gameRules")]
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly List<string> _gameRules = new();
|
2020-06-05 19:42:43 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("chance")]
|
2020-06-05 19:42:43 +02:00
|
|
|
public float Chance { get; set; } = 1.0f;
|
|
|
|
|
|
2021-06-20 10:09:24 +02:00
|
|
|
public void RuleAdded(GameRuleAddedEvent obj)
|
2020-06-05 19:42:43 +02:00
|
|
|
{
|
2021-06-20 10:09:24 +02:00
|
|
|
if(_gameRules.Contains(obj.Rule.GetType().Name))
|
2020-06-05 19:42:43 +02:00
|
|
|
Spawn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TrySpawn()
|
|
|
|
|
{
|
|
|
|
|
if (_gameRules.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Spawn();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
foreach (var rule in _gameRules)
|
2020-06-05 19:42:43 +02:00
|
|
|
{
|
2021-06-20 10:09:24 +02:00
|
|
|
if (!EntitySystem.Get<GameTicker>().HasGameRule(rule)) continue;
|
2020-06-05 19:42:43 +02:00
|
|
|
Spawn();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 16:49:54 -05:00
|
|
|
public virtual void Spawn()
|
2020-06-05 19:42:43 +02:00
|
|
|
{
|
|
|
|
|
if (Chance != 1.0f && !_robustRandom.Prob(Chance))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Prototypes.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning($"Prototype list in ConditionalSpawnComponent is empty! Entity: {Owner}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 15:32:38 +02:00
|
|
|
if(!Owner.Deleted)
|
2020-11-18 15:45:53 +01:00
|
|
|
Owner.EntityManager.SpawnEntity(_robustRandom.Pick(Prototypes), Owner.Transform.Coordinates);
|
2020-06-05 19:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-11 16:49:54 -05:00
|
|
|
public virtual void MapInit()
|
2020-06-05 19:42:43 +02:00
|
|
|
{
|
|
|
|
|
TrySpawn();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|