using UnityEngine; using System.Collections; using DevelopEngine; using System.Collections.Generic; using AX.TrackRecord; public class CengIDManager : SingletonMono { /// /// 只维护了室内层的所有CengID。 /// public Dictionary> CengAttriDict = new Dictionary>(); public void AddItemToDict(GameObject obj) { BuildItem cengAttri = new BuildItem(); cengAttri.floor = obj.GetComponent().cengID; cengAttri.buildType = obj.GetComponent().CengIDBuildType; /*//key值减小部分进行存储,确保后期查找时找到正确的CengID //Tips:查找CengID时是根据y值查找,找到第一个比当前y值高的层获取CengID。*/ float y_key = obj.transform.position.y/* - 0.5f*/; AddItemToDict(y_key, cengAttri); } private void AddItemToDict(float y_key, BuildItem cengAttri) { if (CengAttriDict.ContainsKey(y_key)) { CengAttriDict[y_key].Add(cengAttri); } else { CengAttriDict.Add(y_key, new List() { cengAttri }); } CengAttriDict.DictSort(); } public int GetCengIDByY(float y, BuildType buildType) { var keys = CengAttriDict.Keys; if (keys.Count < 1) { return 0; } //获取当前寻路点对应的cengid有两种情况: //1. 找y值最接近的层,这种情况是在平层上寻路 //2. 找第一个比当前y值大的key_y的前一个key_y,即为当前层 var list_y = new List();//第一种情况 foreach (var key_y in keys) { var dis = Mathf.Abs(y - key_y); if (dis < 0.2f) { list_y.Add(key_y); } } if (list_y.Count > 0)//满足情况1,处理情况1 { list_y.Sort();//升序排列 foreach (var key_y in list_y) { if (ReturnCengIDByY(key_y, buildType) != 0) { return ReturnCengIDByY(key_y, buildType); } } } else if (buildType != BuildType.None)//不满足情况1,尝试情况2 { //float ceng_key = 0; //foreach (var key_y in keys) //{ // if (key_y - y > 0.2f) // { // break; // } // ceng_key = key_y; //} //if (ReturnCengIDByY(ceng_key, buildType) != 0) //{ // return ReturnCengIDByY(ceng_key, buildType); //} } return 0; } private int ReturnCengIDByY(float key_y, BuildType buildType) { var buildItemList = CengAttriDict[key_y]; foreach (var item in buildItemList) { if (item.buildType == buildType) { return item.floor; } } return 0; } }