Re-organize all projects (#4166)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameTicking;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Spawners.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ConditionalSpawnerComponent : Component, IMapInit
|
||||
{
|
||||
[Dependency] private readonly IGameTicker _gameTicker = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
public override string Name => "ConditionalSpawner";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("prototypes")]
|
||||
public List<string> Prototypes { get; set; } = new();
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("gameRules")]
|
||||
private readonly List<string> _gameRules = new();
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("chance")]
|
||||
public float Chance { get; set; } = 1.0f;
|
||||
|
||||
private void RuleAdded(GameRuleAddedEventArgs obj)
|
||||
{
|
||||
if(_gameRules.Contains(obj.GameRule.GetType().Name))
|
||||
Spawn();
|
||||
}
|
||||
|
||||
private void TrySpawn()
|
||||
{
|
||||
if (_gameRules.Count == 0)
|
||||
{
|
||||
Spawn();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var rule in _gameRules)
|
||||
{
|
||||
if (!_gameTicker.HasGameRule(rule)) continue;
|
||||
Spawn();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Spawn()
|
||||
{
|
||||
if (Chance != 1.0f && !_robustRandom.Prob(Chance))
|
||||
return;
|
||||
|
||||
if (Prototypes.Count == 0)
|
||||
{
|
||||
Logger.Warning($"Prototype list in ConditionalSpawnComponent is empty! Entity: {Owner}");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!Owner.Deleted)
|
||||
Owner.EntityManager.SpawnEntity(_robustRandom.Pick(Prototypes), Owner.Transform.Coordinates);
|
||||
}
|
||||
|
||||
public virtual void MapInit()
|
||||
{
|
||||
_gameTicker.OnRuleAdded += RuleAdded;
|
||||
|
||||
TrySpawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
69
Content.Server/Spawners/Components/RandomSpawnerComponent.cs
Normal file
69
Content.Server/Spawners/Components/RandomSpawnerComponent.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Spawners.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class RandomSpawnerComponent : ConditionalSpawnerComponent
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
public override string Name => "RandomSpawner";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("rarePrototypes")]
|
||||
public List<string> RarePrototypes { get; set; } = new();
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("rareChance")]
|
||||
public float RareChance { get; set; } = 0.05f;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("offset")]
|
||||
public float Offset { get; set; } = 0.2f;
|
||||
|
||||
public override void Spawn()
|
||||
{
|
||||
if (RarePrototypes.Count > 0 && (RareChance == 1.0f || _robustRandom.Prob(RareChance)))
|
||||
{
|
||||
Owner.EntityManager.SpawnEntity(_robustRandom.Pick(RarePrototypes), Owner.Transform.Coordinates);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Chance != 1.0f && !_robustRandom.Prob(Chance))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Prototypes.Count == 0)
|
||||
{
|
||||
Logger.Warning($"Prototype list in RandomSpawnerComponent is empty! Entity: {Owner}");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!Owner.Deleted)
|
||||
{
|
||||
var random = IoCManager.Resolve<IRobustRandom>();
|
||||
|
||||
var x_negative = random.Prob(0.5f) ? -1 : 1;
|
||||
var y_negative = random.Prob(0.5f) ? -1 : 1;
|
||||
|
||||
var entity = Owner.EntityManager.SpawnEntity(_robustRandom.Pick(Prototypes), Owner.Transform.Coordinates);
|
||||
entity.Transform.LocalPosition += new Vector2(random.NextFloat() * Offset * x_negative, random.NextFloat() * Offset * y_negative);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void MapInit()
|
||||
{
|
||||
Spawn();
|
||||
Owner.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Content.Server/Spawners/Components/SpawnPointComponent.cs
Normal file
35
Content.Server/Spawners/Components/SpawnPointComponent.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Content.Shared.Markers;
|
||||
using Content.Shared.Roles;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Spawners.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedSpawnPointComponent))]
|
||||
public sealed class SpawnPointComponent : SharedSpawnPointComponent
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("job_id")]
|
||||
private string? _jobId;
|
||||
|
||||
[field: ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("spawn_type")]
|
||||
public SpawnPointType SpawnType { get; } = SpawnPointType.Unset;
|
||||
|
||||
public JobPrototype? Job => string.IsNullOrEmpty(_jobId) ? null : _prototypeManager.Index<JobPrototype>(_jobId);
|
||||
}
|
||||
|
||||
public enum SpawnPointType
|
||||
{
|
||||
Unset = 0,
|
||||
LateJoin,
|
||||
Job,
|
||||
Observer,
|
||||
}
|
||||
}
|
||||
81
Content.Server/Spawners/Components/TimedSpawnerComponent.cs
Normal file
81
Content.Server/Spawners/Components/TimedSpawnerComponent.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Spawners.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class TimedSpawnerComponent : Component, ISerializationHooks
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
public override string Name => "TimedSpawner";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("prototypes")]
|
||||
public List<string> Prototypes { get; set; } = new();
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("chance")]
|
||||
public float Chance { get; set; } = 1.0f;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("intervalSeconds")]
|
||||
public int IntervalSeconds { get; set; } = 60;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("MinimumEntitiesSpawned")]
|
||||
public int MinimumEntitiesSpawned { get; set; } = 1;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("MaximumEntitiesSpawned")]
|
||||
public int MaximumEntitiesSpawned { get; set; } = 1;
|
||||
|
||||
private CancellationTokenSource? TokenSource;
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
if (MinimumEntitiesSpawned > MaximumEntitiesSpawned)
|
||||
throw new ArgumentException("MaximumEntitiesSpawned can't be lower than MinimumEntitiesSpawned!");
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SetupTimer();
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
TokenSource?.Cancel();
|
||||
}
|
||||
|
||||
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);
|
||||
Owner.EntityManager.SpawnEntity(entity, Owner.Transform.Coordinates);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user