Wolf.cs 11 KB

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