// 查找与主角最近的对象 public T FindNearstEntity <T>(IFindCondition <T> condition) { if (m_ClientGlobal == null) { return(default(T)); } if (m_ClientGlobal.MainPlayer == null) { return(default(T)); } Vector3 pos = m_ClientGlobal.MainPlayer.GetPos(); List <T> lstEntity = new List <T>(); FindAllEntity <T>(ref lstEntity); float fMinDis = 5000000.0f; T ret = default(T); for (int i = 0; i < lstEntity.Count; ++i) { if (lstEntity[i] == null) { continue; } IEntity en = lstEntity[i] as IEntity; if (en != null) { if (en.GetUID() == m_ClientGlobal.MainPlayer.GetUID()) { continue; } } if (condition != null) { if (!condition.Conform(lstEntity[i])) { continue; } } if (en != null) { if (en.GetUID() != m_ClientGlobal.MainPlayer.GetUID()) { float fDis = Vector3.SqrMagnitude(pos - en.GetPos()); if (fDis < fMinDis) { fMinDis = fDis; ret = lstEntity[i]; } } } } lstEntity.Clear(); lstEntity = null; return(ret); }
// 根据一定的范围查找对象 范围判定在IFindCondition里面实现 public void FindEntityRange <T>(IFindCondition <T> condition, ref List <T> lst) { Profiler.BeginSample("FindEntityRange"); lst.Clear(); if (m_ClientGlobal == null) { return; } if (m_ClientGlobal.MainPlayer == null) { return; } Profiler.BeginSample("typeof"); Type key = typeof(T); Profiler.EndSample(); object lstObj = null; if (!m_dicObj.TryGetValue(key, out lstObj)) {//代码优化 (有问题可以先还原)去掉每次3k的开销 modify by zhudianyu lstObj = new List <T>(100); m_dicObj.Add(key, lstObj); } List <T> lstEntity = lstObj as List <T>; lstEntity.Clear(); Profiler.BeginSample("FindAllEntity"); FindAllEntity <T>(ref lstEntity); Profiler.EndSample(); for (int i = 0; i < lstEntity.Count; ++i) { if (lstEntity[i] == null) { continue; } IEntity en = lstEntity[i] as IEntity; if (en != null) { if (en.GetUID() == m_ClientGlobal.MainPlayer.GetUID()) { continue; } } if (condition != null) { Profiler.BeginSample("condition"); // 符合条件,加入输出列表 if (condition.Conform(lstEntity[i])) { lst.Add(lstEntity[i]); } Profiler.EndSample(); } } lstEntity.Clear(); // lstEntity = null; Profiler.EndSample(); }