sasalka/Code/WorlModelClothSpawner.cs
2025-06-26 01:56:08 +03:00

60 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Sasalka;
public sealed class WorlModelClothSpawner : Component
{
[Property] public GameObject Prefab { get; set; }
[Property] public Vector3 CenterPosition { get; set; } = Vector3.Zero;
[Property] public float Spacing { get; set; } = 50.0f;
[Property] public float Height { get; set; } = 0.0f;
protected override void OnStart()
{
var definitions = ResourceLibrary.GetAll<ClothingItemDefinition>();
var clothingItemDefinitions = definitions.ToList();
int total = clothingItemDefinitions.Count();
if ( total == 0 )
{
Log.Warning( "No clothing definitions found." );
return;
}
// Автоматически вычисляем минимальный радиус круга, чтобы вместить все предметы
// Площадь круга: πr², а площадь одного предмета: Spacing²
// Нам нужно чтобы πr² >= total * Spacing² → r >= sqrt(total * Spacing² / π)
float estimatedRadius = MathF.Sqrt( (total * Spacing * Spacing) / MathF.PI );
float radius = estimatedRadius;
Log.Info( $"Auto-calculated radius: {radius}" );
int defIndex = 0;
for ( float x = -radius; x <= radius; x += Spacing )
{
for ( float y = -radius; y <= radius; y += Spacing )
{
if ( defIndex >= total )
break;
if ( x * x + y * y <= radius * radius )
{
Vector3 pos = CenterPosition + new Vector3( x, y, Height );
var gO = Prefab.Clone( pos );
gO.NetworkSpawn( null );
var item = gO.Components.Get<InventoryItem>();
if ( item is not null )
item.Definition = clothingItemDefinitions[defIndex];
defIndex++;
}
}
if ( defIndex >= total )
break;
}
}
}