267 lines
13 KiB
C#
267 lines
13 KiB
C#
using UnityEngine;
|
|||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Процедурная генерация 3D-узлов моторного отсека (используется EngineBayManager для машин без
|
||
|
|
/// отдельных подкапотных мешей и магазином запчастей для новых агрегатов).
|
||
|
|
/// </summary>
|
||
|
|
public static class EnginePartFactory
|
||
|
|
{
|
||
|
|
public static int InteractLayer
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
int l = LayerMask.NameToLayer("Interactble");
|
||
|
|
if (l < 0) l = LayerMask.NameToLayer("Interactable");
|
||
|
|
return l < 0 ? 0 : l;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static Material M(string key, Color c) => PartCatalog.Instance.GetMaterial(key, c);
|
||
|
|
|
||
|
|
public static Material Black => M("eb_black", new Color(0.07f, 0.07f, 0.08f));
|
||
|
|
public static Material DarkGrey => M("eb_darkgrey", new Color(0.18f, 0.19f, 0.21f));
|
||
|
|
public static Material Metal => M("eb_metal", new Color(0.62f, 0.63f, 0.66f));
|
||
|
|
public static Material Red => M("eb_red", new Color(0.8f, 0.12f, 0.1f));
|
||
|
|
public static Material Yellow => M("eb_yellow", new Color(0.95f, 0.75f, 0.1f));
|
||
|
|
public static Material Blue => M("eb_blue", new Color(0.12f, 0.35f, 0.85f));
|
||
|
|
public static Material Orange => M("eb_orange", new Color(0.92f, 0.45f, 0.08f));
|
||
|
|
public static Material Pink => M("eb_pink", new Color(0.95f, 0.3f, 0.55f));
|
||
|
|
public static Material TankWhite => M("eb_tank", new Color(0.88f, 0.86f, 0.8f));
|
||
|
|
public static Material Copper => M("eb_copper", new Color(0.72f, 0.42f, 0.2f));
|
||
|
|
|
||
|
|
public static string GetObjectName(EnginePartType type)
|
||
|
|
{
|
||
|
|
switch (type)
|
||
|
|
{
|
||
|
|
case EnginePartType.Battery: return "Battery_12V";
|
||
|
|
case EnginePartType.SparkPlugs: return "SparkPlugs";
|
||
|
|
case EnginePartType.AirFilter: return "AirFilter";
|
||
|
|
case EnginePartType.OilFilter: return "OilFilter";
|
||
|
|
case EnginePartType.Radiator: return "Radiator";
|
||
|
|
case EnginePartType.Alternator: return "Alternator";
|
||
|
|
case EnginePartType.FuelFilter: return "FuelFilter";
|
||
|
|
default: return "EnginePart";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static float GetMass(EnginePartType type)
|
||
|
|
{
|
||
|
|
switch (type)
|
||
|
|
{
|
||
|
|
case EnginePartType.Battery: return 14f;
|
||
|
|
case EnginePartType.Radiator: return 6f;
|
||
|
|
case EnginePartType.Alternator: return 5f;
|
||
|
|
case EnginePartType.SparkPlugs: return 1.5f;
|
||
|
|
default: return 1f;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Создает съемный агрегат. asLoose = true — новая деталь из магазина, лежащая отдельно от машины.</summary>
|
||
|
|
public static EnginePart CreatePart(EnginePartType type, Transform parent, Vector3 localPos, Quaternion localRot, float widthHint = 0.6f, bool asLoose = false)
|
||
|
|
{
|
||
|
|
GameObject root = new GameObject(GetObjectName(type));
|
||
|
|
root.layer = InteractLayer;
|
||
|
|
if (parent != null) root.transform.SetParent(parent, false);
|
||
|
|
root.transform.localPosition = localPos;
|
||
|
|
root.transform.localRotation = localRot;
|
||
|
|
|
||
|
|
BuildVisual(type, root.transform, widthHint);
|
||
|
|
SetLayerRecursive(root, InteractLayer);
|
||
|
|
|
||
|
|
BoxCollider box = root.AddComponent<BoxCollider>();
|
||
|
|
Bounds b = LocalBounds(root.transform);
|
||
|
|
box.center = b.center;
|
||
|
|
box.size = b.size + Vector3.one * 0.02f;
|
||
|
|
box.isTrigger = !asLoose;
|
||
|
|
|
||
|
|
CarPart cp = root.AddComponent<CarPart>();
|
||
|
|
CarPartData data = PartCatalog.Instance.GetEngineData(type);
|
||
|
|
cp.SetData(data, GetMass(type));
|
||
|
|
|
||
|
|
EnginePart ep = root.AddComponent<EnginePart>();
|
||
|
|
ep.partType = type;
|
||
|
|
ep.SetAsNew();
|
||
|
|
return ep;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void BuildVisual(EnginePartType type, Transform root, float width)
|
||
|
|
{
|
||
|
|
switch (type)
|
||
|
|
{
|
||
|
|
case EnginePartType.Battery:
|
||
|
|
Prim(PrimitiveType.Cube, root, "Case", new Vector3(0f, 0f, 0f), new Vector3(0.24f, 0.18f, 0.17f), Black);
|
||
|
|
Prim(PrimitiveType.Cube, root, "Lid", new Vector3(0f, 0.095f, 0f), new Vector3(0.245f, 0.02f, 0.175f), DarkGrey);
|
||
|
|
Prim(PrimitiveType.Cube, root, "Label", new Vector3(0f, 0.01f, 0.086f), new Vector3(0.2f, 0.07f, 0.004f), Yellow);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Terminal_Plus", new Vector3(0.08f, 0.115f, 0.05f), new Vector3(0.028f, 0.015f, 0.028f), Red);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Terminal_Minus", new Vector3(-0.08f, 0.115f, 0.05f), new Vector3(0.028f, 0.015f, 0.028f), Black);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case EnginePartType.SparkPlugs:
|
||
|
|
Prim(PrimitiveType.Cube, root, "CoilRail", Vector3.zero, new Vector3(0.42f, 0.04f, 0.07f), DarkGrey);
|
||
|
|
for (int i = 0; i < 4; i++)
|
||
|
|
{
|
||
|
|
float x = -0.15f + i * 0.1f;
|
||
|
|
Prim(PrimitiveType.Cylinder, root, $"Coil_{i + 1}", new Vector3(x, 0.045f, 0f), new Vector3(0.04f, 0.035f, 0.04f), Black);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, $"Boot_{i + 1}", new Vector3(x, 0.09f, 0f), new Vector3(0.022f, 0.012f, 0.022f), Red);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case EnginePartType.AirFilter:
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Housing", Vector3.zero, new Vector3(0.3f, 0.05f, 0.3f), Black);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Lid", new Vector3(0f, 0.055f, 0f), new Vector3(0.24f, 0.01f, 0.24f), DarkGrey);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "WingNut", new Vector3(0f, 0.07f, 0f), new Vector3(0.05f, 0.012f, 0.05f), Metal);
|
||
|
|
Prim(PrimitiveType.Cube, root, "Snout", new Vector3(0f, 0f, -0.19f), new Vector3(0.09f, 0.06f, 0.14f), Black);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case EnginePartType.OilFilter:
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Canister", Vector3.zero, new Vector3(0.09f, 0.06f, 0.09f), Orange);
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Base", new Vector3(0f, -0.065f, 0f), new Vector3(0.095f, 0.006f, 0.095f), Metal);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case EnginePartType.Radiator:
|
||
|
|
{
|
||
|
|
float w = Mathf.Clamp(width * 0.75f, 0.45f, 0.8f);
|
||
|
|
Prim(PrimitiveType.Cube, root, "Core", Vector3.zero, new Vector3(w, 0.38f, 0.035f), DarkGrey);
|
||
|
|
for (int i = 0; i < 9; i++)
|
||
|
|
Prim(PrimitiveType.Cube, root, $"Fin_{i}", new Vector3(0f, -0.16f + i * 0.04f, 0.019f), new Vector3(w * 0.96f, 0.006f, 0.004f), Metal);
|
||
|
|
Prim(PrimitiveType.Cube, root, "TopTank", new Vector3(0f, 0.21f, 0f), new Vector3(w + 0.02f, 0.04f, 0.06f), Black);
|
||
|
|
Prim(PrimitiveType.Cube, root, "BottomTank", new Vector3(0f, -0.21f, 0f), new Vector3(w + 0.02f, 0.04f, 0.06f), Black);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
case EnginePartType.Alternator:
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Body", Vector3.zero, new Vector3(0.13f, 0.055f, 0.13f), Metal, new Vector3(0f, 0f, 90f));
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Pulley", new Vector3(0.065f, 0f, 0f), new Vector3(0.07f, 0.008f, 0.07f), Black, new Vector3(0f, 0f, 90f));
|
||
|
|
Prim(PrimitiveType.Cube, root, "Bracket", new Vector3(-0.02f, -0.07f, 0f), new Vector3(0.08f, 0.03f, 0.05f), DarkGrey);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case EnginePartType.FuelFilter:
|
||
|
|
Prim(PrimitiveType.Cylinder, root, "Body", Vector3.zero, new Vector3(0.06f, 0.05f, 0.06f), TankWhite);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Пластиковый бачок с крышкой. Крышка получает имя, по которому её находит CarFluids.</summary>
|
||
|
|
public static Transform CreateReservoir(FluidType type, Transform parent, Vector3 localPos)
|
||
|
|
{
|
||
|
|
GameObject root = new GameObject($"Reservoir_{type}");
|
||
|
|
root.layer = InteractLayer;
|
||
|
|
root.transform.SetParent(parent, false);
|
||
|
|
root.transform.localPosition = localPos;
|
||
|
|
|
||
|
|
Material capMat;
|
||
|
|
Material fillMat;
|
||
|
|
Vector3 tankSize;
|
||
|
|
string capName;
|
||
|
|
switch (type)
|
||
|
|
{
|
||
|
|
case FluidType.Coolant:
|
||
|
|
tankSize = new Vector3(0.14f, 0.13f, 0.10f); capMat = Black; fillMat = Pink; capName = "Gen_CoolantCap"; break;
|
||
|
|
case FluidType.BrakeFluid:
|
||
|
|
tankSize = new Vector3(0.09f, 0.08f, 0.06f); capMat = Yellow; fillMat = TankWhite; capName = "Gen_BrakeFluidCap"; break;
|
||
|
|
case FluidType.WasherFluid:
|
||
|
|
tankSize = new Vector3(0.16f, 0.17f, 0.12f); capMat = Blue; fillMat = Blue; capName = "Gen_WasherFluidCap"; break;
|
||
|
|
default:
|
||
|
|
tankSize = Vector3.zero; capMat = Yellow; fillMat = Black; capName = "Gen_OilCap"; break;
|
||
|
|
}
|
||
|
|
|
||
|
|
float capY = 0.012f;
|
||
|
|
if (tankSize != Vector3.zero)
|
||
|
|
{
|
||
|
|
Prim(PrimitiveType.Cube, root.transform, "Tank", Vector3.zero, tankSize, TankWhite);
|
||
|
|
Prim(PrimitiveType.Cube, root.transform, "FluidLevel", new Vector3(0f, -tankSize.y * 0.2f, tankSize.z * 0.51f), new Vector3(tankSize.x * 0.8f, tankSize.y * 0.45f, 0.004f), fillMat);
|
||
|
|
capY = tankSize.y * 0.5f + 0.012f;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Prim(PrimitiveType.Cylinder, root.transform, "FillerNeck", new Vector3(0f, -0.02f, 0f), new Vector3(0.045f, 0.02f, 0.045f), DarkGrey);
|
||
|
|
}
|
||
|
|
|
||
|
|
GameObject cap = Prim(PrimitiveType.Cylinder, root.transform, capName, new Vector3(0f, capY, 0f), new Vector3(0.055f, 0.012f, 0.055f), capMat);
|
||
|
|
BoxCollider bc = cap.AddComponent<BoxCollider>();
|
||
|
|
bc.isTrigger = true;
|
||
|
|
bc.size = new Vector3(2.4f, 3.5f, 2.4f); // относительно масштаба крышки — удобная зона наведения
|
||
|
|
SetLayerRecursive(root, InteractLayer);
|
||
|
|
return cap.transform;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static OilDipstick CreateDipstick(Transform parent, Vector3 localPos)
|
||
|
|
{
|
||
|
|
GameObject root = new GameObject("OilDipstick");
|
||
|
|
root.transform.SetParent(parent, false);
|
||
|
|
root.transform.localPosition = localPos;
|
||
|
|
Prim(PrimitiveType.Cylinder, root.transform, "Ring", new Vector3(0f, 0.03f, 0f), new Vector3(0.045f, 0.005f, 0.045f), Yellow);
|
||
|
|
Prim(PrimitiveType.Cylinder, root.transform, "Rod", new Vector3(0f, -0.04f, 0f), new Vector3(0.008f, 0.07f, 0.008f), Metal);
|
||
|
|
SphereCollider sc = root.AddComponent<SphereCollider>();
|
||
|
|
sc.isTrigger = true;
|
||
|
|
sc.radius = 0.05f;
|
||
|
|
sc.center = new Vector3(0f, 0.03f, 0f);
|
||
|
|
SetLayerRecursive(root, InteractLayer);
|
||
|
|
return root.AddComponent<OilDipstick>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static OilDrainPlug CreateOilPan(Transform parent, Vector3 localPos, float width)
|
||
|
|
{
|
||
|
|
GameObject pan = new GameObject("OilPan");
|
||
|
|
pan.transform.SetParent(parent, false);
|
||
|
|
pan.transform.localPosition = localPos;
|
||
|
|
Prim(PrimitiveType.Cube, pan.transform, "Sump", Vector3.zero, new Vector3(Mathf.Clamp(width * 0.45f, 0.3f, 0.5f), 0.08f, 0.32f), DarkGrey);
|
||
|
|
|
||
|
|
GameObject plug = Prim(PrimitiveType.Cylinder, pan.transform, "OilDrainPlug", new Vector3(0f, -0.045f, -0.08f), new Vector3(0.04f, 0.008f, 0.04f), Metal);
|
||
|
|
SphereCollider sc = plug.AddComponent<SphereCollider>();
|
||
|
|
sc.isTrigger = true;
|
||
|
|
sc.radius = 3.2f; // ~13 см с учетом масштаба пробки
|
||
|
|
SetLayerRecursive(pan, InteractLayer);
|
||
|
|
return plug.AddComponent<OilDrainPlug>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static GameObject Prim(PrimitiveType type, Transform parent, string name, Vector3 localPos, Vector3 localScale, Material mat, Vector3? euler = null)
|
||
|
|
{
|
||
|
|
GameObject go = GameObject.CreatePrimitive(type);
|
||
|
|
go.name = name;
|
||
|
|
Collider c = go.GetComponent<Collider>();
|
||
|
|
if (c != null)
|
||
|
|
{
|
||
|
|
if (Application.isPlaying) Object.Destroy(c);
|
||
|
|
else Object.DestroyImmediate(c);
|
||
|
|
}
|
||
|
|
go.transform.SetParent(parent, false);
|
||
|
|
go.transform.localPosition = localPos;
|
||
|
|
go.transform.localRotation = Quaternion.Euler(euler ?? Vector3.zero);
|
||
|
|
go.transform.localScale = localScale;
|
||
|
|
Renderer r = go.GetComponent<Renderer>();
|
||
|
|
if (r != null && mat != null) r.sharedMaterial = mat;
|
||
|
|
return go;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void SetLayerRecursive(GameObject go, int layer)
|
||
|
|
{
|
||
|
|
foreach (Transform t in go.GetComponentsInChildren<Transform>(true))
|
||
|
|
t.gameObject.layer = layer;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Габариты всех мешей объекта в его локальных координатах.</summary>
|
||
|
|
public static Bounds LocalBounds(Transform root)
|
||
|
|
{
|
||
|
|
bool has = false;
|
||
|
|
Bounds result = new Bounds(Vector3.zero, Vector3.zero);
|
||
|
|
foreach (MeshFilter mf in root.GetComponentsInChildren<MeshFilter>(true))
|
||
|
|
{
|
||
|
|
if (mf.sharedMesh == null) continue;
|
||
|
|
Bounds mb = mf.sharedMesh.bounds;
|
||
|
|
Vector3 e = mb.extents;
|
||
|
|
for (int i = 0; i < 8; i++)
|
||
|
|
{
|
||
|
|
Vector3 corner = mb.center + new Vector3(i % 2 == 0 ? -e.x : e.x, (i / 2) % 2 == 0 ? -e.y : e.y, i / 4 == 0 ? -e.z : e.z);
|
||
|
|
Vector3 p = root.InverseTransformPoint(mf.transform.TransformPoint(corner));
|
||
|
|
if (!has)
|
||
|
|
{
|
||
|
|
result = new Bounds(p, Vector3.zero);
|
||
|
|
has = true;
|
||
|
|
}
|
||
|
|
else result.Encapsulate(p);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|