Wolf.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using DG.Tweening;
  6. public class Wolf : TargetAnimal
  7. {
  8. //动画播放器
  9. AnimationPlayer ap;
  10. //寻路代理
  11. NavMeshAgent agent;
  12. void Awake()
  13. {
  14. ap = GetComponent<AnimationPlayer>();
  15. agent = GetComponent<NavMeshAgent>();
  16. }
  17. void Start()
  18. {
  19. initAniListener();
  20. InitAutoStrategy();
  21. this.agent.avoidancePriority = avoidancePriority;
  22. }
  23. static int _avoidancePriority = 0;
  24. static int avoidancePriority {
  25. get {
  26. if (_avoidancePriority < 50) {
  27. _avoidancePriority++;
  28. } else {
  29. _avoidancePriority = 1;
  30. }
  31. return _avoidancePriority;
  32. }
  33. }
  34. void Update()
  35. {
  36. if (HasCloseToDestination()) {
  37. OnReachDestination();
  38. }
  39. UpdateAutoStrategy();
  40. UpdateAction();
  41. }
  42. //可选皮肤材质
  43. [SerializeField] Material[] materials;
  44. public void ChangeColorByType(int type) {
  45. GetComponentInChildren<SkinnedMeshRenderer>().material = materials[type - 1];
  46. }
  47. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  48. {
  49. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  50. arrow.Hit();
  51. if (partName == "Leg" || partName == "Tail") {
  52. state.hp -= 2;
  53. }
  54. else if (partName == "Body") {
  55. state.hp -= 3;
  56. }
  57. else if (partName == "Head") {
  58. state.hp -= 100;
  59. }
  60. if (state.hp > 0) {
  61. Hurt();
  62. } else {
  63. Die(arrow);
  64. }
  65. }
  66. void Die(Arrow arrow) {
  67. if (state.dead) return;
  68. arrow.onDoNextShoot += delegate() { Destroy(this.gameObject); };
  69. state.ResetActionState();
  70. state.dead = true;
  71. this.agent.enabled = false;
  72. onDie?.Invoke(this);
  73. AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.gameObject));
  74. AudioMgr.ins.PlayCheer(true);
  75. }
  76. void Hurt() {
  77. state.lockingTarget = true;
  78. state.ResetActionState();
  79. AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.gameObject));
  80. }
  81. //启动寻路
  82. void SetDestination(Vector3 pos) {
  83. state.ResetActionState();
  84. state.moving = true;
  85. this.agent.destination = pos;
  86. }
  87. //寻路结束
  88. void OnReachDestination() {
  89. if (!state.moving) return;
  90. state.ResetActionState();
  91. }
  92. //是否已经接近目的地(寻路完成判断)
  93. bool HasCloseToDestination() {
  94. if (!state.moving) return true;
  95. if (Vector3.Distance(this.agent.nextPosition, this.agent.destination) < 0.25f) {
  96. return true;
  97. }
  98. if (state.movingTime > 0.1f && this.agent.velocity.magnitude < 0.05f) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. float willStayTime;
  104. float willMoveTime;
  105. bool autoMoving = false;
  106. float autoMovingTime;
  107. bool willAttack = false;
  108. bool hasRunAround = false;
  109. int needRunAroundCount = 0;
  110. bool hasRunToHunter = false;
  111. //帧更新逻辑-自动策略
  112. void UpdateAutoStrategy() {
  113. if (state.dead) return;
  114. if (state.lockingTarget) {
  115. if (state.attacking) {
  116. this.transform.LookAt(this.hunterPosition);
  117. }
  118. if (!hasRunToHunter) {
  119. hasRunToHunter = true;
  120. RunToAttackHunter();
  121. willAttack = true;
  122. return;
  123. }
  124. if (!state.moving && !state.attacking) {
  125. if (willAttack) {
  126. willAttack = false;
  127. Attack();
  128. needRunAroundCount = 3;
  129. return;
  130. }
  131. if (needRunAroundCount > 0) {
  132. needRunAroundCount--;
  133. RunAroundHunter();
  134. hasRunAround = true;
  135. return;
  136. }
  137. if (hasRunAround) {
  138. hasRunAround = false;
  139. RunToAttackHunter();
  140. }
  141. willAttack = true;
  142. return;
  143. }
  144. return;
  145. }
  146. if (state.stayingTime > willStayTime) {
  147. //退出stay
  148. RandomWillStayTime();
  149. //进入move
  150. autoMoving = true;
  151. MoveSlowlyInZPath();
  152. }
  153. if (autoMoving) {
  154. if (state.moving) {
  155. autoMovingTime += Time.deltaTime;
  156. } else if (autoMovingTime < willMoveTime) {
  157. MoveSlowlyInZPath();
  158. }
  159. if (autoMovingTime >= willMoveTime) {
  160. //退出move
  161. RandomWillMoveTime();
  162. autoMoving = false;
  163. autoMovingTime = 0;
  164. //进入stay
  165. Stay();
  166. }
  167. }
  168. }
  169. void InitAutoStrategy() {
  170. this.willStayTime = 1f + Random.value * 2;
  171. RandomWillMoveTime();
  172. }
  173. void RandomWillStayTime() {
  174. this.willStayTime = Random.value * 2 + 3;
  175. }
  176. void RandomWillMoveTime() {
  177. this.willMoveTime = Random.value * 3 + 5;
  178. }
  179. //起跳点
  180. Vector3 jumpPoint;
  181. //落地点
  182. Vector3 landPoint;
  183. //攻击
  184. void Attack() {
  185. state.ResetActionState();
  186. state.attacking = true;
  187. curAnimIndex = -1; /*注意:这样可避免狼两次使用同一攻击动作而第二次无法播放的情况 */
  188. int hurtValue = 2;
  189. if (canAttackID == "A") {
  190. state.attackA = true;
  191. #region //起点和终点计算
  192. jumpPoint = this.transform.position;
  193. Vector3 hunterPos = hunterPosition;
  194. hunterPos.y = jumpPoint.y;
  195. Vector3 deltaPointer = jumpPoint - hunterPos;
  196. landPoint = hunterPos + deltaPointer.normalized * 2f;
  197. #endregion
  198. Vector3 displace = landPoint - jumpPoint;
  199. float baseoffset = this.agent.baseOffset;
  200. Sequence seq = DOTween.Sequence();
  201. seq.Append(DOTween.To(() => 0f, value => {
  202. this.transform.position = jumpPoint + displace * value;
  203. if (value < 0.5) {
  204. this.agent.baseOffset = baseoffset + value;
  205. } else {
  206. this.agent.baseOffset = baseoffset + (1 - value);
  207. }
  208. }, 1f, 0.6f));
  209. seq.AppendCallback(delegate() {
  210. this.transform.position = landPoint;
  211. if (!state.dead) onAttack?.Invoke(this, hurtValue);
  212. });
  213. seq.Append(DOTween.To(() => 0f, value => {
  214. this.transform.position = landPoint;
  215. }, 1f, 0.634f));
  216. seq.AppendCallback(delegate() {
  217. if (!state.dead) stopAniAttackA();
  218. this.transform.position = landPoint;
  219. });
  220. seq.Append(DOTween.To(() => 0f, value => {
  221. this.transform.position = landPoint;
  222. }, 1f, 0.1f)); //通过dotween是它不被动画的位移影响;
  223. } else if (canAttackID == "B") {
  224. state.attackB = true;
  225. hurtValue = 3;
  226. onAttack?.Invoke(this, hurtValue);
  227. }
  228. }
  229. //跑到能攻击玩家的坐标点
  230. string canAttackID = null;
  231. void RunToAttackHunter() {
  232. float needDistance;
  233. Vector3 displace = animalsBaseT.forward;
  234. // if (Random.value < 0.33f) {
  235. // needDistance = 2;
  236. // canAttackID = "B";
  237. // } else {
  238. displace = Quaternion.AngleAxis(Random.Range(-30f, 30f), Vector3.up) * displace;
  239. needDistance = 8;
  240. canAttackID = "A";
  241. // }
  242. Vector3 newPos = animalsBaseT.position + displace * needDistance;
  243. SetDestination(newPos);
  244. state.moveQuickly = true;
  245. agent.speed = 5f;
  246. }
  247. //在敌人身边奔跑徘徊
  248. void RunAroundHunter() {
  249. float baseDistance = 7;
  250. float baseDistanceMoveRange = 7;
  251. Vector3 standardPointer = animalsBaseT.forward;
  252. Vector3 pointerWithLen = standardPointer.normalized * (baseDistance + Random.value * baseDistanceMoveRange);
  253. pointerWithLen = Quaternion.AngleAxis(-60f + Random.value * 120f, Vector3.up) * pointerWithLen;
  254. Vector3 newPos = animalsBaseT.position + pointerWithLen;
  255. SetDestination(newPos);
  256. state.moveQuickly = true;
  257. agent.speed = 5f;
  258. }
  259. //Z字型路径缓慢移动
  260. Queue<Vector3> zPathPoints = new Queue<Vector3>();
  261. bool canCreateZPath = true;
  262. void MoveSlowlyInZPath() {
  263. if (zPathPoints.Count > 0) {
  264. SetDestination(zPathPoints.Dequeue());
  265. state.moveSlowly = true;
  266. agent.speed = 0.8f;
  267. return;
  268. }
  269. if (!canCreateZPath) {
  270. state.lockingTarget = true;
  271. return;
  272. }
  273. //构建Z字型路径
  274. zPathPoints.Clear();
  275. Vector3 standardPos = animalsBaseT.forward;
  276. Vector3 hunterPos = hunterPosition;
  277. Vector3 pointerToMe = GetPointerHunterToMe().normalized;
  278. Vector3 pointer1 = Quaternion.AngleAxis(120, Vector3.up) * pointerToMe;
  279. Vector3 pointer2 = Quaternion.AngleAxis(-120, Vector3.up) * pointerToMe;
  280. Vector3 myPos = this.transform.position;
  281. int[] pointerSeq = {1, 2, 1};
  282. bool isReverse = Random.value < 0.5;
  283. foreach (int seqID in pointerSeq)
  284. {
  285. Vector3 lastMyPos = myPos;
  286. if (seqID == 1) myPos = myPos + (isReverse ? pointer2 : pointer1) * 5;
  287. if (seqID == 2) myPos = myPos + (isReverse ? pointer1 : pointer2) * 10;
  288. hunterPos.y = myPos.y;
  289. Vector3 hunterToMe = myPos - hunterPos;
  290. float angle = Vector3.Angle(hunterToMe, standardPos);
  291. float minDistance = 8;
  292. //如果下一个构建的坐标在猎手身后或者距离猎手低于指定距离
  293. if (angle > 90 || Vector3.Distance(hunterPos, myPos) < minDistance) {
  294. Vector3 hunterToMe2 = lastMyPos - hunterPos;
  295. if (hunterToMe2.magnitude > minDistance + 2) {
  296. Vector3 displace = (hunterToMe2).normalized * minDistance;
  297. myPos = hunterPos + displace;
  298. zPathPoints.Enqueue(myPos);
  299. }
  300. canCreateZPath = false;
  301. break;
  302. } else {
  303. zPathPoints.Enqueue(myPos);
  304. }
  305. }
  306. }
  307. //停留
  308. void Stay() {
  309. if (state.moving) {
  310. this.agent.destination = this.agent.nextPosition;
  311. }
  312. state.ResetActionState();
  313. state.staying = true;
  314. }
  315. //动画播放
  316. int curAnimIndex = 0;
  317. void playAniStay() {
  318. ap.play(curAnimIndex = 3, WrapMode.Loop);
  319. }
  320. bool isAniStay() {
  321. return curAnimIndex == 3;
  322. }
  323. void playAniMoveSlowly() {
  324. ap.play(curAnimIndex = 5, WrapMode.Loop);
  325. }
  326. bool isAniMoveSlowly() {
  327. return curAnimIndex == 5;
  328. }
  329. void playAniMoveQuickly() {
  330. ap.play(curAnimIndex = 4, WrapMode.Loop);
  331. }
  332. bool isAniMoveQuickly() {
  333. return curAnimIndex == 4;
  334. }
  335. void playAniAttakA() {
  336. ap.play(curAnimIndex = 1, WrapMode.Once);
  337. }
  338. bool isAniAttakA() {
  339. return curAnimIndex == 1;
  340. }
  341. void stopAniAttackA() {
  342. ap.StopAnimation(1);
  343. }
  344. void playAniAttakB() {
  345. ap.play(curAnimIndex = 0, WrapMode.Once);
  346. }
  347. bool isAniAttakB() {
  348. return curAnimIndex == 0;
  349. }
  350. void playAniDie() {
  351. ap.play(curAnimIndex = 2, WrapMode.Once);
  352. }
  353. bool isAniDie() {
  354. return curAnimIndex == 2;
  355. }
  356. void initAniListener() {
  357. this.ap.completeCallback = delegate(AnimationPlayerCompleteResult res) {
  358. if (res.index == 1 || res.index == 0) {
  359. this.state.ResetActionState();
  360. }
  361. };
  362. }
  363. //帧更新逻辑-通过状态更新动作动画
  364. void UpdateAction() {
  365. if (state.staying) {
  366. state.stayingTime += Time.deltaTime;
  367. if (!isAniStay()) playAniStay();
  368. }
  369. else if (state.moving) {
  370. state.movingTime += Time.deltaTime;
  371. if (state.moveSlowly && !isAniMoveSlowly()) playAniMoveSlowly();
  372. if (state.moveQuickly && !isAniMoveQuickly()) playAniMoveQuickly();
  373. }
  374. else if (state.attacking) {
  375. if (state.attackA && !isAniAttakA()) playAniAttakA();
  376. if (state.attackB && !isAniAttakB()) playAniAttakB();
  377. }
  378. else if (state.dead) {
  379. if (!isAniDie()) playAniDie();
  380. }
  381. }
  382. //状态
  383. [System.Serializable]
  384. public class State {
  385. //数值区
  386. public int hp = 1;
  387. //动作区
  388. public bool staying = true;
  389. public bool moving = false;
  390. public bool moveSlowly = false; //慢走
  391. public bool moveQuickly = false; //跑动
  392. public bool attacking = false;
  393. public bool attackA = false; //扑击
  394. public bool attackB = false; //撕咬
  395. public bool dead = false;
  396. public float stayingTime = 0;
  397. public float movingTime = 0;
  398. //特定区
  399. public bool lockingTarget = false; //锁定目标
  400. //重置动作区状态
  401. public void ResetActionState() {
  402. this.staying = false;
  403. this.moving = false;
  404. this.moveSlowly = false;
  405. this.moveQuickly = false;
  406. this.attacking = false;
  407. this.attackA = false;
  408. this.attackB = false;
  409. this.dead = false;
  410. this.stayingTime = 0;
  411. this.movingTime = 0;
  412. }
  413. }
  414. [SerializeField] public State state = new State();
  415. //委托
  416. public System.Action<Wolf> onDie;
  417. public System.Action<Wolf, int> onAttack;
  418. }