121 lines
5.1 KiB
C#
121 lines
5.1 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Сливная пробка масляного поддона. Доступна, когда передняя часть авто поднята домкратом:
|
|
/// старое масло сливается в таз, после чего свежее заливается сверху из канистры.
|
|
/// </summary>
|
|
public class OilDrainPlug : MonoBehaviour, IInteractable
|
|
{
|
|
private CarFluids fluids;
|
|
private bool draining;
|
|
|
|
private void Awake()
|
|
{
|
|
fluids = GetComponentInParent<CarFluids>();
|
|
}
|
|
|
|
private bool IsFrontLifted()
|
|
{
|
|
VehicleInteraction car = GetComponentInParent<VehicleInteraction>();
|
|
if (car == null) return true;
|
|
|
|
foreach (CarPart part in car.GetComponentsInChildren<CarPart>(true))
|
|
{
|
|
if (part.TotalBoltsCount == 0 && part.LinkedWheelCollider == null) continue;
|
|
Vector3 local = car.transform.InverseTransformPoint(part.transform.position);
|
|
if (local.z > 0f && CarJack.IsWheelLifted(part.transform)) return true;
|
|
}
|
|
|
|
foreach (WheelCollider wc in car.GetComponentsInChildren<WheelCollider>(true))
|
|
{
|
|
Vector3 local = car.transform.InverseTransformPoint(wc.transform.position);
|
|
if (local.z > 0f && CarJack.IsWheelLifted(wc.transform)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public string GetPrompt()
|
|
{
|
|
if (fluids == null) fluids = GetComponentInParent<CarFluids>();
|
|
if (fluids == null) return "Сливная пробка картера";
|
|
if (draining) return "Сливаем старое масло в таз...";
|
|
|
|
float liters = fluids.EngineOil.currentLiters;
|
|
if (liters < 0.05f)
|
|
return "Масло слито. Закрутите пробку и залейте свежее масло сверху через горловину";
|
|
|
|
if (!IsFrontLifted())
|
|
return "Сливная пробка картера\n<color=#F2A93B>Поднимите переднюю часть авто домкратом, чтобы добраться до пробки</color>";
|
|
|
|
return $"[E] Открутить сливную пробку и слить масло ({liters:F1} л, состояние {Mathf.RoundToInt(fluids.EngineOil.quality * 100f)}%)";
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (draining || fluids == null || fluids.EngineOil.currentLiters < 0.05f || !IsFrontLifted()) return;
|
|
StartCoroutine(DrainRoutine());
|
|
}
|
|
|
|
private IEnumerator DrainRoutine()
|
|
{
|
|
draining = true;
|
|
|
|
// Таз под пробкой
|
|
Vector3 ground = transform.position + Vector3.down * 0.6f;
|
|
if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 2f, ~0, QueryTriggerInteraction.Ignore))
|
|
ground = hit.point;
|
|
|
|
GameObject basin = EnginePartFactory.Prim(PrimitiveType.Cylinder, null, "OilBasin", ground + Vector3.up * 0.04f,
|
|
new Vector3(0.5f, 0.04f, 0.5f), EnginePartFactory.Metal);
|
|
GameObject oilSurface = EnginePartFactory.Prim(PrimitiveType.Cylinder, basin.transform, "Oil", new Vector3(0f, 0.2f, 0f),
|
|
new Vector3(0.9f, 0.1f, 0.9f), EnginePartFactory.Black);
|
|
oilSurface.transform.localScale = new Vector3(0.9f, 0.05f, 0.9f);
|
|
|
|
ParticleSystem stream = CreateStream();
|
|
ProceduralAudioManager.Instance?.Play(SfxId.OilDrain, transform.position, 0.8f);
|
|
|
|
float total = fluids.EngineOil.currentLiters;
|
|
float t = 0f;
|
|
const float duration = 3f;
|
|
while (t < duration)
|
|
{
|
|
t += Time.deltaTime;
|
|
fluids.EngineOil.currentLiters = Mathf.Lerp(total, 0f, t / duration);
|
|
oilSurface.transform.localPosition = new Vector3(0f, Mathf.Lerp(-0.3f, 0.6f, t / duration), 0f);
|
|
yield return null;
|
|
}
|
|
|
|
fluids.DrainEngineOil();
|
|
if (stream != null) Destroy(stream.gameObject, 1f);
|
|
Destroy(basin, 25f);
|
|
draining = false;
|
|
GameHUD.Notify("Старое масло слито. Залейте свежее моторное масло из желтой канистры", GameUI.Accent);
|
|
}
|
|
|
|
private ParticleSystem CreateStream()
|
|
{
|
|
GameObject pObj = new GameObject("OilStream");
|
|
pObj.transform.position = transform.position;
|
|
pObj.transform.rotation = Quaternion.LookRotation(Vector3.down);
|
|
ParticleSystem ps = pObj.AddComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.startLifetime = 0.35f;
|
|
main.startSpeed = 1.6f;
|
|
main.startSize = 0.03f;
|
|
main.startColor = new Color(0.12f, 0.08f, 0.02f, 1f);
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
main.gravityModifier = 1f;
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 80f;
|
|
var shape = ps.shape;
|
|
shape.shapeType = ParticleSystemShapeType.Cone;
|
|
shape.angle = 3f;
|
|
shape.radius = 0.01f;
|
|
var r = pObj.GetComponent<ParticleSystemRenderer>();
|
|
r.sharedMaterial = PartCatalog.ParticleMaterial;
|
|
ps.Play();
|
|
return ps;
|
|
}
|
|
}
|