Wolf.cs 13 KB

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