AIUtility.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. using UnityEngine;
  11. using UnityGameFramework.Runtime;
  12. namespace MetaClient
  13. {
  14. /// <summary>
  15. /// AI 工具类。
  16. /// </summary>
  17. public static class AIUtility
  18. {
  19. private static Dictionary<CampPair, RelationType> s_CampPairToRelation = new Dictionary<CampPair, RelationType>();
  20. private static Dictionary<KeyValuePair<CampType, RelationType>, CampType[]> s_CampAndRelationToCamps = new Dictionary<KeyValuePair<CampType, RelationType>, CampType[]>();
  21. static AIUtility()
  22. {
  23. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Player), RelationType.Friendly);
  24. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Enemy), RelationType.Hostile);
  25. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Neutral), RelationType.Neutral);
  26. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Player2), RelationType.Hostile);
  27. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Enemy2), RelationType.Hostile);
  28. s_CampPairToRelation.Add(new CampPair(CampType.Player, CampType.Neutral2), RelationType.Neutral);
  29. s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Enemy), RelationType.Friendly);
  30. s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Neutral), RelationType.Neutral);
  31. s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Player2), RelationType.Hostile);
  32. s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Enemy2), RelationType.Hostile);
  33. s_CampPairToRelation.Add(new CampPair(CampType.Enemy, CampType.Neutral2), RelationType.Neutral);
  34. s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Neutral), RelationType.Neutral);
  35. s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Player2), RelationType.Neutral);
  36. s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Enemy2), RelationType.Neutral);
  37. s_CampPairToRelation.Add(new CampPair(CampType.Neutral, CampType.Neutral2), RelationType.Hostile);
  38. s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Player2), RelationType.Friendly);
  39. s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Enemy2), RelationType.Hostile);
  40. s_CampPairToRelation.Add(new CampPair(CampType.Player2, CampType.Neutral2), RelationType.Neutral);
  41. s_CampPairToRelation.Add(new CampPair(CampType.Enemy2, CampType.Enemy2), RelationType.Friendly);
  42. s_CampPairToRelation.Add(new CampPair(CampType.Enemy2, CampType.Neutral2), RelationType.Neutral);
  43. s_CampPairToRelation.Add(new CampPair(CampType.Neutral2, CampType.Neutral2), RelationType.Neutral);
  44. }
  45. /// <summary>
  46. /// 获取两个阵营之间的关系。
  47. /// </summary>
  48. /// <param name="first">阵营一。</param>
  49. /// <param name="second">阵营二。</param>
  50. /// <returns>阵营间关系。</returns>
  51. public static RelationType GetRelation(CampType first, CampType second)
  52. {
  53. if (first > second)
  54. {
  55. CampType temp = first;
  56. first = second;
  57. second = temp;
  58. }
  59. RelationType relationType;
  60. if (s_CampPairToRelation.TryGetValue(new CampPair(first, second), out relationType))
  61. {
  62. return relationType;
  63. }
  64. Log.Warning("Unknown relation between '{0}' and '{1}'.", first.ToString(), second.ToString());
  65. return RelationType.Unknown;
  66. }
  67. /// <summary>
  68. /// 获取和指定具有特定关系的所有阵营。
  69. /// </summary>
  70. /// <param name="camp">指定阵营。</param>
  71. /// <param name="relation">关系。</param>
  72. /// <returns>满足条件的阵营数组。</returns>
  73. public static CampType[] GetCamps(CampType camp, RelationType relation)
  74. {
  75. KeyValuePair<CampType, RelationType> key = new KeyValuePair<CampType, RelationType>(camp, relation);
  76. CampType[] result = null;
  77. if (s_CampAndRelationToCamps.TryGetValue(key, out result))
  78. {
  79. return result;
  80. }
  81. // TODO: GC Alloc
  82. List<CampType> camps = new List<CampType>();
  83. Array campTypes = Enum.GetValues(typeof(CampType));
  84. for (int i = 0; i < campTypes.Length; i++)
  85. {
  86. CampType campType = (CampType)campTypes.GetValue(i);
  87. if (GetRelation(camp, campType) == relation)
  88. {
  89. camps.Add(campType);
  90. }
  91. }
  92. // TODO: GC Alloc
  93. result = camps.ToArray();
  94. s_CampAndRelationToCamps[key] = result;
  95. return result;
  96. }
  97. /// <summary>
  98. /// 获取实体间的距离。
  99. /// </summary>
  100. /// <returns>实体间的距离。</returns>
  101. public static float GetDistance(Entity fromEntity, Entity toEntity)
  102. {
  103. Transform fromTransform = fromEntity.CachedTransform;
  104. Transform toTransform = toEntity.CachedTransform;
  105. return (toTransform.position - fromTransform.position).magnitude;
  106. }
  107. public static void PerformCollision(TargetableObject entity, Entity other)
  108. {
  109. if (entity == null || other == null)
  110. {
  111. return;
  112. }
  113. TargetableObject target = other as TargetableObject;
  114. if (target != null)
  115. {
  116. ImpactData entityImpactData = entity.GetImpactData();
  117. ImpactData targetImpactData = target.GetImpactData();
  118. if (GetRelation(entityImpactData.Camp, targetImpactData.Camp) == RelationType.Friendly)
  119. {
  120. return;
  121. }
  122. int entityDamageHP = CalcDamageHP(targetImpactData.Attack, entityImpactData.Defense);
  123. int targetDamageHP = CalcDamageHP(entityImpactData.Attack, targetImpactData.Defense);
  124. int delta = Mathf.Min(entityImpactData.HP - entityDamageHP, targetImpactData.HP - targetDamageHP);
  125. if (delta > 0)
  126. {
  127. entityDamageHP += delta;
  128. targetDamageHP += delta;
  129. }
  130. entity.ApplyDamage(target, entityDamageHP);
  131. target.ApplyDamage(entity, targetDamageHP);
  132. return;
  133. }
  134. Bullet bullet = other as Bullet;
  135. if (bullet != null)
  136. {
  137. ImpactData entityImpactData = entity.GetImpactData();
  138. ImpactData bulletImpactData = bullet.GetImpactData();
  139. if (GetRelation(entityImpactData.Camp, bulletImpactData.Camp) == RelationType.Friendly)
  140. {
  141. return;
  142. }
  143. int entityDamageHP = CalcDamageHP(bulletImpactData.Attack, entityImpactData.Defense);
  144. entity.ApplyDamage(bullet, entityDamageHP);
  145. GameEntry.Entity.HideEntity(bullet);
  146. return;
  147. }
  148. }
  149. private static int CalcDamageHP(int attack, int defense)
  150. {
  151. if (attack <= 0)
  152. {
  153. return 0;
  154. }
  155. if (defense < 0)
  156. {
  157. defense = 0;
  158. }
  159. return attack * attack / (attack + defense);
  160. }
  161. [StructLayout(LayoutKind.Auto)]
  162. private struct CampPair
  163. {
  164. private readonly CampType m_First;
  165. private readonly CampType m_Second;
  166. public CampPair(CampType first, CampType second)
  167. {
  168. m_First = first;
  169. m_Second = second;
  170. }
  171. public CampType First
  172. {
  173. get
  174. {
  175. return m_First;
  176. }
  177. }
  178. public CampType Second
  179. {
  180. get
  181. {
  182. return m_Second;
  183. }
  184. }
  185. }
  186. }
  187. }