Rabbit.cs 7.8 KB

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