|
|
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;
|
|
|
}
|
|
|
}
|
|
|
}
|