|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
|
|
|
public class RoadSys : MonoBehaviour
|
|
|
{
|
|
|
|
|
|
/// 路段检测数据字典
|
|
|
public Dictionary<string, int> roadCheckerDict = new Dictionary<string, int>();
|
|
|
|
|
|
/// 2D地图中路段状态的对象的字典
|
|
|
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;//监控的车辆数量
|
|
|
MeshRenderer renderer = state_object.GetComponent<MeshRenderer>();
|
|
|
|
|
|
if (numble >= 0 && numble <= Const.RoadCheckNumber)//绿色状态
|
|
|
{
|
|
|
renderer.material.SetInt("_ColorIndex",0);
|
|
|
}
|
|
|
else if(numble > Const.RoadCheckNumber && numble <= Const.RoadCheckNumber*2)//黄色状态
|
|
|
{
|
|
|
renderer.material.SetInt("_ColorIndex", 1);
|
|
|
}
|
|
|
else if (numble > Const.RoadCheckNumber*2)//红色拥堵
|
|
|
{
|
|
|
renderer.material.SetInt("_ColorIndex", 2);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Debug.LogError("没有获取到状态表示器:" + item.Key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
timer = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|