60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
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;
|
||
}
|
||
}
|
||
}
|