ECS StorageFill (#6351)
This commit is contained in:
46
Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs
Normal file
46
Content.Server/Storage/EntitySystems/StorageSystem.Fill.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Storage.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems;
|
||||
|
||||
public sealed partial class StorageSystem
|
||||
{
|
||||
private void OnStorageFillMapInit(EntityUid uid, StorageFillComponent component, MapInitEvent args)
|
||||
{
|
||||
if (component.Contents.Count == 0) return;
|
||||
|
||||
if (!TryComp<IStorageComponent>(uid, out var storage))
|
||||
{
|
||||
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({uid})");
|
||||
return;
|
||||
}
|
||||
|
||||
var coordinates = Transform(uid).Coordinates;
|
||||
var alreadySpawnedGroups = new HashSet<string>();
|
||||
|
||||
foreach (var entry in component.Contents)
|
||||
{
|
||||
// Handle "Or" groups
|
||||
if (!string.IsNullOrEmpty(entry.GroupId) && alreadySpawnedGroups.Contains(entry.GroupId)) continue;
|
||||
|
||||
// Check random spawn
|
||||
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
||||
if (entry.SpawnProbability != 1f && !_random.Prob(entry.SpawnProbability)) continue;
|
||||
|
||||
for (var i = 0; i < entry.Amount; i++)
|
||||
{
|
||||
var ent = EntityManager.SpawnEntity(entry.PrototypeId, coordinates);
|
||||
|
||||
if (storage.Insert(ent)) continue;
|
||||
|
||||
Logger.ErrorS("storage", $"Tried to StorageFill {entry.PrototypeId} inside {uid} but can't.");
|
||||
EntityManager.DeleteEntity(ent);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(entry.GroupId)) alreadySpawnedGroups.Add(entry.GroupId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,16 @@ using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class StorageSystem : EntitySystem
|
||||
public sealed partial class StorageSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
private readonly List<IPlayerSession> _sessionCache = new();
|
||||
|
||||
@@ -33,6 +35,8 @@ namespace Content.Server.Storage.EntitySystems
|
||||
SubscribeLocalEvent<EntityStorageComponent, GetInteractionVerbsEvent>(AddToggleOpenVerb);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetActivationVerbsEvent>(AddOpenUiVerb);
|
||||
SubscribeLocalEvent<EntityStorageComponent, RelayMovementEntityEvent>(OnRelayMovement);
|
||||
|
||||
SubscribeLocalEvent<StorageFillComponent, MapInitEvent>(OnStorageFillMapInit);
|
||||
}
|
||||
|
||||
private void OnRelayMovement(EntityUid uid, EntityStorageComponent component, RelayMovementEntityEvent args)
|
||||
|
||||
Reference in New Issue
Block a user