Rabbit.cs 7.7 KB

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