Wolf.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. //血量
  12. [System.NonSerialized] public int hp = 5;
  13. //猎人的变换组件
  14. Transform hunterT {
  15. get {
  16. return ArmBow.ins.transform;
  17. }
  18. }
  19. [SerializeField] Material[] materials;
  20. public void ChangeColorByType(int type) {
  21. GetComponentInChildren<SkinnedMeshRenderer>().material = materials[type - 1];
  22. }
  23. void Awake()
  24. {
  25. ap = GetComponent<AnimationPlayer>();
  26. agent = GetComponent<NavMeshAgent>();
  27. ap.completeCallback = onAniComplete;
  28. }
  29. void Start()
  30. {
  31. //不同个体动画播放速度差异化
  32. ap.speed = Random.value * 0.2f + 0.8f;
  33. //初始动画
  34. playAniForIdle();
  35. //初始化自动策略
  36. InitAutoStrategy();
  37. }
  38. void Update()
  39. {
  40. //寻路过程监测
  41. if (HasCloseToDestination()) {
  42. OnReachDestination();
  43. } else {
  44. OnMovingToDestination();
  45. }
  46. UpdateAction();
  47. UpdateActionTime();
  48. UpdateAutoStrategy();
  49. //输入监听
  50. if (Input.GetKeyDown(KeyCode.D)) {
  51. RandomMove();
  52. }
  53. }
  54. public void Disappear() {
  55. Destroy(this.gameObject);
  56. }
  57. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  58. {
  59. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  60. arrow.Hit();
  61. if (partName == "Leg") hp -= 2;
  62. else if (partName == "Body") hp -= 3;
  63. else if (partName == "Head") hp = 0;
  64. if (hp <= 0) {
  65. Die();
  66. } else {
  67. Hurt();
  68. }
  69. }
  70. void Die() {
  71. if (dead) return;
  72. dead = true;
  73. this.agent.enabled = false;
  74. PuaseAutoStrategy();
  75. onDie?.Invoke(this);
  76. AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.gameObject));
  77. }
  78. void Hurt() {
  79. RunAwayFromHunter();
  80. PuaseAutoStrategy();
  81. addHurtFlag();
  82. AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.gameObject));
  83. }
  84. Queue<int> hurtFlagList = new Queue<int>();
  85. void addHurtFlag() {
  86. hurtFlagList.Enqueue(0);
  87. Invoke("clearOneHurtFlag", 5);
  88. }
  89. void clearOneHurtFlag() {
  90. if (hurtFlagList.Count > 0) {
  91. hurtFlagList.Dequeue();
  92. if (hurtFlagList.Count == 0) {
  93. OnHurtFlagTimeOut();
  94. }
  95. }
  96. }
  97. void OnHurtFlagTimeOut() {
  98. if (!dead) {
  99. ResumeAutoStrategy();
  100. }
  101. }
  102. //是否已经死亡
  103. bool dead = false;
  104. //是否处于移动状态
  105. bool _moving = false;
  106. bool moving {
  107. get {
  108. return _moving;
  109. }
  110. set {
  111. if (_moving && !value) {
  112. movingTime = 0;
  113. }
  114. if (!_moving && value) stayingTime = 0;
  115. _moving = value;
  116. }
  117. }
  118. //实时记录“单次移动的持续时间”
  119. float movingTime = 0;
  120. //实时停留时间
  121. float stayingTime = 0;
  122. //启动寻路
  123. void SetDestination(Vector3 pos) {
  124. moving = true;
  125. this.agent.destination = pos;
  126. }
  127. //寻路过程
  128. void OnMovingToDestination() {
  129. if (!moving) return;
  130. }
  131. //寻路结束
  132. void OnReachDestination() {
  133. if (!moving) return;
  134. moving = false;
  135. }
  136. //是否已经接近目的地(寻路完成判断)
  137. bool HasCloseToDestination() {
  138. if (!moving) return true;
  139. if (movingTime > 0.1) {
  140. if (this.agent.velocity.magnitude < 0.05f) {
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. //更新动作
  147. void UpdateAction() {
  148. if (dead) {
  149. if (!isAniDie()) playAniDie();
  150. } else if (moving) {
  151. if (!isAniRun()) playAniRun();
  152. } else {
  153. if (!IsAniForIdle()) {
  154. playAniForIdle();
  155. }
  156. }
  157. }
  158. //记录动作时间
  159. void UpdateActionTime() {
  160. if (moving) {
  161. movingTime += Time.deltaTime;
  162. } else {
  163. stayingTime += Time.deltaTime;
  164. }
  165. }
  166. float willStayTime;
  167. float willMoveTime;
  168. bool autoMoving = false;
  169. float autoMovingTime;
  170. bool autoStrategy = true;
  171. //自动策略的更新逻辑
  172. void UpdateAutoStrategy() {
  173. if (!autoStrategy) return;
  174. if (stayingTime > willStayTime) {
  175. autoMoving = true;
  176. RandomMove();
  177. RandomWillStayTime();
  178. }
  179. if (autoMoving) {
  180. if (moving) {
  181. autoMovingTime += Time.deltaTime;
  182. } else if (autoMovingTime < willMoveTime) {
  183. RandomMove();
  184. } else if (autoMovingTime >= willMoveTime) {
  185. Stay();
  186. RandomWillMoveTime();
  187. autoMoving = false;
  188. autoMovingTime = 0;
  189. }
  190. }
  191. }
  192. void InitAutoStrategy() {
  193. autoStrategy = true;
  194. this.willStayTime = 1 + Random.value * 4;
  195. RandomWillMoveTime();
  196. }
  197. void PuaseAutoStrategy() {
  198. this.autoStrategy = false;
  199. }
  200. void ResumeAutoStrategy() {
  201. this.autoStrategy = true;
  202. RandomWillStayTime();
  203. RandomWillMoveTime();
  204. this.autoMoving = false;
  205. this.autoMovingTime = 0;
  206. }
  207. void RandomWillStayTime() {
  208. this.willStayTime = Random.value * 3 + 5;
  209. }
  210. void RandomWillMoveTime() {
  211. this.willMoveTime = Random.value * 2 + 3;
  212. }
  213. //逃跑远离猎人
  214. void RunAwayFromHunter()
  215. {
  216. Vector3 backVec = GetPointerHunterToMe();
  217. SetDestination(transform.position + backVec.normalized * 5);
  218. }
  219. //获取猎人到我的位移向量
  220. Vector3 GetPointerHunterToMe() {
  221. Vector3 hunterPos = hunterT.position;
  222. Vector3 myPos = transform.position;
  223. hunterPos.y = myPos.y;
  224. return myPos - hunterPos;
  225. }
  226. //随机移动
  227. void RandomMove() {
  228. Vector3 newDir = Quaternion.AngleAxis(Random.value * 180, Vector3.up) * this.transform.parent.forward;
  229. Vector3 newPos = this.transform.parent.position + newDir.normalized * 10 * Random.value;
  230. SetDestination(newPos);
  231. }
  232. //停留
  233. void Stay() {
  234. if (moving) {
  235. this.agent.destination = this.agent.nextPosition;
  236. OnReachDestination();
  237. }
  238. }
  239. //当前动画索引
  240. int curAnimIndex = 0;
  241. void playAniDie() {
  242. ap.play(curAnimIndex = 2, WrapMode.Once);
  243. }
  244. bool isAniDie() {
  245. return curAnimIndex == 2;
  246. }
  247. void playAniRun() {
  248. ap.play(curAnimIndex = 4, WrapMode.Loop);
  249. }
  250. bool isAniRun() {
  251. return curAnimIndex == 4;
  252. }
  253. void onAniComplete(AnimationPlayerCompleteResult res) {
  254. if (res.index == 2) {
  255. Invoke("Disappear", 2);
  256. }
  257. }
  258. bool IsAniForIdle() {
  259. return curAnimIndex == 3;
  260. }
  261. void playAniForIdle() {
  262. curAnimIndex = 3;
  263. ap.play(curAnimIndex, WrapMode.Loop);
  264. }
  265. //委托
  266. public System.Action<Wolf> onDie;
  267. }