Yeji.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. [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 / 2;//因为agent的baseoffset会受节点的scale影响
  20. }
  21. void Awake()
  22. {
  23. ap = GetComponent<AnimationPlayer>();
  24. agent = GetComponent<NavMeshAgent>();
  25. }
  26. void Start()
  27. {
  28. //初始化自动策略
  29. InitAutoStrategy();
  30. }
  31. void Update()
  32. {
  33. //寻路过程监测
  34. if (HasCloseToDestination()) {
  35. OnReachDestination();
  36. } else {
  37. OnMovingToDestination();
  38. }
  39. UpdateAction();
  40. UpdateActionTime();
  41. UpdateAutoStrategy();
  42. }
  43. public void Disappear() {
  44. Destroy(this.gameObject);
  45. }
  46. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  47. {
  48. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  49. arrow.Hit();
  50. if (partName == "Wing") {
  51. hp -= 1;
  52. this.agent.speed /= 2f;
  53. }
  54. else if (partName == "Body") hp -= 2;
  55. else if (partName == "Head") hp -= 3;
  56. if (hp <= 0) {
  57. Die(arrow);
  58. } else {
  59. Hurt();
  60. }
  61. }
  62. void Die(Arrow arrow) {
  63. if (dead) return;
  64. arrow.onDoNextShoot += delegate() {
  65. Disappear();
  66. };
  67. dead = true;
  68. this.agent.enabled = false;
  69. PuaseAutoStrategy();
  70. onDie?.Invoke(this);
  71. AudioMgr.ins.PlayAnimalEffect("bird_die", AudioMgr.GetAudioSource(this.gameObject));
  72. AudioMgr.ins.PlayCheer(true);
  73. if (currentHeight < 1.5f) {
  74. arrow.arrowCameraComp.arrowCameraTemplate.SendMsg(0, null);
  75. }
  76. }
  77. void Hurt() {
  78. CancelStand();
  79. AudioMgr.ins.PlayAnimalEffect("bird_injured", AudioMgr.GetAudioSource(this.gameObject));
  80. }
  81. //站立状态
  82. bool standing = false;
  83. //是否已经死亡
  84. [System.NonSerialized] public bool dead = false;
  85. //是否处于移动状态
  86. bool _moving = false;
  87. bool moving {
  88. get {
  89. return _moving;
  90. }
  91. set {
  92. if (_moving != value) {
  93. movingTime = 0;
  94. stayingTime = 0;
  95. }
  96. _moving = value;
  97. }
  98. }
  99. //实时记录“单次移动的持续时间”
  100. float movingTime = 0;
  101. //实时停留时间
  102. float stayingTime = 0;
  103. //启动寻路
  104. void SetDestination(Vector3 pos) {
  105. moving = true;
  106. this.agent.destination = pos;
  107. }
  108. //寻路过程
  109. void OnMovingToDestination() {
  110. if (!moving) return;
  111. }
  112. //寻路结束
  113. void OnReachDestination() {
  114. if (!moving) return;
  115. moving = false;
  116. }
  117. //是否已经接近目的地(寻路完成判断)
  118. bool HasCloseToDestination() {
  119. if (!moving) return true;
  120. if (movingTime > 0.1) {
  121. if (this.agent.velocity.magnitude < 0.05f) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. //更新动作
  128. void UpdateAction() {
  129. if (dead) {
  130. if (!isAniDie()) playAniDie();
  131. } else if (standing) {
  132. if (!isAniStand()) playAniStand();
  133. } else {
  134. if (!isAniFly()) playAniFly();
  135. }
  136. }
  137. //记录动作时间
  138. void UpdateActionTime() {
  139. if (moving) {
  140. movingTime += Time.deltaTime;
  141. } else {
  142. stayingTime += Time.deltaTime;
  143. }
  144. }
  145. bool autoStrategy = true;
  146. //自动策略的更新逻辑
  147. void UpdateAutoStrategy() {
  148. if (!autoStrategy) return;
  149. RandomMove();
  150. }
  151. void InitAutoStrategy() {
  152. autoStrategy = true;
  153. }
  154. void PuaseAutoStrategy() {
  155. autoStrategy = false;
  156. }
  157. //逃跑远离猎人
  158. void RunAwayFromHunter()
  159. {
  160. Vector3 backVec = GetPointerHunterToMe();
  161. SetDestination(transform.position + backVec.normalized * 5);
  162. }
  163. float toFlyHeight = 1.5f;
  164. float canStandTime = 3f;
  165. bool toUp = false;
  166. bool toLand = false;
  167. //能够在前几棵树徘徊
  168. [System.NonSerialized] public int canFlyTreeCount = 4;
  169. //将会绕树转
  170. bool willMoveRoundTree = false;
  171. Queue<Vector3> roundTreePostions = new Queue<Vector3>();
  172. //随机移动
  173. void RandomMove() {
  174. if (dead) return;
  175. if (moving) {
  176. if (toUp) {
  177. float nextFlyHeight = currentHeight + Time.deltaTime * 1f;
  178. if (nextFlyHeight > toFlyHeight) {
  179. nextFlyHeight = toFlyHeight;
  180. toUp = false;
  181. }
  182. SetFlyHeight(nextFlyHeight);
  183. return;
  184. }
  185. if (toLand) {
  186. float nextH = currentHeight - Time.deltaTime * 1.5f;
  187. if (nextH < 0) {
  188. nextH = 0;
  189. toLand = false;
  190. //进入站立状态
  191. standing = true;
  192. canStandTime = 2f + Random.value * 3f;
  193. Stay();
  194. }
  195. SetFlyHeight(nextH);
  196. return;
  197. }
  198. return;
  199. }
  200. if (standing) {
  201. canStandTime -= Time.deltaTime;
  202. if (canStandTime <= 0) {
  203. CancelStand();
  204. }
  205. return;
  206. }
  207. if (willMoveRoundTree && roundTreePostions.Count > 0) {
  208. Vector3 pos = roundTreePostions.Dequeue();
  209. SetDestination(pos);
  210. return;
  211. } else {
  212. willMoveRoundTree = false;
  213. }
  214. bool isOnLand = currentHeight < 0.001f;
  215. if (!isOnLand) {
  216. if (Random.value < 0.5) {
  217. toLand = true;
  218. return;
  219. }
  220. }
  221. //规划下一次行动路线
  222. Vector3 myPos = this.transform.position;
  223. TreeAreaRecorder.AreaInfo[] areaInfos = treeAreaRecorder.copyAreaInfos(canFlyTreeCount);
  224. for (int i = 0; i < canFlyTreeCount; i++) {
  225. areaInfos[i].teamCompareValue = areaInfos[i].distanceInHorizontal(myPos);
  226. }
  227. System.Array.Sort(areaInfos, new ComparerAreaInfosByDistance());
  228. //如果离这棵树比较近,有几率使其绕着树转
  229. TreeAreaRecorder.AreaInfo firstAreaInfo = areaInfos[0];
  230. bool isNearTree4th = areaInfos.Length == 4 && firstAreaInfo == treeAreaRecorder.getValidAreaInfo(3);
  231. if (!isNearTree4th && firstAreaInfo.teamCompareValue < firstAreaInfo.radius + 1.5f && Random.value < 0.5) {
  232. willMoveRoundTree = true;
  233. Vector3 treePos = areaInfos[0].transform.position;
  234. treePos.y = myPos.y;
  235. Vector3 pointer = myPos - treePos;
  236. roundTreePostions.Clear();
  237. int direction = Random.value >= 0.5 ? 1 : -1;
  238. for (int i = 1; i <= 6; i++) {
  239. roundTreePostions.Enqueue(treePos + Quaternion.AngleAxis(i * 60 * direction, Vector3.up) * pointer);
  240. }
  241. RandomMove();
  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. //停留
  258. void Stay() {
  259. if (moving) {
  260. this.agent.destination = this.agent.nextPosition;
  261. OnReachDestination();
  262. }
  263. }
  264. //取消站在地上
  265. void CancelStand() {
  266. if (standing) {
  267. standing = false;
  268. toUp = true;
  269. toFlyHeight = 1.5f + Random.value * 2.5f;
  270. }
  271. }
  272. //当前动画索引
  273. int curAnimIndex = -1;
  274. void playAniDie() {
  275. ap.play(curAnimIndex = 1, WrapMode.Once);
  276. }
  277. bool isAniDie() {
  278. return curAnimIndex == 1;
  279. }
  280. void playAniFly() {
  281. ap.play(curAnimIndex = 0, WrapMode.Loop);
  282. }
  283. bool isAniFly() {
  284. return curAnimIndex == 0;
  285. }
  286. void playAniStand() {
  287. ap.play(curAnimIndex = 2, WrapMode.Loop);
  288. }
  289. bool isAniStand() {
  290. return curAnimIndex == 2;
  291. }
  292. //委托
  293. public System.Action<Yeji> onDie;
  294. }