Wolf.cs 11 KB

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