Yeji.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.PlayAnimalEffect("bird_die", AudioMgr.GetAudioSource(this.gameObject));
  81. AudioMgr.ins.PlayCheer(true);
  82. if (currentHeight < 1.5f) {
  83. arrow.arrowCameraComp.arrowCameraTemplate.SendMsg(0, null);
  84. }
  85. }
  86. void Hurt() {
  87. CancelStand();
  88. AudioMgr.ins.PlayAnimalEffect("bird_injured", AudioMgr.GetAudioSource(this.gameObject));
  89. }
  90. #endregion
  91. #region 寻路导航
  92. bool _moving;
  93. bool moving {
  94. get { return _moving; }
  95. set {
  96. movingTime = 0;
  97. _moving = value;
  98. }
  99. }
  100. float movingTime;
  101. //启动寻路
  102. void SetDestination(Vector3 pos) {
  103. moving = true;
  104. this.agent.destination = pos;
  105. }
  106. //寻路过程
  107. void OnMovingToDestination() {
  108. if (!moving) return;
  109. movingTime += Time.deltaTime;
  110. }
  111. //寻路结束
  112. void OnReachDestination() {
  113. if (!moving) return;
  114. moving = false;
  115. }
  116. //是否已经接近目的地(寻路完成判断)
  117. bool HasCloseToDestination() {
  118. if (!moving) return true;
  119. return movingTime > 0.1 && this.agent.velocity.magnitude < 0.05f;
  120. }
  121. //停止寻路
  122. void StopNavigation() {
  123. if (!moving) return;
  124. this.agent.destination = this.agent.nextPosition;
  125. this.agent.velocity = Vector3.zero;
  126. this.transform.position = this.agent.destination;
  127. OnReachDestination();
  128. }
  129. #endregion
  130. #region 自动逻辑
  131. //自动策略的更新逻辑
  132. void UpdateAutoStrategy() {
  133. if (state.dead) return;
  134. UpdateFlyUpDown();
  135. UpdateBehavior();
  136. }
  137. float toFlyHeight = 2f;
  138. float canStandTime = 3f;
  139. //能够在前几棵树徘徊
  140. [System.NonSerialized] public int canFlyTreeCount = 4;
  141. //将会绕树转
  142. bool willMoveRoundTree = false;
  143. Queue<Vector3> roundTreePostions = new Queue<Vector3>();
  144. //随机移动
  145. void UpdateFlyUpDown() {
  146. if (state.up && state.landing) { //起飞阶段
  147. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  148. bool isTakeOff = animatorStateInfo.IsName("FlyFromGround");
  149. if (isTakeOff) {
  150. if (animatorStateInfo.normalizedTime >= 1.0) {
  151. state.landing = false;
  152. state.flying = true;
  153. Invoke("ReleaseLandLock", Random.value * 3f + 1.0f);
  154. }
  155. }
  156. return;
  157. }
  158. if (state.up && state.flying) { //上升阶段
  159. float surplusHeight = toFlyHeight - currentHeight;
  160. float smoothValue = surplusHeight / toFlyHeight;
  161. float nextFlyHeight = currentHeight + Time.deltaTime * (1f + 2.5f * smoothValue);
  162. this.animator.speed = 1f + 2f * smoothValue;
  163. if (nextFlyHeight >= toFlyHeight) { //上升完成
  164. nextFlyHeight = toFlyHeight;
  165. this.animator.speed = 1;
  166. state.up = false;
  167. }
  168. SetFlyHeight(nextFlyHeight);
  169. return;
  170. }
  171. if (state.down && state.flying) { //下降阶段
  172. float nextH = currentHeight - Time.deltaTime * 2f;
  173. if (nextH <= 0) { //下降完成
  174. nextH = 0;
  175. state.landing = true;
  176. state.flying = false;
  177. StopNavigation();
  178. }
  179. SetFlyHeight(nextH);
  180. return;
  181. }
  182. if (state.down && state.landing) { //着落阶段
  183. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  184. bool isLand = animatorStateInfo.IsName("LandOnGround");
  185. if (isLand) {
  186. if (animatorStateInfo.normalizedTime >= 1.0) {
  187. state.down = false;
  188. //进入站立状态
  189. state.standing = true;
  190. canStandTime = 2f + Random.value * 3f;
  191. animator.CrossFade("IdleOnGround2", 0.2f);
  192. }
  193. }
  194. return;
  195. }
  196. }
  197. float willFlyTime;
  198. float flyingTime;
  199. void UpdateBehavior() {
  200. if (state.standing) {
  201. canStandTime -= Time.deltaTime;
  202. if (canStandTime <= 0) {
  203. CancelStand();
  204. }
  205. return;
  206. }
  207. if (state.landing) return;
  208. if (state.flying && !state.down && !state.up) {
  209. flyingTime += Time.deltaTime;
  210. if (flyingTime >= willFlyTime && DoLandLock()) {
  211. flyingTime = 0;
  212. state.down = true;
  213. }
  214. }
  215. if (moving) return;
  216. if (willMoveRoundTree && roundTreePostions.Count > 0) {
  217. Vector3 pos = roundTreePostions.Dequeue();
  218. SetDestination(pos);
  219. return;
  220. } else {
  221. willMoveRoundTree = false;
  222. }
  223. //规划下一次行动路线
  224. Vector3 myPos = this.transform.position;
  225. TreeAreaRecorder.AreaInfo[] areaInfos = treeAreaRecorder.copyAreaInfos(canFlyTreeCount);
  226. for (int i = 0; i < canFlyTreeCount; i++) {
  227. areaInfos[i].teamCompareValue = areaInfos[i].distanceInHorizontal(myPos);
  228. }
  229. System.Array.Sort(areaInfos, new ComparerAreaInfosByDistance());
  230. //如果离这棵树比较近,有几率使其绕着树转
  231. TreeAreaRecorder.AreaInfo firstAreaInfo = areaInfos[0];
  232. bool isNearTree4th = areaInfos.Length == 4 && firstAreaInfo == treeAreaRecorder.getValidAreaInfo(3);
  233. if (!isNearTree4th && firstAreaInfo.teamCompareValue < firstAreaInfo.radius + 1.5f && Random.value < 0.5) {
  234. willMoveRoundTree = true;
  235. Vector3 treePos = areaInfos[0].transform.position;
  236. treePos.y = myPos.y;
  237. Vector3 pointer = myPos - treePos;
  238. roundTreePostions.Clear();
  239. int direction = Random.value >= 0.5 ? 1 : -1;
  240. for (int i = 1; i <= 6; i++) {
  241. roundTreePostions.Enqueue(treePos + Quaternion.AngleAxis(i * 60 * direction, Vector3.up) * pointer);
  242. }
  243. return;
  244. }
  245. TreeAreaRecorder.AreaInfo areaInfo = areaInfos[Random.Range(1, canFlyTreeCount)];
  246. Vector3 newPos = YejiHuntGameMode.CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  247. SetDestination(newPos);
  248. }
  249. class ComparerAreaInfosByDistance : IComparer {
  250. public int Compare(object x, object y) {
  251. TreeAreaRecorder.AreaInfo a = (TreeAreaRecorder.AreaInfo)x;
  252. TreeAreaRecorder.AreaInfo b = (TreeAreaRecorder.AreaInfo)y;
  253. if (a.teamCompareValue - b.teamCompareValue > 0) return 1;
  254. if (a.teamCompareValue - b.teamCompareValue < 0) return -1;
  255. else return 0;
  256. }
  257. }
  258. void CancelStand() {
  259. if (!state.standing) return;
  260. state.standing = false;
  261. state.up = true;
  262. toFlyHeight = RandomOnePreHeight();
  263. animator.CrossFade("FlyFromGround", 0.1f);
  264. RandomWillFlyTime();
  265. }
  266. void RandomWillFlyTime() {
  267. willFlyTime = 4 + Random.value * 3f;
  268. }
  269. static float[] preFlyHeights;
  270. static int[] preFlyHeightCools;
  271. public static void InitPreHeights() {
  272. preFlyHeights = new float[]{2, 2.5f, 3, 3.5f, 4f};
  273. preFlyHeightCools = new int[]{0, 0, 0, 0, 0};
  274. }
  275. public static float RandomOnePreHeight() {
  276. List<int> indexes = new List<int>();
  277. for (int i = 0; i < preFlyHeightCools.Length; i++) {
  278. if (preFlyHeightCools[i] > 0) preFlyHeightCools[i] = preFlyHeightCools[i] - 1;
  279. if (preFlyHeightCools[i] == 0) {
  280. indexes.Add(i);
  281. }
  282. }
  283. int index = indexes[Random.Range(0, indexes.Count)];
  284. preFlyHeightCools[index] = 3;
  285. return preFlyHeights[index];
  286. }
  287. public static bool landLocked = false; //着陆锁,禁止地上同时存在两只鸟,避免穿模
  288. private bool myLandLocked = false;
  289. public bool DoLandLock() {
  290. if (landLocked) return false;
  291. return myLandLocked = landLocked = true;
  292. }
  293. public void ReleaseLandLock() {
  294. if (!myLandLocked) return;
  295. myLandLocked = landLocked = false;
  296. }
  297. #endregion
  298. //状态
  299. [System.Serializable]
  300. public class State {
  301. public bool standing = false;
  302. [SerializeField] private bool _dead;
  303. public bool dead {
  304. get { return _dead; }
  305. set {
  306. _dead = value;
  307. animal.animator.SetBool("dead", value);
  308. }
  309. }
  310. [SerializeField] private bool _down;
  311. public bool down {
  312. get { return _down; }
  313. set {
  314. _down = value;
  315. animal.animator.SetBool("down", value);
  316. }
  317. }
  318. [SerializeField] private bool _landing;
  319. public bool landing {
  320. get { return _landing; }
  321. set {
  322. _landing = value;
  323. animal.animator.SetBool("landing", value);
  324. }
  325. }
  326. [SerializeField] private bool _up;
  327. public bool up {
  328. get { return _up; }
  329. set {
  330. _up = value;
  331. animal.animator.SetBool("up", value);
  332. }
  333. }
  334. [SerializeField] private bool _flying;
  335. public bool flying {
  336. get { return _flying; }
  337. set {
  338. _flying = value;
  339. animal.animator.SetBool("flying", value);
  340. }
  341. }
  342. public Yeji animal;
  343. }
  344. [SerializeField] public State state = new State();
  345. //委托
  346. public System.Action<Yeji> onDie;
  347. }