203 lines
5.4 KiB
C#
203 lines
5.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using EVP;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class CarInfoManager : MonoBehaviour
|
|
{
|
|
public VehicleController target;
|
|
|
|
|
|
[Header("左向灯")] public GameObject leftIndicator; // 左向灯
|
|
[Header("右向灯")] public GameObject rightIndicator; // 右向灯
|
|
[Header("前照灯")] public GameObject headlights; // 前照灯
|
|
[Header("雨刷器")] public GameObject windshieldWipers; // 雨刷器
|
|
[Header("喷水")] public GameObject waterSpray; // 喷水
|
|
[Header("停车")] public GameObject parking; // 停车
|
|
[Header("安全带")] public GameObject seatbelt; // 安全带
|
|
[Header("发动机油压")] public GameObject engineOilPressure; // 发动机油压
|
|
[Header("蓄电池")] public GameObject batteryCharge; // 蓄电池
|
|
[Header("油车转速")] public TMP_Text rpm; // 油车转速
|
|
[Header("高亮车灯")] public GameObject highBeamLights; // 高亮车灯
|
|
[Header("公里数")] public TMP_Text odometer; // 公里数
|
|
[Header("油量")] public TMP_Text fuelLevel; // 油量
|
|
|
|
private bool isBlinking = false;
|
|
private bool stopRequested = false;
|
|
private CancellationTokenSource cancellationTokenSource;
|
|
|
|
private GameObject Indicator;
|
|
|
|
#region 耗油量
|
|
|
|
public float fuelLevelValue = 100f; // 初始油量
|
|
private float consumptionRate = 50f; // 每公里的油耗
|
|
public float totalDistanceTraveled = 0f; // 总行驶距离
|
|
private float speed; // 时速(假设其他脚本会更新这个值)
|
|
|
|
#endregion
|
|
|
|
|
|
private void Start()
|
|
{
|
|
// StartBlinking(leftIndicator);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开车灯
|
|
/// </summary>
|
|
public void OpenHighBeamLights()
|
|
{
|
|
if (highBeamLights.activeInHierarchy)
|
|
{
|
|
highBeamLights.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
highBeamLights.SetActive(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开雨刷器
|
|
/// </summary>
|
|
public void OpenWindshieldWipers()
|
|
{
|
|
if (windshieldWipers.activeInHierarchy)
|
|
{
|
|
windshieldWipers.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
windshieldWipers.SetActive(true);
|
|
}
|
|
}
|
|
|
|
// private IEnumerator Start()
|
|
// {
|
|
// StartBlinking(leftIndicator);
|
|
// yield return new WaitForSeconds(2);
|
|
// StopBlinking(leftIndicator);
|
|
// StartBlinking(rightIndicator);
|
|
// }
|
|
|
|
private void Update()
|
|
{
|
|
if (target.speed < 0)
|
|
{
|
|
rpm.text = Convert.ToInt32(Math.Abs(target.speed)).ToString();
|
|
odometer.text = Convert.ToInt32((Math.Abs(target.speed) * 3.6f)).ToString();
|
|
speed = Math.Abs(target.speed);
|
|
}
|
|
else
|
|
{
|
|
rpm.text = Convert.ToInt32(target.speed).ToString();
|
|
odometer.text = Convert.ToInt32((target.speed * 3.6f)).ToString();
|
|
speed = target.speed;
|
|
}
|
|
|
|
|
|
#region 计算耗油量
|
|
|
|
// 计算这一帧行驶的距离
|
|
float distanceTraveled = speed * Time.deltaTime / 3600f; // 将速度从时速转换为每秒,并计算行驶的距离
|
|
float fuelConsumed = distanceTraveled * consumptionRate;
|
|
|
|
// 更新油量
|
|
fuelLevelValue -= fuelConsumed;
|
|
if (fuelLevelValue < 0) fuelLevelValue = 0;
|
|
|
|
fuelLevel.text = Convert.ToInt32(fuelLevelValue).ToString() + "%";
|
|
|
|
// 可以在这里添加油量显示逻辑
|
|
// Debug.Log("Current Fuel Level: " + fuelLevelValue);
|
|
// Debug.Log("Total Distance Traveled: " + totalDistanceTraveled + " km");
|
|
|
|
#endregion
|
|
}
|
|
|
|
#region 转向灯动画
|
|
|
|
public void LeftStartBlinking()
|
|
{
|
|
Indicator = leftIndicator;
|
|
StartBlinking(Indicator);
|
|
}
|
|
|
|
public void RightStartBlinking()
|
|
{
|
|
Indicator = rightIndicator;
|
|
StartBlinking(Indicator);
|
|
}
|
|
|
|
public void StartBlinking(GameObject game)
|
|
{
|
|
// 如果已经有一个闪烁过程在运行,则先取消它
|
|
|
|
if (isBlinking)
|
|
return;
|
|
|
|
|
|
StopBlinking();
|
|
|
|
if (!isBlinking)
|
|
isBlinking = true;
|
|
|
|
cancellationTokenSource?.Cancel();
|
|
cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
BlinkAsync(cancellationTokenSource.Token, game);
|
|
}
|
|
|
|
// 停止闪烁的方法
|
|
public void StopBlinking()
|
|
{
|
|
|
|
if(!isBlinking)
|
|
return;
|
|
|
|
isBlinking = false;
|
|
if (cancellationTokenSource != null)
|
|
{
|
|
cancellationTokenSource.Cancel();
|
|
cancellationTokenSource = null;
|
|
}
|
|
|
|
Indicator.SetActive(false);
|
|
}
|
|
|
|
// 控制闪烁的异步方法
|
|
private async void BlinkAsync(CancellationToken token, GameObject game)
|
|
{
|
|
try
|
|
{
|
|
// 初始隐藏物体
|
|
game.SetActive(true);
|
|
|
|
while (true)
|
|
{
|
|
await Task.Delay(500, token); // 使用取消令牌的延迟
|
|
if (token.IsCancellationRequested)
|
|
return;
|
|
|
|
game.SetActive(!game.activeSelf); // 切换显示/隐藏状态
|
|
}
|
|
}
|
|
catch (TaskCanceledException)
|
|
{
|
|
// 处理取消时的逻辑(如果需要)
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void OnDestroy()
|
|
{
|
|
cancellationTokenSource.Cancel();
|
|
cancellationTokenSource = null;
|
|
}
|
|
} |