Code/k/ClothesTest.cs
using System.Collections.Generic;
using System.Text.Json;
namespace Sandbox.k;
[Title( nameof(ClothesTest) )]
[Category( "k.Clothing" )]
public sealed class ClothesTest : Component
{
[RequireComponent] private SkinnedModelRenderer _model { get; set; }
[Property] private string[] _hat { get; set; }
[Property] private string[] _facial { get; set; }
[Property] private string[] _top { get; set; }
[Property] private string[] _gloves { get; set; }
[Property] private string[] _bottom { get; set; }
[Property] private string[] _footwear { get; set; }
private Dictionary<Clothing.ClothingCategory, List<Clothing>> _clothes;
private List<ClothesCatalog> _parsedCatalogs;
[Button]
private void Test()
{
InitializeClothes();
var container = new ClothingContainer();
ApplyCollection( container, Clothing.ClothingCategory.Hat, _hat );
ApplyCollection( container, Clothing.ClothingCategory.Facial, _facial );
ApplyCollection( container, Clothing.ClothingCategory.Tops, _top );
ApplyCollection( container, Clothing.ClothingCategory.Gloves, _gloves );
ApplyCollection( container, Clothing.ClothingCategory.Bottoms, _bottom );
ApplyCollection( container, Clothing.ClothingCategory.Footwear, _footwear );
container.Apply( _model );
}
private void ApplyCollection(ClothingContainer container, Clothing.ClothingCategory category, string[] collection)
{
foreach ( var item in collection )
{
if ( TryGetClothes( category, item, out var clothe ) )
{
container.Add( clothe );
}
}
}
private bool TryGetClothes( Clothing.ClothingCategory category, string name, out Clothing clothe )
{
clothe = null;
if ( !_clothes.TryGetValue( category, out var clothes ) ) return false;
clothe = clothes.Find( x => x.Title == name );
if ( clothe == null ) return false;
return true;
}
[Button]
private void TakeThemOff()
{
var container = new ClothingContainer();
container.Apply( _model );
}
[Button]
private void Print()
{
InitializeClothes();
_parsedCatalogs = new List<ClothesCatalog>();
foreach ( var (category, clothes) in _clothes )
{
var clotheTitles = new List<string>();
foreach ( var clothe in clothes )
{
clotheTitles.Add( clothe.Title );
}
var catalog = new ClothesCatalog
{
Category = category.ToString(),
Clothes = clotheTitles
};
_parsedCatalogs.Add( catalog );
}
Log.Info( JsonSerializer.Serialize( _parsedCatalogs, new JsonSerializerOptions { WriteIndented = true } ) );
}
private void InitializeClothes()
{
if (_clothes != null) return;
var clothes = ResourceLibrary.GetAll<Clothing>();
_clothes = new Dictionary<Clothing.ClothingCategory, List<Clothing>>();
foreach ( var clothing in clothes )
{
var category = clothing.Category;
if ( _clothes.TryGetValue( category, out var clothe ) )
{
clothe.Add( clothing );
continue;
}
// whitelist error
// _clothes[clothing.Category] = [ clothing ];
_clothes[clothing.Category] = new List<Clothing>() { clothing };
}
}
}
public class ClothesCatalog
{
public string Category { get; set; }
public List<string> Clothes { get; set; }
}