Yeji.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class Yeji : TargetAnimal
  6. {
  7. //动画播放器
  8. Animator animator;
  9. //寻路代理
  10. NavMeshAgent agent;
  11. //血量
  12. [System.NonSerialized] public int hp = 3;
  13. [System.NonSerialized] public TreeAreaRecorder treeAreaRecorder;
  14. [System.NonSerialized] public float flyPlaneHeight = 5;
  15. float currentHeight = 0;
  16. public void SetFlyHeight(float value) {
  17. currentHeight = value;
  18. float agentBaseOffset = value - flyPlaneHeight;
  19. this.agent.baseOffset = agentBaseOffset / 1.2f;//因为agent的baseoffset会受节点的scale影响
  20. state.flying = currentHeight > 0;
  21. }
  22. void Awake()
  23. {
  24. state.animal = this;
  25. animator = GetComponent<Animator>();
  26. agent = GetComponent<NavMeshAgent>();
  27. this.agent.avoidancePriority = avoidancePriority;
  28. RandomWillFlyTime();
  29. }
  30. static int _avoidancePriority = 0;
  31. static int avoidancePriority {
  32. get {
  33. if (_avoidancePriority < 50) {
  34. _avoidancePriority++;
  35. } else {
  36. _avoidancePriority = 1;
  37. }
  38. return _avoidancePriority;
  39. }
  40. }
  41. void OnDestroy() {
  42. ReleaseLandLock();
  43. }
  44. void Update()
  45. {
  46. //寻路过程监测
  47. if (HasCloseToDestination()) {
  48. OnReachDestination();
  49. } else {
  50. OnMovingToDestination();
  51. }
  52. UpdateAutoStrategy();
  53. }
  54. #region 被击触发
  55. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  56. {
  57. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  58. arrow.Hit();
  59. if (partName == "Wing") {
  60. hp -= 1;
  61. this.agent.speed /= 2f;
  62. }
  63. else if (partName == "Body") hp -= 2;
  64. else if (partName == "Head") hp -= 3;
  65. if (hp <= 0) {
  66. Die(arrow);
  67. } else {
  68. Hurt();
  69. }
  70. }
  71. void Die(Arrow arrow) {
  72. if (state.dead) return;
  73. arrow.onDoNextShoot += delegate() {
  74. Destroy(this.gameObject);
  75. };
  76. this.animator.speed = 1;
  77. state.dead = true;
  78. this.agent.enabled = false;
  79. onDie?.Invoke(this);
  80. AudioMgr.ins.PlayCheer(true);
  81. if (currentHeight < 1.5f) {
  82. arrow.arrowCameraComp.arrowCameraTemplate.SendMsg(0, null);
  83. }
  84. }
  85. void Hurt() {
  86. CancelStand();
  87. AudioMgr.ins.PlayCheer(true);
  88. }
  89. #endregion
  90. #region 寻路导航
  91. bool _moving;
  92. bool moving {
  93. get { return _moving; }
  94. set {
  95. movingTime = 0;
  96. _moving = value;
  97. }
  98. }
  99. float movingTime;
  100. //启动寻路
  101. void SetDestination(Vector3 pos) {
  102. moving = true;
  103. this.agent.destination = pos;
  104. }
  105. //寻路过程
  106. void OnMovingToDestination() {
  107. if (!moving) return;
  108. movingTime += Time.deltaTime;
  109. }
  110. //寻路结束
  111. void OnReachDestination() {
  112. if (!moving) return;
  113. moving = false;
  114. }
  115. //是否已经接近目的地(寻路完成判断)
  116. bool HasCloseToDestination() {
  117. if (!moving) return true;
  118. return movingTime > 0.1 && this.agent.velocity.magnitude < 0.05f;
  119. }
  120. //停止寻路
  121. void StopNavigation() {
  122. if (!moving) return;
  123. this.agent.destination = this.agent.nextPosition;
  124. this.agent.velocity = Vector3.zero;
  125. this.transform.position = this.agent.destination;
  126. OnReachDestination();
  127. }
  128. #endregion
  129. #region 自动逻辑
  130. //自动策略的更新逻辑
  131. void UpdateAutoStrategy() {
  132. if (state.dead) return;
  133. UpdateFlyUpDown();
  134. UpdateBehavior();
  135. }
  136. float toFlyHeight = 2f;
  137. float canStandTime = 3f;
  138. //能够在前几棵树徘徊
  139. [System.NonSerialized] public int canFlyTreeCount = 4;
  140. //将会绕树转
  141. bool willMoveRoundTree = false;
  142. Queue<Vector3> roundTreePostions = new Queue<Vector3>();
  143. //随机移动
  144. void UpdateFlyUpDown() {
  145. if (state.up && state.landing) { //起飞阶段
  146. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  147. bool isTakeOff = animatorStateInfo.IsName("FlyFromGround");
  148. if (isTakeOff) {
  149. if (animatorStateInfo.normalizedTime >= 1.0) {
  150. state.landing = false;
  151. state.flying = true;
  152. Invoke("ReleaseLandLock", Random.value * 3f + 1.0f);
  153. }
  154. }
  155. return;
  156. }
  157. if (state.up && state.flying) { //上升阶段
  158. float surplusHeight = toFlyHeight - currentHeight;
  159. float smoothValue = surplusHeight / toFlyHeight;
  160. float nextFlyHeight = currentHeight + Time.deltaTime * (1f + 2.5f * smoothValue);
  161. this.animator.speed = 1f + 2f * smoothValue;
  162. if (nextFlyHeight >= toFlyHeight) { //上升完成
  163. nextFlyHeight = toFlyHeight;
  164. this.animator.speed = 1;
  165. state.up = false;
  166. }
  167. SetFlyHeight(nextFlyHeight);
  168. return;
  169. }
  170. if (state.down && state.flying) { //下降阶段
  171. float nextH = currentHeight - Time.deltaTime * 2f;
  172. if (nextH <= 0) { //下降完成
  173. nextH = 0;
  174. state.landing = true;
  175. state.flying = false;
  176. StopNavigation();
  177. }
  178. SetFlyHeight(nextH);
  179. return;
  180. }
  181. if (state.down && state.landing) { //着落阶段
  182. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  183. bool isLand = animatorStateInfo.IsName("LandOnGround");
  184. if (isLand) {
  185. if (animatorStateInfo.normalizedTime >= 1.0) {
  186. state.down = false;
  187. //进入站立状态
  188. state.standing = true;
  189. canStandTime = 8f + Random.value * 2f;
  190. animator.CrossFade("IdleOnGround2", 0.2f);
  191. }
  192. }
  193. return;
  194. }
  195. }
  196. float willFlyTime;
  197. float flyingTime;
  198. void UpdateBehavior() {
  199. if (state.standing) {
  200. canStandTime -= Time.deltaTime;
  201. if (canStandTime <= 0) {
  202. CancelStand();
  203. }
  204. return;
  205. }
  206. if (state.landing) return;
  207. if (state.flying && !state.down && !state.up) {
  208. flyingTime += Time.deltaTime;
  209. if (flyingTime >= willFlyTime && DoLandLock()) {
  210. flyingTime = 0;
  211. state.down = true;
  212. }
  213. }
  214. if (moving) return;
  215. if (willMoveRoundTree && roundTreePostions.Count > 0) {
  216. Vector3 pos = roundTreePostions.Dequeue();
  217. SetDestination(pos);
  218. return;
  219. } else {
  220. willMoveRoundTree = false;
  221. }
  222. //规划下一次行动路线
  223. Vector3 myPos = this.transform.position;
  224. TreeAreaRecorder.AreaInfo[] areaInfos = treeAreaRecorder.copyAreaInfos(canFlyTreeCount);
  225. for (int i = 0; i < canFlyTreeCount; i++) {
  226. areaInfos[i].teamCompareValue = areaInfos[i].distanceInHorizontal(myPos);
  227. }
  228. System.Array.Sort(areaInfos, new ComparerAreaInfosByDistance());
  229. //如果离这棵树比较近,有几率使其绕着树转
  230. TreeAreaRecorder.AreaInfo firstAreaInfo = areaInfos[0];
  231. bool isNearTree4th = areaInfos.Length == 4 && firstAreaInfo == treeAreaRecorder.getValidAreaInfo(3);
  232. if (!isNearTree4th && firstAreaInfo.teamCompareValue < firstAreaInfo.radius + 1.5f && Random.value < 0.5) {
  233. willMoveRoundTree = true;
  234. Vector3 treePos = areaInfos[0].transform.position;
  235. treePos.y = myPos.y;
  236. Vector3 pointer = myPos - treePos;
  237. roundTreePostions.Clear();
  238. int direction = Random.value >= 0.5 ? 1 : -1;
  239. for (int i = 1; i <= 6; i++) {
  240. roundTreePostions.Enqueue(treePos + Quaternion.AngleAxis(i * 60 * direction, Vector3.up) * pointer);
  241. }
  242. return;
  243. }
  244. TreeAreaRecorder.AreaInfo areaInfo = areaInfos[Random.Range(1, canFlyTreeCount)];
  245. Vector3 newPos = YejiHuntGameMode.CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  246. SetDestination(newPos);
  247. }
  248. class ComparerAreaInfosByDistance : IComparer {
  249. public int Compare(object x, object y) {
  250. TreeAreaRecorder.AreaInfo a = (TreeAreaRecorder.AreaInfo)x;
  251. TreeAreaRecorder.AreaInfo b = (TreeAreaRecorder.AreaInfo)y;
  252. if (a.teamCompareValue - b.teamCompareValue > 0) return 1;
  253. if (a.teamCompareValue - b.teamCompareValue < 0) return -1;
  254. else return 0;
  255. }
  256. }
  257. void CancelStand() {
  258. if (!state.standing) return;
  259. state.standing = false;
  260. state.up = true;
  261. toFlyHeight = RandomOnePreHeight();
  262. animator.CrossFade("FlyFromGround", 0.1f);
  263. RandomWillFlyTime();
  264. }
  265. void RandomWillFlyTime() {
  266. willFlyTime = 4 + Random.value * 3f;
  267. }
  268. static float[] preFlyHeights;
  269. static int[] preFlyHeightCools;
  270. public static void InitPreHeights() {
  271. preFlyHeights = new float[]{2, 2.5f, 3, 3.5f, 4f};
  272. preFlyHeightCools = new int[]{0, 0, 0, 0, 0};
  273. }
  274. public static float RandomOnePreHeight() {
  275. List<int> indexes = new List<int>();
  276. for (int i = 0; i < preFlyHeightCools.Length; i++) {
  277. if (preFlyHeightCools[i] > 0) preFlyHeightCools[i] = preFlyHeightCools[i] - 1;
  278. if (preFlyHeightCools[i] == 0) {
  279. indexes.Add(i);
  280. }
  281. }
  282. int index = indexes[Random.Range(0, indexes.Count)];
  283. preFlyHeightCools[index] = 3;
  284. return preFlyHeights[index];
  285. }
  286. public static bool landLocked = false; //着陆锁,禁止地上同时存在两只鸟,避免穿模
  287. private bool myLandLocked = false;
  288. public bool DoLandLock() {
  289. if (landLocked) return false;
  290. return myLandLocked = landLocked = true;
  291. }
  292. public void ReleaseLandLock() {
  293. if (!myLandLocked) return;
  294. myLandLocked = landLocked = false;
  295. }
  296. #endregion
  297. //状态
  298. [System.Serializable]
  299. public class State {
  300. public bool standing = false;
  301. [SerializeField] private bool _dead;
  302. public bool dead {
  303. get { return _dead; }
  304. set {
  305. _dead = value;
  306. animal.animator.SetBool("dead", value);
  307. }
  308. }
  309. [SerializeField] private bool _down;
  310. public bool down {
  311. get { return _down; }
  312. set {
  313. _down = value;
  314. animal.animator.SetBool("down", value);
  315. }
  316. }
  317. [SerializeField] private bool _landing;
  318. public bool landing {
  319. get { return _landing; }
  320. set {
  321. _landing = value;
  322. animal.animator.SetBool("landing", value);
  323. }
  324. }
  325. [SerializeField] private bool _up;
  326. public bool up {
  327. get { return _up; }
  328. set {
  329. _up = value;
  330. animal.animator.SetBool("up", value);
  331. }
  332. }
  333. [SerializeField] private bool _flying;
  334. public bool flying {
  335. get { return _flying; }
  336. set {
  337. _flying = value;
  338. animal.animator.SetBool("flying", value);
  339. }
  340. }
  341. public Yeji animal;
  342. }
  343. [SerializeField] public State state = new State();
  344. //委托
  345. public System.Action<Yeji> onDie;
  346. }