Trash bag visualizer (#7199)

This commit is contained in:
Alex Evgrashin
2022-03-23 16:14:23 +03:00
committed by GitHub
parent 46ac70a734
commit 74d4adfb96
5 changed files with 115 additions and 18 deletions

View File

@@ -0,0 +1,22 @@
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
using Robust.Client.GameObjects;
namespace Content.Client.Storage.Visualizers;
public sealed class StorageFillVisualizerSystem : VisualizerSystem<StorageFillVisualizerComponent>
{
protected override void OnAppearanceChange(EntityUid uid, StorageFillVisualizerComponent component, ref AppearanceChangeEvent args)
{
base.OnAppearanceChange(uid, component, ref args);
if (!TryComp(uid, out SpriteComponent? sprite))
return;
if (!args.Component.TryGetData(StorageFillVisuals.FillLevel, out int level))
return;
var state = $"{component.FillBaseName}-{level}";
sprite.LayerSetState(StorageFillLayers.Fill, state);
}
}

View File

@@ -73,9 +73,9 @@ namespace Content.Server.Storage.Components
private EntityWhitelist? _whitelist = null; private EntityWhitelist? _whitelist = null;
private bool _storageInitialCalculated; private bool _storageInitialCalculated;
private int _storageUsed; public int StorageUsed;
[DataField("capacity")] [DataField("capacity")]
private int _storageCapacityMax = 10000; public int StorageCapacityMax = 10000;
public readonly HashSet<IPlayerSession> SubscribedSessions = new(); public readonly HashSet<IPlayerSession> SubscribedSessions = new();
[DataField("storageSoundCollection")] [DataField("storageSoundCollection")]
@@ -123,7 +123,7 @@ namespace Content.Server.Storage.Components
private void RecalculateStorageUsed() private void RecalculateStorageUsed()
{ {
_storageUsed = 0; StorageUsed = 0;
_sizeCache.Clear(); _sizeCache.Clear();
if (Storage == null) if (Storage == null)
@@ -134,7 +134,7 @@ namespace Content.Server.Storage.Components
foreach (var entity in Storage.ContainedEntities) foreach (var entity in Storage.ContainedEntities)
{ {
var item = _entityManager.GetComponent<SharedItemComponent>(entity); var item = _entityManager.GetComponent<SharedItemComponent>(entity);
_storageUsed += item.Size; StorageUsed += item.Size;
_sizeCache.Add(entity, item.Size); _sizeCache.Add(entity, item.Size);
} }
} }
@@ -149,13 +149,13 @@ namespace Content.Server.Storage.Components
EnsureInitialCalculated(); EnsureInitialCalculated();
if (_entityManager.TryGetComponent(entity, out ServerStorageComponent? storage) && if (_entityManager.TryGetComponent(entity, out ServerStorageComponent? storage) &&
storage._storageCapacityMax >= _storageCapacityMax) storage.StorageCapacityMax >= StorageCapacityMax)
{ {
return false; return false;
} }
if (_entityManager.TryGetComponent(entity, out SharedItemComponent? store) && if (_entityManager.TryGetComponent(entity, out SharedItemComponent? store) &&
store.Size > _storageCapacityMax - _storageUsed) store.Size > StorageCapacityMax - StorageUsed)
{ {
return false; return false;
} }
@@ -205,7 +205,7 @@ namespace Content.Server.Storage.Components
if (_entityManager.TryGetComponent(message.Entity, out SharedItemComponent? storable)) if (_entityManager.TryGetComponent(message.Entity, out SharedItemComponent? storable))
size = storable.Size; size = storable.Size;
_storageUsed += size; StorageUsed += size;
_sizeCache[message.Entity] = size; _sizeCache[message.Entity] = size;
UpdateClientInventories(); UpdateClientInventories();
@@ -230,7 +230,7 @@ namespace Content.Server.Storage.Components
return; return;
} }
_storageUsed -= size; StorageUsed -= size;
UpdateClientInventories(); UpdateClientInventories();
} }
@@ -350,7 +350,7 @@ namespace Content.Server.Storage.Components
var stored = StoredEntities.Select(e => e).ToArray(); var stored = StoredEntities.Select(e => e).ToArray();
#pragma warning disable 618 #pragma warning disable 618
SendNetworkMessage(new StorageHeldItemsMessage(stored, _storageUsed, _storageCapacityMax), session.ConnectedClient); SendNetworkMessage(new StorageHeldItemsMessage(stored, StorageUsed, StorageCapacityMax), session.ConnectedClient);
#pragma warning restore 618 #pragma warning restore 618
} }

