Files
tbd-station-14/Content.Server/Storage/Components/StorageFillComponent.cs
Vera Aguilera Puerto 5cd42c9ad6 Inline UID
2021-12-03 15:53:09 +01:00

62 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using Content.Shared.Storage;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Storage.Components
{
[RegisterComponent]
public sealed class StorageFillComponent : Component, IMapInit
{
public override string Name => "StorageFill";
[DataField("contents")] private List<EntitySpawnEntry> _contents = new();
public IReadOnlyList<EntitySpawnEntry> Contents => _contents;
void IMapInit.MapInit()
{
if (_contents.Count == 0)
{
return;
}
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out IStorageComponent? storage))
{
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({Owner})");
return;
}
var random = IoCManager.Resolve<IRobustRandom>();
var alreadySpawnedGroups = new List<string>();
foreach (var storageItem in _contents)
{
if (!string.IsNullOrEmpty(storageItem.GroupId) &&
alreadySpawnedGroups.Contains(storageItem.GroupId)) continue;
if (storageItem.SpawnProbability != 1f &&
!random.Prob(storageItem.SpawnProbability))
{
continue;
}
for (var i = 0; i < storageItem.Amount; i++)
{
storage.Insert(
IoCManager.Resolve<IEntityManager>().SpawnEntity(storageItem.PrototypeId, IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates));
}
if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId);
}
}
}
}