You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoadSys : MonoBehaviour
{
/// <summary>
/// 路段检测数据字典
/// </summary>
public Dictionary<string, int> roadCheckerDict = new Dictionary<string, int>();
/// <summary>
/// 2D地图中路段状态的对象的字典
/// </summary>
public Dictionary<string, GameObject> roadStateDict = new Dictionary<string, GameObject>();
private float timer = 0;
public void Init()
{
InitChecker();
}
//检索检测器和2D地图路段状态器
public void InitChecker()
{
//检测器
GameObject[] checkers = GameObject.FindGameObjectsWithTag("CarFlowChecker");
for (int i = 0; i < checkers.Length; i++)
{
if (checkers[i] != null)
{
roadCheckerDict.Add(checkers[i].name, 0);//初始化阶段车辆数量默认0
}
}
///状态器
GameObject[] states= GameObject.FindGameObjectsWithTag("CarFlowState");
for (int i = 0; i < states.Length; i++)
{
if (states[i] != null)
{
roadStateDict.Add(states[i].name, states[i]);//初始化阶段车辆数量默认0
}
}
}
/// <summary>
/// 路段字典键值修改
/// </summary>
/// <param name="roadName"></param>
/// <param name="value"></param>
public void SetCheckerValue(string roadName,int value)
{
if (roadCheckerDict.ContainsKey(roadName))
{
//改变值
int numble = roadCheckerDict[roadName];
numble += value;
//加入字典
roadCheckerDict.Remove(roadName);
roadCheckerDict.Add(roadName, numble);
}
}
/// <summary>
/// 路况更新函数
/// </summary>
/// <param name="time">间隔时间</param>
public void RoadStateUpdate(float time)
{
timer += Time.deltaTime;
if (timer >= time)
{
foreach (var item in roadCheckerDict)
{
GameObject state_object = null;
//获取到状态表示器
if(roadStateDict.TryGetValue(item.Key, out state_object))
{
int numble = item.Value;//监控的车辆数量
if (numble >= 0 && numble <= 6)//绿色状态
{
state_object.GetComponent<MeshRenderer>().material.color = Color.green;
}
else if(numble > 6 && numble <= 12)//黄色状态
{
state_object.GetComponent<MeshRenderer>().material.color = Color.yellow;
}
else if (numble > 12 )//红色拥堵
{
state_object.GetComponent<MeshRenderer>().material.color = Color.red;
}
}
else
{
Debug.LogError("没有获取到状态表示器:" + item.Key);
}
}
timer = 0;
}
}
}