2026-10-23 02:59:47 +03:00
|
|
|
|
using System.Collections.Generic;
|
2026-10-08 14:38:09 +03:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
public struct CarDefectsSummary
|
|
|
|
|
|
{
|
|
|
|
|
|
public int rustedPartsCount;
|
|
|
|
|
|
public int missingBodyPartsCount;
|
|
|
|
|
|
public int flatTiresCount;
|
|
|
|
|
|
public int missingWheelsCount;
|
|
|
|
|
|
public List<string> missingPartNames;
|
|
|
|
|
|
public float totalPricePenalty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-08 14:38:09 +03:00
|
|
|
|
public class CarConditionGenerator : MonoBehaviour
|
|
|
|
|
|
{
|
2026-10-23 02:59:47 +03:00
|
|
|
|
[Header("Настройки ржавчины")]
|
|
|
|
|
|
[Range(0f, 1f)] [SerializeField] private float rustProbability = 0.65f;
|
|
|
|
|
|
[SerializeField] private float minRustAmount = 0.25f;
|
|
|
|
|
|
[SerializeField] private float maxRustAmount = 0.80f;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("Шанс отсутствия навесных деталей (бамперы, фары, крылья)")]
|
|
|
|
|
|
[Range(0f, 0.5f)] [SerializeField] private float missingBodyPartChance = 0.15f;
|
|
|
|
|
|
[SerializeField] private int maxMissingBodyParts = 3;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("Проблемы с колесами")]
|
|
|
|
|
|
[Range(0f, 1f)] [SerializeField] private float flatTireChance = 0.25f;
|
|
|
|
|
|
[Range(0f, 1f)] [SerializeField] private float missingWheelChance = 0.12f;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
|
|
|
|
|
[Header("Цвет кузова")]
|
|
|
|
|
|
[SerializeField] private bool overrideMaterialColor = false;
|
|
|
|
|
|
[SerializeField] private Color baseCarColor = Color.blue;
|
2026-10-08 14:38:09 +03:00
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
private bool configuredExternally = false;
|
|
|
|
|
|
|
2026-10-08 14:38:09 +03:00
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
2026-10-23 02:59:47 +03:00
|
|
|
|
// Если машина уже настроена через CarMarketManager (по Seed) — не перезаписываем её!
|
|
|
|
|
|
if (!configuredExternally)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenerateCondition();
|
|
|
|
|
|
}
|
2026-10-08 14:38:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
[ContextMenu("Сгенерировать случайное состояние")]
|
2026-10-08 14:38:09 +03:00
|
|
|
|
public void GenerateCondition()
|
|
|
|
|
|
{
|
2026-10-23 02:59:47 +03:00
|
|
|
|
int randomSeed = Random.Range(1, 999999);
|
|
|
|
|
|
Color col = overrideMaterialColor ? baseCarColor : GetCurrentBodyColor();
|
|
|
|
|
|
ApplyConditionBySeed(randomSeed, col, false, out _);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Color GetCurrentBodyColor()
|
|
|
|
|
|
{
|
|
|
|
|
|
int rustProp = Shader.PropertyToID("_RustAmount");
|
|
|
|
|
|
int colorProp = Shader.PropertyToID("_BaseColor");
|
|
|
|
|
|
foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Material m in r.sharedMaterials)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m != null && m.HasProperty(rustProp) && m.HasProperty(colorProp))
|
|
|
|
|
|
return m.GetColor(colorProp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return baseCarColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Генерирует 100% одинаковое состояние машины для одного и того же seed (и в фотобоксе, и на карте).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ApplyConditionBySeed(int seed, Color carColor, bool isPhotoStudio, out CarDefectsSummary summary)
|
|
|
|
|
|
{
|
|
|
|
|
|
configuredExternally = true;
|
|
|
|
|
|
Random.State oldState = Random.state;
|
|
|
|
|
|
Random.InitState(seed);
|
|
|
|
|
|
|
|
|
|
|
|
summary = new CarDefectsSummary
|
|
|
|
|
|
{
|
|
|
|
|
|
missingPartNames = new List<string>()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-10-08 14:38:09 +03:00
|
|
|
|
CarPart[] allParts = GetComponentsInChildren<CarPart>(true);
|
2026-10-23 02:59:47 +03:00
|
|
|
|
|
|
|
|
|
|
// 1. Обрабатываем кузовные и навесные детали (кроме колес)
|
2026-10-12 15:40:07 +03:00
|
|
|
|
foreach (CarPart part in allParts)
|
2026-10-08 14:38:09 +03:00
|
|
|
|
{
|
2026-10-23 02:59:47 +03:00
|
|
|
|
// Пропускаем колеса (у них свой цикл ниже)
|
|
|
|
|
|
if (part.GetComponent<CarWheelCondition>() != null || part.TotalBoltsCount > 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2026-10-08 14:38:09 +03:00
|
|
|
|
CarPartVisuals visuals = part.GetComponent<CarPartVisuals>();
|
|
|
|
|
|
if (visuals == null) visuals = part.gameObject.AddComponent<CarPartVisuals>();
|
|
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
visuals.SetPaintColor(carColor);
|
2026-10-08 14:38:09 +03:00
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
float partPrice = part.Data != null ? part.Data.basePrice : 150f;
|
|
|
|
|
|
string partTitle = part.Data != null ? part.Data.partName : part.name;
|
|
|
|
|
|
|
|
|
|
|
|
// Проверяем, отсутствует ли эта навесная деталь (бампер, фара, крыло, дверь, зеркало)
|
|
|
|
|
|
if (summary.missingBodyPartsCount < maxMissingBodyParts && Random.value < missingBodyPartChance)
|
|
|
|
|
|
{
|
|
|
|
|
|
summary.missingBodyPartsCount++;
|
|
|
|
|
|
summary.missingPartNames.Add(partTitle.ToLower());
|
|
|
|
|
|
summary.totalPricePenalty += partPrice * 1.4f;
|
|
|
|
|
|
part.MakeMissingAtStart(null, false);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Пластик не ржавеет
|
2026-10-08 14:38:09 +03:00
|
|
|
|
if (part.Data != null && !part.Data.canRust)
|
|
|
|
|
|
{
|
2026-10-12 15:40:07 +03:00
|
|
|
|
visuals.SetDamage(0f);
|
2026-10-08 14:38:09 +03:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
if (Random.value < rustProbability)
|
|
|
|
|
|
{
|
|
|
|
|
|
float dmg = Random.Range(minRustAmount, maxRustAmount);
|
|
|
|
|
|
visuals.SetDamage(dmg);
|
|
|
|
|
|
summary.rustedPartsCount++;
|
|
|
|
|
|
summary.totalPricePenalty += partPrice * dmg;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
visuals.SetDamage(0f);
|
|
|
|
|
|
}
|
2026-10-08 14:38:09 +03:00
|
|
|
|
}
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-10-23 02:59:47 +03:00
|
|
|
|
// 2. Обрабатываем основной несъемный кузов (body)
|
2026-10-20 17:54:31 +03:00
|
|
|
|
int rustProp = Shader.PropertyToID("_RustAmount");
|
|
|
|
|
|
foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (r.GetComponentInParent<CarPart>(true) != null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (Material m in r.sharedMaterials)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m != null && m.HasProperty(rustProp))
|
|
|
|
|
|
{
|
|
|
|
|
|
CarPartVisuals bodyVis = r.GetComponent<CarPartVisuals>();
|
|
|
|
|
|
if (bodyVis == null) bodyVis = r.gameObject.AddComponent<CarPartVisuals>();
|
2026-10-23 02:59:47 +03:00
|
|
|
|
bodyVis.SetPaintColor(carColor);
|
|
|
|
|
|
|
|
|
|
|
|
float bodyDmg = Random.value < rustProbability ? Random.Range(minRustAmount, maxRustAmount) : 0f;
|
|
|
|
|
|
bodyVis.SetDamage(bodyDmg);
|
|
|
|
|
|
if (bodyDmg > 0.05f)
|
|
|
|
|
|
{
|
|
|
|
|
|
summary.rustedPartsCount++;
|
|
|
|
|
|
summary.totalPricePenalty += 250f * bodyDmg;
|
|
|
|
|
|
}
|
2026-10-20 17:54:31 +03:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-23 02:59:47 +03:00
|
|
|
|
|
|
|
|
|
|
// 3. Обрабатываем колеса (Нормальное / Спущенное / Отсутствует)
|
|
|
|
|
|
CarWheelCondition[] wheels = GetComponentsInChildren<CarWheelCondition>(true);
|
|
|
|
|
|
foreach (CarWheelCondition wheel in wheels)
|
|
|
|
|
|
{
|
|
|
|
|
|
float roll = Random.value;
|
|
|
|
|
|
// В одном объявлении убираем максимум 1 колесо, чтобы машина не висела вся на кирпичах
|
|
|
|
|
|
if (roll < missingWheelChance && summary.missingWheelsCount < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
summary.missingWheelsCount++;
|
|
|
|
|
|
summary.totalPricePenalty += 220f;
|
|
|
|
|
|
CarPart wp = wheel.GetComponent<CarPart>();
|
|
|
|
|
|
if (wp != null) wp.MakeMissingAtStart(null, !isPhotoStudio);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (roll < missingWheelChance + flatTireChance)
|
|
|
|
|
|
{
|
|
|
|
|
|
summary.flatTiresCount++;
|
|
|
|
|
|
summary.totalPricePenalty += 100f;
|
|
|
|
|
|
wheel.ApplyState(CarWheelCondition.TireState.Flat);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
wheel.ApplyState(CarWheelCondition.TireState.Normal);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Random.state = oldState;
|
2026-10-08 14:38:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|