PlayerHelperAdon.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Adon.Game.BO;
  2. using AdonGameKit;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace Adon.Game.Helper
  7. {
  8. /// <summary>
  9. /// 玩家帮助类
  10. /// </summary>
  11. public static class PlayerHelperAdon
  12. {
  13. /// <summary>
  14. /// 攻击范围内获取最近怪物的距离
  15. /// </summary>
  16. /// <param name="attacker">攻击者</param>
  17. /// <param name="searchRange">搜索范围</param>
  18. /// <param name="mob"></param>
  19. /// <returns></returns>
  20. public static float GetNearEnemyDistance(BaseHero attacker, float searchRange, out GameObject mob)
  21. {
  22. //Collider[] monsters = Physics.OverlapSphere(attacker.transform.position, searchRange, 1 << LayerMask.NameToLayer("Enemy"));
  23. List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
  24. float num = -1f;
  25. mob = null;
  26. if (monsters.Count > 0)
  27. {
  28. for (int i = 0; i < monsters.Count; i++)
  29. {
  30. if (monsters[i] != null && monsters[i].isDie == false)
  31. {
  32. float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
  33. if (distance <= searchRange)
  34. {
  35. if (num < 0f)
  36. {
  37. num = distance;
  38. mob = monsters[i].gameObject;
  39. }
  40. else if (distance < num)
  41. {
  42. num = distance;
  43. mob = monsters[i].gameObject;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return num;
  50. }
  51. /// <summary>
  52. /// 攻击范围内怪物列表
  53. /// </summary>
  54. /// <param name="attacker">攻击者</param>
  55. /// <param name="searchRange">搜索范围</param>
  56. /// <param name="mob"></param>
  57. /// <returns></returns>
  58. public static List<GameObject> GetNearEnemysDistance(GameObject attacker, float searchRange)
  59. {
  60. List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
  61. float num = -1f;
  62. List<GameObject> mobs = new List<GameObject>();
  63. if (monsters.Count > 0)
  64. {
  65. for (int i = 0; i < monsters.Count; i++)
  66. {
  67. if (monsters[i] != null && monsters[i].mData.HP_Final > 0)
  68. {
  69. float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
  70. if (distance <= searchRange)
  71. {
  72. if (num < 0f)
  73. {
  74. num = distance;
  75. mobs.Add(monsters[i].gameObject);
  76. }
  77. else if (distance < num)
  78. {
  79. num = distance;
  80. mobs.Add(monsters[i].gameObject);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return mobs;
  87. }
  88. public static List<GameObject> GetAllEnemysDistance(GameObject attacker, float searchRange)
  89. {
  90. List<Role> monsters = RoleManager.Instance.GetLiveMonsters();
  91. List<GameObject> mobs = new List<GameObject>();
  92. if (monsters.Count > 0)
  93. {
  94. for (int i = 0; i < monsters.Count; i++)
  95. {
  96. if (monsters[i] != null && monsters[i].mData.HP_Final > 0)
  97. {
  98. float distance = GetDistance(attacker.gameObject, monsters[i].gameObject);
  99. if (distance <= searchRange)
  100. {
  101. mobs.Add(monsters[i].gameObject);
  102. }
  103. }
  104. }
  105. }
  106. return mobs;
  107. }
  108. /// <summary>
  109. /// 获取范围内敌人
  110. /// </summary>
  111. /// <param name="searchRange">搜寻范围</param>
  112. /// <param name="detectionAngle">检测角度</param>
  113. /// <param name="hero">英雄对象</param>
  114. /// <returns></returns>
  115. public static List<GameObject> GetNearMonsters(float searchRange, float detectionAngle, GameObject hero)
  116. {
  117. List<GameObject> monsters = new List<GameObject>();
  118. if (RoleManager.Instance.GetLiveMonsters().Count > 0)//存在的怪
  119. {
  120. for (int i = 0; i < RoleManager.Instance.GetLiveMonsters().Count; i++)//遍历列表
  121. {
  122. GameObject monsterObj = RoleManager.Instance.GetLiveMonsters()[i].gameObject;
  123. float distance = GetDistance(hero, monsterObj);//怪物与英雄的距离
  124. if (distance <= searchRange)//距离小于等于搜寻范围
  125. {
  126. Vector3 eyePos = hero.transform.position;
  127. Vector3 toMonster = monsterObj.transform.position - eyePos;//向量
  128. if (Vector3.Dot(toMonster.normalized, hero.transform.forward) > Mathf.Cos(detectionAngle * 0.5f * Mathf.Deg2Rad))
  129. {
  130. Debug.DrawRay(eyePos, toMonster, Color.blue);//绘制视线
  131. monsters.Add(monsterObj);
  132. }
  133. }
  134. }
  135. }
  136. return monsters;
  137. }
  138. /// <summary>
  139. /// 获取怪物周围的英雄
  140. /// </summary>
  141. /// <param name="searchRange">搜寻范围</param>
  142. /// <param name="mob">怪物对象</param>
  143. /// <returns></returns>
  144. public static List<GameObject> GetNearEnemyHero(float searchRange, float detectionAngle, GameObject mob)
  145. {
  146. List<GameObject> heros = new List<GameObject>();
  147. if (RoleManager.Instance.GetPlayer() != null)
  148. {
  149. GameObject heroObj = RoleManager.Instance.GetPlayer().gameObject;
  150. float distance = GetDistance(mob, heroObj);//怪物与英雄的距离
  151. if (distance <= searchRange)//距离小于等于搜寻范围
  152. {
  153. Vector3 eyePos = mob.transform.position;
  154. Vector3 toPlayer = heroObj.transform.position - eyePos;//向量
  155. if (Vector3.Dot(toPlayer.normalized, mob.transform.forward) > Mathf.Cos(detectionAngle * 0.5f * Mathf.Deg2Rad))
  156. {
  157. Debug.DrawRay(eyePos, toPlayer, Color.blue);//绘制视线
  158. heros.Add(heroObj);
  159. }
  160. }
  161. }
  162. if (heros.Count > 0)
  163. {
  164. return heros;
  165. }
  166. else
  167. {
  168. return null;
  169. }
  170. }
  171. /// <summary>
  172. /// 获取敌人近身位置
  173. /// </summary>
  174. /// <param name="src">自身对象</param>
  175. /// <param name="enemy">敌人对象</param>
  176. /// <param name="range">范围</param>
  177. /// <param name="angle">角度</param>
  178. /// <returns></returns>
  179. public static Vector3 GetEnemyNearPosition(GameObject src, GameObject enemy, float range, float angle)
  180. {
  181. Vector3 vector = enemy.transform.position - src.transform.position;//目标向量
  182. vector.Normalize();
  183. vector = vector * range;
  184. return Quaternion.AngleAxis(angle, Vector3.up) * vector + enemy.transform.position;
  185. }
  186. /// <summary>
  187. /// 获取对象之间的距离
  188. /// </summary>
  189. /// <param name="src"></param>
  190. /// <param name="target"></param>
  191. /// <returns></returns>
  192. public static float GetDistance(GameObject src, GameObject target)
  193. {
  194. return Vector3.Distance(src.transform.position, target.transform.position);
  195. }
  196. //internal static float GetNearEnemyDistance(BaseCharacterAdon m_BaseCharacterAdon, float m_AutoAttackRange, out GameObject targetMonster)
  197. //{
  198. // throw new NotImplementedException();
  199. //}
  200. /// <summary>
  201. /// 计算夹角的角度 0~360
  202. /// </summary>
  203. /// <param name="from_"></param>
  204. /// <param name="to_"></param>
  205. /// <returns></returns>
  206. public static float Angle_360(Vector3 from_, Vector3 to_)
  207. {
  208. Vector3 v3 = Vector3.Cross(from_, to_);
  209. if (v3.y > 0)
  210. return Vector3.Angle(from_, to_);
  211. else
  212. return 360 - Vector3.Angle(from_, to_);
  213. }
  214. }
  215. }