TargetSelector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame;
  5. using XGame.Framework;
  6. namespace FL.Battle
  7. {
  8. /// <summary>
  9. /// 目标选择组件
  10. /// </summary>
  11. public class TargetSelector : ITargetSelector
  12. {
  13. struct FindTargetArgs
  14. {
  15. public EEntityType targetType;
  16. public ETargetFindType findType;
  17. /// <summary>
  18. /// 目标选择回调函数,同步查找目标时该方法无效
  19. /// <选中的目标>
  20. /// </summary>
  21. public Action<ITarget> onSelected;
  22. }
  23. private Dictionary<EEntityType, HashSet<ITarget>> _typeToTargetsMap;
  24. private Dictionary<long, ITarget> _idToTargetMap;
  25. private Dictionary<ITarget, FindTargetArgs> _waitingMap;
  26. private List<ITarget> _tempLst = new List<ITarget>();
  27. private long _playerEntityId;
  28. public void Register(ITarget target)
  29. {
  30. if (_typeToTargetsMap == null)
  31. {
  32. _typeToTargetsMap = new Dictionary<EEntityType, HashSet<ITarget>>();
  33. _idToTargetMap = new Dictionary<long, ITarget>();
  34. }
  35. var type = target.Entity.EntityType;
  36. if (!_typeToTargetsMap.TryGetValue(type, out var targets))
  37. {
  38. targets = new HashSet<ITarget>();
  39. _typeToTargetsMap.Add(type, targets);
  40. }
  41. targets.Add(target);
  42. _idToTargetMap.Add(target.Entity.EntityId, target);
  43. if (target.Entity.EntityType == EEntityType.Player)
  44. {
  45. _playerEntityId = target.Entity.EntityId;
  46. }
  47. }
  48. public void Unregister(ITarget target)
  49. {
  50. if (_typeToTargetsMap == null) return;
  51. foreach (var targets in _typeToTargetsMap.Values)
  52. {
  53. if (targets.Contains(target))
  54. {
  55. targets.Remove(target);
  56. break;
  57. }
  58. }
  59. _idToTargetMap.Remove(target.Entity.EntityId);
  60. _waitingMap?.Remove(target);
  61. if (target.Entity.EntityId == _playerEntityId)
  62. {
  63. _playerEntityId = 0;
  64. }
  65. }
  66. public ITarget GetTarget(long entityId)
  67. {
  68. ITarget target = null;
  69. _idToTargetMap?.TryGetValue(entityId, out target);
  70. return target;
  71. }
  72. public ITarget GetPlayer()
  73. {
  74. return GetTarget(_playerEntityId);
  75. }
  76. ITarget ITargetSelector.FindSync(ITarget finder, EEntityType targetType, ETargetFindType findType, Func<ITarget, bool> customFilter)
  77. {
  78. var info = new FinderInfo()
  79. {
  80. uid = finder.Entity.EntityId,
  81. position = finder.Position,
  82. radius = finder.Entity.Attr.Radius
  83. };
  84. return FindTarget(info, targetType, findType, customFilter);
  85. }
  86. ITarget ITargetSelector.FindSync(FinderInfo finder, EEntityType targetType, ETargetFindType findType, Func<ITarget, bool> customFilter)
  87. {
  88. return FindTarget(finder, targetType, findType, customFilter);
  89. }
  90. bool ITargetSelector.FindTargets(FinderInfo finder, EEntityType targetType, ETargetFindType findType, Func<ITarget, bool> customFilter, ref List<ITarget> targets)
  91. {
  92. if (!TryGetTargets(targetType, out var preTargets))
  93. {
  94. Log.Debug($"没有可选目标 Finder:{finder.uid} TargetType:{targetType}");
  95. return false;
  96. }
  97. if (targets == null)
  98. targets = new List<ITarget>();
  99. foreach (var target in preTargets)
  100. {
  101. if (!IsCanSelect(target, findType is ETargetFindType.NearestAlive or ETargetFindType.InRangeAlive))
  102. continue;
  103. if (customFilter.SafeInvoke(target))
  104. {
  105. continue;
  106. }
  107. if (IsInRange(finder, target, out var distance))
  108. {
  109. targets.Add(target);
  110. }
  111. }
  112. return targets.Count > 0;
  113. }
  114. bool ITargetSelector.FindTargets(Rect rect, EEntityType targetType, ETargetFindType findType, Func<ITarget, bool> customFilter, ref List<ITarget> targets)
  115. {
  116. if (!TryGetTargets(targetType, out var preTargets))
  117. {
  118. Log.Debug($"没有可选目标 Rect:{rect} TargetType:{targetType}");
  119. return false;
  120. }
  121. if (targets == null)
  122. targets = new List<ITarget>();
  123. foreach (var target in preTargets)
  124. {
  125. if (!IsCanSelect(target, findType is ETargetFindType.NearestAlive or ETargetFindType.InRangeAlive))
  126. continue;
  127. if (customFilter.SafeInvoke(target))
  128. {
  129. continue;
  130. }
  131. if (rect.Contains(target.Position))
  132. {
  133. targets.Add(target);
  134. }
  135. }
  136. return targets.Count > 0;
  137. }
  138. void ITargetSelector.FindAsync(ITarget finder, EEntityType targetType, ETargetFindType findType, Action<ITarget> onSelected)
  139. {
  140. if (_waitingMap == null)
  141. {
  142. _waitingMap = new Dictionary<ITarget, FindTargetArgs>();
  143. }
  144. _waitingMap[finder] = new FindTargetArgs()
  145. {
  146. targetType = targetType,
  147. findType = findType,
  148. onSelected = onSelected
  149. };
  150. }
  151. public Vector3 RandomPosition(EEntityType targetType, bool isCloseToTarget, Func<ITarget, bool> customFilter = null)
  152. {
  153. if (isCloseToTarget)
  154. {
  155. var tmpTarget = RandomTarget(targetType, customFilter);
  156. if (tmpTarget != null)
  157. { // 目标坐标(-1, 1)内随机
  158. var result = tmpTarget.Position;
  159. var radius = tmpTarget.Entity.Attr.Radius;
  160. result.x += UnityEngine.Random.Range(-radius, radius);
  161. result.y += UnityEngine.Random.Range(-radius, radius);
  162. //Log.Debug($"RandomPosition Target:{tmpTarget.Position} Result:{result}");
  163. return result;
  164. }
  165. }
  166. var tarPosition = GetPlayer().Position; //玩家的坐标和本节战斗的位置重叠
  167. if (targetType == EEntityType.Monster)
  168. {// 目标是怪物,怪物的战斗位置在玩家坐标上移8
  169. // TODO 转成配置
  170. tarPosition.y += 8;
  171. }
  172. tarPosition.x += UnityEngine.Random.Range(-4, 5);
  173. tarPosition.y += UnityEngine.Random.Range(-2, 2);
  174. return tarPosition;
  175. }
  176. public void OnUpdate(int times)
  177. {
  178. var count = _waitingMap?.Count ?? 0;
  179. if (count == 0)
  180. return;
  181. _tempLst.AddRange(_waitingMap.Keys);
  182. for (var i = 0; i < count; i++)
  183. {
  184. var finder = _tempLst[i];
  185. if (!IsCanSelect(finder, false))
  186. continue;
  187. if (!_waitingMap.TryGetValue(finder, out var args))
  188. {
  189. continue;
  190. }
  191. var info = new FinderInfo()
  192. {
  193. uid = finder.Entity.EntityId,
  194. position = finder.Position,
  195. radius = finder.Entity.Attr.Radius
  196. };
  197. var target = FindTarget(info, args.targetType, args.findType, null);
  198. if (target != null)
  199. { // 找到目标了才回调,并删除数据
  200. args.onSelected.SafeInvoke(target);
  201. _waitingMap.Remove(finder);
  202. }
  203. }
  204. _tempLst.Clear();
  205. }
  206. /// <summary>
  207. /// 查找目标
  208. /// </summary>
  209. /// <param name="finder"></param>
  210. /// <param name="targetType"></param>
  211. /// <param name="findType"></param>
  212. /// <param name="customFilter">过滤器,不能选择时返回true</param>
  213. /// <returns></returns>
  214. private ITarget FindTarget(FinderInfo finder, EEntityType targetType, ETargetFindType findType, Func<ITarget, bool> customFilter)
  215. {
  216. if (!TryGetTargets(targetType, out var targets))
  217. {
  218. Log.Debug($"没有可选目标 Finder:{finder.uid} TargetType:{targetType}");
  219. return null;
  220. }
  221. ITarget result = null;
  222. var distanceMin = float.MaxValue;
  223. foreach (var target in targets)
  224. {
  225. if (!IsCanSelect(target, findType is ETargetFindType.NearestAlive or ETargetFindType.InRangeAlive))
  226. continue;
  227. if (customFilter.SafeInvoke(target))
  228. {
  229. continue;
  230. }
  231. var isInRange = IsInRange(finder, target, out var distance);
  232. if (findType is ETargetFindType.Nearest or ETargetFindType.NearestAlive)
  233. {
  234. if (result == null || distanceMin > distance)
  235. {
  236. result = target;
  237. distanceMin = distance;
  238. }
  239. }
  240. else if (isInRange)
  241. {
  242. result = target;
  243. break;
  244. }
  245. }
  246. return result;
  247. }
  248. private ITarget RandomTarget(EEntityType targetType, Func<ITarget, bool> customFilter)
  249. {
  250. if (!TryGetTargets(targetType, out var targets))
  251. {
  252. Log.Debug($"没有可选目标 TargetType: {targetType}");
  253. return null;
  254. }
  255. var list = ListPool.Acquire<ITarget>();
  256. foreach (var target in targets)
  257. {
  258. var random = UnityEngine.Random.Range(0, 1000) % 2;
  259. if (random == 0)
  260. list.Insert(0, target);
  261. else
  262. list.Add(target);
  263. }
  264. ITarget result = null;
  265. foreach (var target in list)
  266. {
  267. if (!IsCanSelect(target, true))
  268. continue;
  269. if (customFilter.SafeInvoke(target))
  270. {
  271. continue;
  272. }
  273. result = target;
  274. break;
  275. }
  276. ListPool.Recycle(list);
  277. return result;
  278. }
  279. /// <summary>
  280. /// 目标是否可选择
  281. /// </summary>
  282. /// <param name="target"></param>
  283. /// <param name="isAlive">True: 除了死亡状态,其他状态都可选</param>
  284. /// <returns></returns>
  285. private bool IsCanSelect(ITarget target, bool isAlive)
  286. {
  287. if (target.IsDead) return false;
  288. if (target.Entity.IsState(EEntityState.Moving))
  289. return isAlive;
  290. return isAlive || target.IsMoving == false;
  291. //return target.Entity.State switch
  292. //{
  293. // EEntityState.Idle or EEntityState.Battle => isAlive || target.IsMoving == false,
  294. // EEntityState.Dead => false,
  295. // _ => isAlive,
  296. //};
  297. }
  298. private bool TryGetTargets(EEntityType entityType, out HashSet<ITarget> targets)
  299. {
  300. if (_typeToTargetsMap != null && _typeToTargetsMap.TryGetValue(entityType, out targets))
  301. {
  302. return true;
  303. }
  304. targets = null;
  305. return false;
  306. }
  307. private bool IsInRange(FinderInfo finder, ITarget target, out float distance)
  308. {
  309. distance = Vector3.Distance(target.Position, finder.position);
  310. distance -= (finder.radius + target.Entity.Attr.Radius); // 去掉双方的半径
  311. return distance <= 0;
  312. }
  313. public void Clear()
  314. {
  315. _typeToTargetsMap?.Clear();
  316. _idToTargetMap?.Clear();
  317. _waitingMap?.Clear();
  318. _tempLst?.Clear();
  319. _playerEntityId = 0;
  320. }
  321. }
  322. }