using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoadSys : MonoBehaviour
{
///
/// 路段检测数据字典
///
public Dictionary roadCheckerDict = new Dictionary();
///
/// 2D地图中路段状态的对象的字典
///
public Dictionary roadStateDict = new Dictionary();
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
}
}
}
///
/// 路段字典键值修改
///
///
///
public void SetCheckerValue(string roadName,int value)
{
if (roadCheckerDict.ContainsKey(roadName))
{
//改变值
int numble = roadCheckerDict[roadName];
numble += value;
//加入字典
roadCheckerDict.Remove(roadName);
roadCheckerDict.Add(roadName, numble);
}
}
///
/// 路况更新函数
///
/// 间隔时间
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();
if (numble >= 0 && numble <= 4)//绿色状态
{
renderer.material.SetInt("_ColorIndex",0);
}
else if(numble > 4 && numble <= 8)//黄色状态
{
renderer.material.SetInt("_ColorIndex", 1);
}
else if (numble > 8 )//红色拥堵
{
renderer.material.SetInt("_ColorIndex", 2);
}
}
else
{
Debug.LogError("没有获取到状态表示器:" + item.Key);
}
}
timer = 0;
}
}
}