/// <summary> /// 获取距离我最近的对手 /// </summary> /// <param name="sprite"></param> /// <returns></returns> public XLSpriteBase getCloselyMatchSprite(XLSpriteBase sprite, XLSpriteBase ignoreSprite) { if (sprite == null) { return(null); } List <XLSpriteBase> matchSprites = null; if (sprite.Country == Defaults.Country.Enemy) { matchSprites = _selfSoldiers; } else if (sprite.Country == Defaults.Country.Me || sprite.Country == Defaults.Country.Friend) { matchSprites = _enemySoldiers; } if (matchSprites == null) { return(null); } XLSpriteBase ret = null; float retDistance = -1; for (int i = 0; i < matchSprites.Count; i++) { XLSpriteBase matchSprite = matchSprites[i]; if (!matchSprite.isCanBeTarget()) { continue; } // 如果对手就只有一个那就不用换目标了 && 更换目标 if (matchSprites.Count > 1 && matchSprite == ignoreSprite) { continue; } float matchDistance = Vector3.Distance(sprite.transform.position, matchSprite.transform.position); if (ret == null || matchDistance < retDistance) { ret = matchSprite; retDistance = matchDistance; continue; } } return(ret); }
/// <summary> /// 获取距离我最近的对手 /// </summary> /// <param name="sprite"></param> /// <returns></returns> public XLSpriteBase getCloselyMatchSprite1(XLSpriteBase sprite, XLSpriteBase ignoreSprite) { if (sprite == null) { return(null); } List <XLSpriteBase> matchSprites = null; if (sprite.Country == Defaults.Country.Enemy) { matchSprites = _selfSoldiers; } else if (sprite.Country == Defaults.Country.Me || sprite.Country == Defaults.Country.Friend) { matchSprites = _enemySoldiers; } if (matchSprites == null) { return(null); } XLSpriteBase ret = null; float retDistance = -1; List <float> distArray = new List <float>(matchSprites.Count); //float[] distArray = new float[matchSprites.Count]; float maxDist = 100000f; float totalHP = 0; for (int i = 0; i < matchSprites.Count; i++) { XLSpriteBase matchSprite = matchSprites[i]; if (!matchSprite.isCanBeTarget()) { distArray.Add(maxDist); continue; } // 如果对手就只有一个那就不用换目标了 && 更换目标 if (matchSprites.Count > 1 && matchSprite == ignoreSprite) { distArray.Add(maxDist); continue; } float matchDistance = Vector3.Distance(sprite.transform.position, matchSprite.transform.position); distArray.Add(matchDistance); totalHP += matchSprite._data.HP; } // 对距离进行排序,然后在距离最短的几个里面找一个血量最小的攻击 var sorted = distArray.Select((x, i) => new KeyValuePair <float, int>(x, i)).OrderBy(x => x.Key).ToList(); List <float> sortedDistArray = sorted.Select(x => x.Key).ToList(); List <int> idx = sorted.Select(x => x.Value).ToList(); float minRatio = 1; //if (sprite._data.Country == Defaults.Country.Me) // 只有我方士兵才适用这种寻敌方式 if (true) // 双方士兵都用这种寻敌方式 { //Debug.Log("xueliang--------"); for (int i = 0; i < Math.Min(8, distArray.Count); i++) // 以8人为小组单位,在里面选一个血量最小的士兵进行攻击。 { float ratio = (float)matchSprites[idx[i]]._data.HP / (float)sprite._data.HP; // *UnityEngine.Random.Range(0.1f, 1); if (ret == null || ratio < minRatio) { ret = matchSprites[idx[i]]; minRatio = ratio; continue; } } } else { ret = matchSprites[idx[0]]; } // 调试用的,用来显示最后有多少士兵存活,总共还剩多少血量 //string strTotal = String.Format("#.{0},HP={1}", matchSprites.Count, totalHP); //if(matchSprites[0]._data.Country != Defaults.Country.Me) // Debug.Log(strTotal); return(ret); }