99 lines
3.7 KiB
C#
99 lines
3.7 KiB
C#
using UnityEngine;
|
|||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Фары автомобиля: клавиша L в машине переключает Выключено → Ближний → Дальний свет.
|
||
|
|
/// Прожекторы создаются автоматически по габаритам кузова; в темноте освещают дорогу.
|
||
|
|
/// </summary>
|
||
|
|
[DisallowMultipleComponent]
|
||
|
|
public class CarLightingController : MonoBehaviour
|
||
|
|
{
|
||
|
|
public enum HeadlightMode { Off, Low, High }
|
||
|
|
|
||
|
|
[SerializeField] private KeyCode toggleKey = KeyCode.L;
|
||
|
|
[SerializeField] private float lowRange = 32f, highRange = 75f;
|
||
|
|
[SerializeField] private float lowAngle = 75f, highAngle = 48f;
|
||
|
|
[SerializeField] private float lowIntensity = 6f, highIntensity = 14f;
|
||
|
|
|
||
|
|
public HeadlightMode Mode { get; private set; } = HeadlightMode.Off;
|
||
|
|
|
||
|
|
private Light[] headlights;
|
||
|
|
private Light[] tailLights;
|
||
|
|
private VehicleInteraction vehicle;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
vehicle = GetComponent<VehicleInteraction>();
|
||
|
|
BuildLights();
|
||
|
|
Apply();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void BuildLights()
|
||
|
|
{
|
||
|
|
Bounds b = EngineBayManager.LocalRendererBounds(transform, transform, null);
|
||
|
|
float y = b.min.y + b.size.y * 0.42f;
|
||
|
|
float x = b.size.x * 0.34f;
|
||
|
|
|
||
|
|
headlights = new Light[2];
|
||
|
|
tailLights = new Light[2];
|
||
|
|
for (int i = 0; i < 2; i++)
|
||
|
|
{
|
||
|
|
float side = i == 0 ? -1f : 1f;
|
||
|
|
|
||
|
|
GameObject h = new GameObject(i == 0 ? "Headlight_L" : "Headlight_R");
|
||
|
|
h.transform.SetParent(transform, false);
|
||
|
|
h.transform.localPosition = new Vector3(b.center.x + side * x, y, b.max.z - 0.05f);
|
||
|
|
h.transform.localRotation = Quaternion.Euler(6f, 0f, 0f);
|
||
|
|
Light l = h.AddComponent<Light>();
|
||
|
|
l.type = LightType.Spot;
|
||
|
|
l.color = new Color(1f, 0.95f, 0.82f);
|
||
|
|
l.shadows = LightShadows.None;
|
||
|
|
headlights[i] = l;
|
||
|
|
|
||
|
|
GameObject t = new GameObject(i == 0 ? "TailLight_L" : "TailLight_R");
|
||
|
|
t.transform.SetParent(transform, false);
|
||
|
|
t.transform.localPosition = new Vector3(b.center.x + side * x, y + 0.05f, b.min.z - 0.1f);
|
||
|
|
Light tl = t.AddComponent<Light>();
|
||
|
|
tl.type = LightType.Point;
|
||
|
|
tl.color = new Color(1f, 0.1f, 0.05f);
|
||
|
|
tl.range = 3.5f;
|
||
|
|
tl.intensity = 1.5f;
|
||
|
|
tl.shadows = LightShadows.None;
|
||
|
|
tailLights[i] = tl;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
if (vehicle == null || !vehicle.isPlayerInside || GameInputLock.IsLocked) return;
|
||
|
|
if (Input.GetKeyDown(toggleKey))
|
||
|
|
{
|
||
|
|
Mode = (HeadlightMode)(((int)Mode + 1) % 3);
|
||
|
|
Apply();
|
||
|
|
ProceduralAudioManager.Instance?.Play(SfxId.RelayClick, transform.position, 0.3f, 1.6f);
|
||
|
|
string text = Mode == HeadlightMode.Off ? "Фары выключены" : (Mode == HeadlightMode.Low ? "Ближний свет" : "Дальний свет");
|
||
|
|
GameHUD.Notify(text, GameUI.Muted);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SetMode(HeadlightMode mode)
|
||
|
|
{
|
||
|
|
Mode = mode;
|
||
|
|
Apply();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Apply()
|
||
|
|
{
|
||
|
|
bool on = Mode != HeadlightMode.Off;
|
||
|
|
foreach (Light l in headlights)
|
||
|
|
{
|
||
|
|
if (l == null) continue;
|
||
|
|
l.enabled = on;
|
||
|
|
l.range = Mode == HeadlightMode.High ? highRange : lowRange;
|
||
|
|
l.spotAngle = Mode == HeadlightMode.High ? highAngle : lowAngle;
|
||
|
|
l.intensity = Mode == HeadlightMode.High ? highIntensity : lowIntensity;
|
||
|
|
l.transform.localRotation = Quaternion.Euler(Mode == HeadlightMode.High ? 2f : 7f, 0f, 0f);
|
||
|
|
}
|
||
|
|
foreach (Light l in tailLights)
|
||
|
|
if (l != null) l.enabled = on;
|
||
|
|
}
|
||
|
|
}
|