View File

@@ -0,0 +1,42 @@
using Content.Server.Storage.Components;
using Content.Shared.Rounding;
using Content.Shared.Storage.Components;
using Robust.Shared.Containers;
namespace Content.Server.Storage.EntitySystems;
public sealed class StorageFillVisualizerSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StorageFillVisualizerComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<StorageFillVisualizerComponent, EntInsertedIntoContainerMessage>(OnInserted);
SubscribeLocalEvent<StorageFillVisualizerComponent, EntRemovedFromContainerMessage>(OnRemoved);
}
private void OnInit(EntityUid uid, StorageFillVisualizerComponent component, ComponentInit args)
{
UpdateAppearance(uid, component: component);
}
private void OnInserted(EntityUid uid, StorageFillVisualizerComponent component, EntInsertedIntoContainerMessage args)
{
UpdateAppearance(uid, component: component);
}
private void OnRemoved(EntityUid uid, StorageFillVisualizerComponent component, EntRemovedFromContainerMessage args)
{
UpdateAppearance(uid, component: component);
}
private void UpdateAppearance(EntityUid uid, ServerStorageComponent? storage = null, AppearanceComponent? appearance = null,
StorageFillVisualizerComponent? component = null)
{
if (!Resolve(uid, ref storage, ref appearance, ref component, false))
return;
var level = ContentHelpers.RoundToEqualLevels(storage.StorageUsed, storage.StorageCapacityMax, component.MaxFillLevels);
appearance.SetData(StorageFillVisuals.FillLevel, level);
}
}

View File

@@ -0,0 +1,28 @@
using Robust.Shared.Serialization;
namespace Content.Shared.Storage.Components;
/// <summary>
/// Change sprite depending on a storage fill percent.
/// </summary>
[RegisterComponent]
public sealed class StorageFillVisualizerComponent : Component
{
[DataField("maxFillLevels", required: true)]
public int MaxFillLevels;
[DataField("fillBaseName", required: true)]
public string FillBaseName = default!;
}
[Serializable, NetSerializable]
public enum StorageFillVisuals : byte
{
FillLevel
}
[Serializable, NetSerializable]
public enum StorageFillLayers : byte
{
Fill
}

View File

@@ -4,8 +4,11 @@
parent: BaseItem parent: BaseItem
components: components:
- type: Sprite - type: Sprite
netSync: false
sprite: Objects/Specific/Janitorial/trashbag.rsi sprite: Objects/Specific/Janitorial/trashbag.rsi
state: icon-3 layers:
- state: icon-0
map: ["enum.StorageFillLayers.Fill"]
- type: Item - type: Item
sprite: Objects/Specific/Janitorial/trashbag.rsi sprite: Objects/Specific/Janitorial/trashbag.rsi
size: 125 size: 125
@@ -23,6 +26,10 @@
tags: tags:
- TrashBag - TrashBag
- DroneUsable - DroneUsable
- type: Appearance
- type: StorageFillVisualizer
maxFillLevels: 4
fillBaseName: icon
- type: entity - type: entity
name: trash bag name: trash bag
@@ -30,21 +37,19 @@
parent: TrashBag parent: TrashBag
components: components:
- type: Sprite - type: Sprite
state: blue-icon-3 layers:
- state: blue-icon-0
map: ["enum.StorageFillLayers.Fill"]
- type: Item - type: Item
HeldPrefix: blue HeldPrefix: blue
- type: StorageFillVisualizer
fillBaseName: blue-icon
- type: entity - type: entity
name: spell of all-consuming cleanliness name: spell of all-consuming cleanliness
id: BagOfSummoningGarbage id: BagOfSummoningGarbage
parent: BaseItem parent: TrashBagBlue
components: components:
- type: Sprite
sprite: Objects/Specific/Janitorial/trashbag.rsi
state: blue-icon-3
- type: Item
sprite: Objects/Specific/Janitorial/trashbag.rsi
HeldPrefix: blue
- type: Storage - type: Storage
capacity: 125000 capacity: 125000
quickInsert: true quickInsert: true