Wolf.cs 7.2 KB

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