H_SafeExperienceDrivingSystem/U3D_DrivingSystem/Assets/Script/TrafficLightManager.cs

212 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
public enum TrafficLightState
{
Red,
Yellow,
Green
}
public class TrafficLightManager : MonoBehaviour
{
[System.Serializable]
public class TrafficLightGroup
{
public List<TMP_Text> lights = new List<TMP_Text>();
public bool startsWithGreen; // 标记组是从绿灯开始还是红灯开始
public float timer; // 计时器
public TrafficLightState currentState;
public TrafficLightState previousState; // 新增字段,用于保存黄灯之前的状态
public List<GameObject> trafficLights = new List<GameObject>();
public List<GameObject> roadEventTrigger = new List<GameObject>();
public void Initialize()
{
// 绿灯开始则设为10秒否则设为39秒
currentState = startsWithGreen ? TrafficLightState.Green : TrafficLightState.Red;
previousState = currentState; // 初始化时,之前的状态与当前状态相同
timer = startsWithGreen ? 10f : 39;
}
public void UpdateTrafficLightTexture(TrafficLightState state)
{
// Debug.Log("123");
foreach (var light in trafficLights)
{
if (light != null)
{
// 假设你有一个方法来根据状态获取相应的贴图
//Texture newTexture = GetTextureForState(state);
light.transform.Find("red2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
light.transform.Find("green2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
}
}
switch (state)
{
case TrafficLightState.Red:
foreach (var light in trafficLights)
{
if (light != null)
{
// 假设你有一个方法来根据状态获取相应的贴图
//Texture newTexture = GetTextureForState(state);
light.transform.Find("red2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
}
}
foreach (var v in roadEventTrigger)
{
v.SetActive(true);
}
break;
case TrafficLightState.Yellow:
foreach (var light in trafficLights)
{
if (light != null)
{
// 假设你有一个方法来根据状态获取相应的贴图
//Texture newTexture = GetTextureForState(state);
light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
}
}
break;
case TrafficLightState.Green:
foreach (var light in trafficLights)
{
if (light != null)
{
// 假设你有一个方法来根据状态获取相应的贴图
//Texture newTexture = GetTextureForState(state);
light.transform.Find("green2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
}
}
foreach (var v in roadEventTrigger)
{
v.SetActive(false);
}
// AddRedLightWarning();
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
// private Texture GetTextureForState(TrafficLightState state)
// {
// // 返回根据 state 获取的贴图
// // 这里你需要根据你的资源来实现具体的逻辑
// }
}
public List<TrafficLightGroup> lightGroups;
void Start()
{
// 初始化每组红绿灯的计时器
foreach (var group in lightGroups)
{
group.Initialize();
}
}
void FixedUpdate()
{
// Debug.Log("Delta Time: " + Time.deltaTime);
foreach (var group in lightGroups)
{
// Debug.Log("Before: " + group.timer);
// group.timer -= Time.deltaTime;
// Debug.Log("After: " + group.timer);
// 更新红绿灯状态
UpdateTrafficLightGroup(group);
}
}
void AddRedLightWarning(TrafficLightGroup group)
{
// 在这里添加提示或更改状态的逻辑
// 例如,你可以在红灯状态下播放声音或显示警告信息
// 以下是一个示例,在控制台输出警告信息:
Debug.LogWarning("红灯警告:请注意停车!");
}
void UpdateTrafficLightGroup(TrafficLightGroup group)
{
group.timer -= Time.deltaTime;
if (group.timer <= 0f)
{
switch (group.currentState)
{
case TrafficLightState.Green:
group.previousState = TrafficLightState.Green;
group.currentState = TrafficLightState.Yellow;
group.timer = 3f; // 绿灯到黄灯的持续时间
break;
case TrafficLightState.Yellow:
group.currentState = TrafficLightState.Red;
group.timer = group.startsWithGreen ? 39 : 20; // 黄灯到红灯的持续时间
break;
case TrafficLightState.Red:
group.previousState = TrafficLightState.Red;
group.currentState = TrafficLightState.Green;
group.timer = 10f; // 红灯到绿灯的持续时间
break;
}
}
// 更新交通灯显示
UpdateLights(group.lights, group.currentState.ToString(), group.timer);
group.UpdateTrafficLightTexture(group.currentState);
}
void UpdateLights(List<TMP_Text> lights, string state, float time)
{
int timeInt = Mathf.CeilToInt(time);
// 使用条件运算符来判断是否需要在前面加上 0
string formattedTime = timeInt < 10 ? "0" + timeInt.ToString() : timeInt.ToString();
string text = formattedTime;
Color color = Color.white;
switch (state)
{
case "Red":
color = Color.red;
break;
case "Yellow":
color = Color.yellow;
break;
case "Green":
color = Color.green;
break;
}
foreach (var light in lights)
{
if (light != null)
{
light.text = text;
light.color = color;
}
}
}
}