Yeji.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class Yeji : TargetAnimal
  6. {
  7. //动画播放器
  8. AnimationPlayer ap;
  9. //寻路代理
  10. NavMeshAgent agent;
  11. //血量
  12. [System.NonSerialized] public int hp = 3;
  13. public TreeAreaRecorder treeAreaRecorder;
  14. //模型的偏移Y(即坐标为Zero时,模型超出地面的高度)
  15. public const float modelOffsetY = 0.33f;
  16. [System.NonSerialized] public float flyPlaneHeight = 5;
  17. public void SetFlyHeight(float value) {
  18. float agentBaseOffset = value - flyPlaneHeight - modelOffsetY;
  19. this.agent.baseOffset = agentBaseOffset / 2;
  20. }
  21. void Awake()
  22. {
  23. ap = GetComponent<AnimationPlayer>();
  24. agent = GetComponent<NavMeshAgent>();
  25. }
  26. void Start()
  27. {
  28. //初始动画
  29. playAniFly();
  30. //初始化自动策略
  31. InitAutoStrategy();
  32. }
  33. void Update()
  34. {
  35. //寻路过程监测
  36. if (HasCloseToDestination()) {
  37. OnReachDestination();
  38. } else {
  39. OnMovingToDestination();
  40. }
  41. UpdateAction();
  42. UpdateActionTime();
  43. UpdateAutoStrategy();
  44. if (Input.GetKeyDown(KeyCode.S)) {
  45. }
  46. }
  47. public void Disappear() {
  48. Destroy(this.gameObject);
  49. }
  50. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  51. {
  52. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  53. arrow.Hit();
  54. if (partName == "Wing") {
  55. hp -= 1;
  56. this.agent.speed /= 2f;
  57. }
  58. else if (partName == "Body") hp -= 2;
  59. else if (partName == "Head") hp -= 3;
  60. if (hp <= 0) {
  61. Die(arrow);
  62. } else {
  63. Hurt();
  64. }
  65. }
  66. void Die(Arrow arrow) {
  67. if (dead) return;
  68. arrow.onDoNextShoot += delegate() {
  69. Disappear();
  70. };
  71. dead = true;
  72. this.agent.enabled = false;
  73. PuaseAutoStrategy();
  74. onDie?.Invoke(this);
  75. AudioMgr.ins.PlayAnimalEffect("bird_die", AudioMgr.GetAudioSource(this.gameObject));
  76. }
  77. void Hurt() {
  78. AudioMgr.ins.PlayAnimalEffect("bird_injured", AudioMgr.GetAudioSource(this.gameObject));
  79. }
  80. //是否已经死亡
  81. bool dead = false;
  82. //是否处于移动状态
  83. bool _moving = false;
  84. bool moving {
  85. get {
  86. return _moving;
  87. }
  88. set {
  89. if (_moving && !value) {
  90. movingTime = 0;
  91. }
  92. if (!_moving && value) stayingTime = 0;
  93. _moving = value;
  94. }
  95. }
  96. //实时记录“单次移动的持续时间”
  97. float movingTime = 0;
  98. //实时停留时间
  99. float stayingTime = 0;
  100. //启动寻路
  101. void SetDestination(Vector3 pos) {
  102. moving = true;
  103. this.agent.destination = pos;
  104. }
  105. //寻路过程
  106. void OnMovingToDestination() {
  107. if (!moving) return;
  108. }
  109. //寻路结束
  110. void OnReachDestination() {
  111. if (!moving) return;
  112. moving = false;
  113. }
  114. //是否已经接近目的地(寻路完成判断)
  115. bool HasCloseToDestination() {
  116. if (!moving) return true;
  117. if (movingTime > 0.1) {
  118. if (this.agent.velocity.magnitude < 0.05f) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. //更新动作
  125. void UpdateAction() {
  126. if (dead) {
  127. if (!isAniDie()) playAniDie();
  128. } else {
  129. if (!isAniFly()) playAniFly();
  130. }
  131. }
  132. //记录动作时间
  133. void UpdateActionTime() {
  134. if (moving) {
  135. movingTime += Time.deltaTime;
  136. } else {
  137. stayingTime += Time.deltaTime;
  138. }
  139. }
  140. float willStayTime;
  141. float willMoveTime;
  142. bool autoMoving = false;
  143. float autoMovingTime;
  144. bool autoStrategy = true;
  145. //自动策略的更新逻辑
  146. void UpdateAutoStrategy() {
  147. if (!autoStrategy) return;
  148. if (stayingTime > willStayTime) {
  149. autoMoving = true;
  150. RandomMove();
  151. RandomWillStayTime();
  152. }
  153. if (autoMoving) {
  154. if (moving) {
  155. autoMovingTime += Time.deltaTime;
  156. } else if (autoMovingTime < willMoveTime) {
  157. RandomMove();
  158. } else if (autoMovingTime >= willMoveTime) {
  159. Stay();
  160. RandomWillMoveTime();
  161. autoMoving = false;
  162. autoMovingTime = 0;
  163. }
  164. }
  165. }
  166. void InitAutoStrategy() {
  167. autoStrategy = true;
  168. this.willStayTime = 1;
  169. RandomWillMoveTime();
  170. }
  171. void PuaseAutoStrategy() {
  172. this.autoStrategy = false;
  173. }
  174. void ResumeAutoStrategy() {
  175. this.autoStrategy = true;
  176. RandomWillStayTime();
  177. RandomWillMoveTime();
  178. this.autoMoving = false;
  179. this.autoMovingTime = 0;
  180. }
  181. void RandomWillStayTime() {
  182. this.willStayTime = 1;
  183. }
  184. void RandomWillMoveTime() {
  185. this.willMoveTime = 20;
  186. }
  187. //逃跑远离猎人
  188. void RunAwayFromHunter()
  189. {
  190. Vector3 backVec = GetPointerHunterToMe();
  191. SetDestination(transform.position + backVec.normalized * 5);
  192. }
  193. //能够在前几棵树徘徊
  194. [System.NonSerialized] public int canFlyTreeCount = 4;
  195. //将会绕树转
  196. bool willMoveRoundTree = false;
  197. Queue<Vector3> roundTreePostions = new Queue<Vector3>();
  198. //随机移动
  199. void RandomMove() {
  200. if (willMoveRoundTree && roundTreePostions.Count > 0) {
  201. Vector3 pos = roundTreePostions.Dequeue();
  202. SetDestination(pos);
  203. return;
  204. } else {
  205. willMoveRoundTree = false;
  206. }
  207. Vector3 myPos = this.transform.position;
  208. TreeAreaRecorder.AreaInfo[] areaInfos = treeAreaRecorder.copyAreaInfos(canFlyTreeCount);
  209. for (int i = 0; i < canFlyTreeCount; i++) {
  210. areaInfos[i].teamCompareValue = areaInfos[i].distanceInHorizontal(myPos);
  211. }
  212. System.Array.Sort(areaInfos, new ComparerAreaInfosByDistance());
  213. //如果离这棵树比较近,有几率使其绕着树转
  214. TreeAreaRecorder.AreaInfo firstAreaInfo = areaInfos[0];
  215. bool isNearTree4th = areaInfos.Length == 4 && firstAreaInfo == treeAreaRecorder.getValidAreaInfo(3);
  216. if (!isNearTree4th && firstAreaInfo.teamCompareValue < firstAreaInfo.radius + 1.5f && Random.value < 0.5) {
  217. willMoveRoundTree = true;
  218. Vector3 treePos = areaInfos[0].transform.position;
  219. treePos.y = myPos.y;
  220. Vector3 pointer = myPos - treePos;
  221. roundTreePostions.Clear();
  222. int direction = Random.value >= 0.5 ? 1 : -1;
  223. for (int i = 1; i <= 6; i++) {
  224. roundTreePostions.Enqueue(treePos + Quaternion.AngleAxis(i * 60 * direction, Vector3.up) * pointer);
  225. }
  226. RandomMove();
  227. return;
  228. }
  229. TreeAreaRecorder.AreaInfo areaInfo = areaInfos[Random.Range(1, canFlyTreeCount)];
  230. Vector3 newPos = YejiHuntGameMode.CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  231. SetDestination(newPos);
  232. }
  233. class ComparerAreaInfosByDistance : IComparer {
  234. public int Compare(object x, object y) {
  235. TreeAreaRecorder.AreaInfo a = (TreeAreaRecorder.AreaInfo)x;
  236. TreeAreaRecorder.AreaInfo b = (TreeAreaRecorder.AreaInfo)y;
  237. if (a.teamCompareValue - b.teamCompareValue > 0) return 1;
  238. if (a.teamCompareValue - b.teamCompareValue < 0) return -1;
  239. else return 0;
  240. }
  241. }
  242. //停留
  243. void Stay() {
  244. if (moving) {
  245. this.agent.destination = this.agent.nextPosition;
  246. OnReachDestination();
  247. }
  248. }
  249. //当前动画索引
  250. int curAnimIndex = 0;
  251. void playAniDie() {
  252. ap.play(curAnimIndex = 1, WrapMode.Once);
  253. }
  254. bool isAniDie() {
  255. return curAnimIndex == 1;
  256. }
  257. void playAniFly() {
  258. ap.play(curAnimIndex = 0, WrapMode.Loop);
  259. }
  260. bool isAniFly() {
  261. return curAnimIndex == 0;
  262. }
  263. //委托
  264. public System.Action<Yeji> onDie;
  265. }