Rabbit.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. [System.NonSerialized] public int colorType = -1;
  15. public void ChangeColorByType(int type) {
  16. colorType = type;
  17. GetComponentInChildren<SkinnedMeshRenderer>().material = materials[type - 1];
  18. }
  19. #region 联机附加部分
  20. public static HashSet<Rabbit> rabbitSet = new HashSet<Rabbit>();
  21. //碰撞体
  22. List<Transform> targetAnimalParts = new List<Transform>();
  23. //识别ID
  24. [System.NonSerialized] public int uid;
  25. //是否为镜像,即只负责渲染,还有作为接收器,接收到外部输入(比如被箭击中)会通知主机
  26. [System.NonSerialized] public bool isMirror;
  27. public RabbitSyncData outputSyncData;
  28. private RabbitSyncData _inputSyncData;
  29. public RabbitSyncData inputSyncData {
  30. get {
  31. return _inputSyncData;
  32. }
  33. set {
  34. _inputSyncData = value;
  35. uid = value.id;
  36. syncRotation.x = value.rx;
  37. syncRotation.y = value.ry;
  38. syncRotation.z = value.rz;
  39. syncRotation.w = value.rw;
  40. syncPosition.x = value.px;
  41. syncPosition.y = value.py;
  42. syncPosition.z = value.pz;
  43. }
  44. }
  45. private Quaternion syncRotation;
  46. private Vector3 syncPosition;
  47. private bool hasUpdateBySyncData = false;
  48. [System.NonSerialized] public bool isInvalid = false; //外部运算用的
  49. [System.NonSerialized] public string onHitData = null;
  50. [System.NonSerialized] public bool onDoNextShootWillDestroy = false;
  51. [System.NonSerialized] public int injuredID = 0;
  52. #endregion
  53. void Awake()
  54. {
  55. rabbitSet.Add(this);
  56. ap = GetComponent<AnimationPlayer>();
  57. agent = GetComponent<NavMeshAgent>();
  58. if (isMirror) {
  59. Destroy(agent);
  60. }
  61. GetTargetAnimalParts(transform, targetAnimalParts);
  62. }
  63. void Start()
  64. {
  65. //不同个体动画播放速度差异化
  66. ap.speed = Random.value * 0.2f + 0.8f;
  67. //初始动画
  68. playAniForIdle();
  69. //初始化自动策略
  70. InitAutoStrategy();
  71. }
  72. void OnDestroy()
  73. {
  74. rabbitSet.Remove(this);
  75. }
  76. void Update()
  77. {
  78. if (isMirror) {
  79. if (inputSyncData == null) return;
  80. if (!hasUpdateBySyncData) {
  81. transform.position = syncPosition;
  82. transform.rotation = syncRotation;
  83. hasUpdateBySyncData = true;
  84. } else {
  85. transform.position = Vector3.Lerp(transform.position, syncPosition, Time.deltaTime * 15);
  86. transform.rotation = Quaternion.Lerp(transform.rotation, syncRotation, Time.deltaTime * 15);
  87. }
  88. if (colorType != inputSyncData.ct) {
  89. ChangeColorByType(inputSyncData.ct);
  90. }
  91. if (inputSyncData.ai != curAnimIndex) {
  92. curAnimIndex = inputSyncData.ai;
  93. if (IsAniForIdle()) {
  94. ap.play(curAnimIndex, WrapMode.Loop);
  95. } else if (isAniJump()) {
  96. playAniJump();
  97. } else if (isAniDie()) {
  98. playAniDie();
  99. }
  100. }
  101. if (inputSyncData.ii != injuredID) {
  102. injuredID = inputSyncData.ii;
  103. AudioMgr.ins.PlayAnimalEffect("rabbit_injured", AudioMgr.GetAudioSource(this.gameObject));
  104. }
  105. return;
  106. }
  107. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  108. if (outputSyncData == null) outputSyncData = new RabbitSyncData();
  109. outputSyncData.SetData(this);
  110. }
  111. //寻路过程监测
  112. if (HasCloseToDestination()) {
  113. OnReachDestination();
  114. } else {
  115. OnMovingToDestination();
  116. }
  117. UpdateAction();
  118. UpdateActionTime();
  119. UpdateAutoStrategy();
  120. }
  121. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  122. {
  123. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  124. arrow.Hit();
  125. if (isMirror) {
  126. onHitData = uid.ToString() + "," + partName;
  127. return;
  128. }
  129. OnHitLogic(arrow, partName);
  130. }
  131. public void OnHitLogic(Arrow arrow, string partName) {
  132. int hurtValue = 0;
  133. if (partName == "Leg" || partName == "Ear") hurtValue = 1;
  134. else if (partName == "Body") hurtValue = 2;
  135. else if (partName == "Head") hurtValue = 3;
  136. hp -= hurtValue;
  137. GameEventCenter.ins.onTargetAnimalHurt?.Invoke(this, hurtValue);
  138. if (hp <= 0) {
  139. Die(arrow);
  140. } else {
  141. Hurt();
  142. }
  143. }
  144. void Die(Arrow arrow) {
  145. if (dead) return;
  146. if (arrow != null) {
  147. arrow.onDoNextShoot += delegate() {
  148. Destroy(this.gameObject);
  149. };
  150. } else {
  151. //需要借住关卡的GameMode来清除
  152. onDoNextShootWillDestroy = true;
  153. }
  154. dead = true;
  155. this.agent.enabled = false;
  156. PuaseAutoStrategy();
  157. onDie?.Invoke(this);
  158. AudioMgr.ins.PlayAnimalEffect("rabbit_injured", AudioMgr.GetAudioSource(this.gameObject));
  159. injuredID++;
  160. }
  161. void Hurt() {
  162. RunAwayFromHunter();
  163. addHurtFlag();
  164. PuaseAutoStrategy();
  165. AudioMgr.ins.PlayAnimalEffect("rabbit_injured", AudioMgr.GetAudioSource(this.gameObject));
  166. injuredID++;
  167. }
  168. JC.CS.CountLocker hurtFlag = new JC.CS.CountLocker();
  169. void addHurtFlag() {
  170. hurtFlag.Lock();
  171. Invoke("clearOneHurtFlag", 5);
  172. }
  173. void clearOneHurtFlag() {
  174. hurtFlag.Unlock();
  175. if (hurtFlag.IsReleased()) {
  176. onHurtFlagTimeout();
  177. }
  178. }
  179. void onHurtFlagTimeout() {
  180. if (!dead) ResumeAutoStrategy();
  181. }
  182. //是否已经死亡
  183. bool dead = false;
  184. //是否处于移动状态
  185. bool _moving = false;
  186. bool moving {
  187. get {
  188. return _moving;
  189. }
  190. set {
  191. if (_moving && !value) {
  192. movingTime = 0;
  193. }
  194. if (!_moving && value) stayingTime = 0;
  195. _moving = value;
  196. }
  197. }
  198. //实时记录“单次移动的持续时间”
  199. float movingTime = 0;
  200. //实时停留时间
  201. float stayingTime = 0;
  202. //启动寻路
  203. void SetDestination(Vector3 pos) {
  204. moving = true;
  205. this.agent.destination = pos;
  206. }
  207. //寻路过程
  208. void OnMovingToDestination() {
  209. if (!moving) return;
  210. }
  211. //寻路结束
  212. void OnReachDestination() {
  213. if (!moving) return;
  214. moving = false;
  215. }
  216. //是否已经接近目的地(寻路完成判断)
  217. bool HasCloseToDestination() {
  218. if (!moving) return true;
  219. if (Vector3.Distance(this.agent.nextPosition, this.agent.destination) < 0.15f) {
  220. return true;
  221. }
  222. if (movingTime > 0.1f && this.agent.velocity.magnitude < 0.05f) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. //更新动作
  228. void UpdateAction() {
  229. if (dead) {
  230. if (!isAniDie()) playAniDie();
  231. } else if (moving) {
  232. if (!isAniJump()) playAniJump();
  233. } else {
  234. if (!IsAniForIdle()) {
  235. playAniForIdle();
  236. }
  237. }
  238. }
  239. //记录动作时间
  240. void UpdateActionTime() {
  241. if (moving) {
  242. movingTime += Time.deltaTime;
  243. } else {
  244. stayingTime += Time.deltaTime;
  245. }
  246. }
  247. float willStayTime;
  248. float willMoveTime;
  249. bool autoMoving = false;
  250. float autoMovingTime;
  251. bool autoStrategy = true;
  252. //自动策略的更新逻辑
  253. void UpdateAutoStrategy() {
  254. if (!autoStrategy) return;
  255. if (stayingTime > willStayTime) {
  256. autoMoving = true;
  257. RandomMove();
  258. RandomWillStayTime();
  259. }
  260. if (autoMoving) {
  261. // if (moving) {
  262. // autoMovingTime += Time.deltaTime;
  263. // } else if (autoMovingTime < willMoveTime) {
  264. // RandomMove();
  265. // } else if (autoMovingTime >= willMoveTime) {
  266. // Stay();
  267. // RandomWillMoveTime();
  268. // autoMoving = false;
  269. // autoMovingTime = 0;
  270. // }
  271. if (!moving) {
  272. Stay();
  273. RandomWillMoveTime();
  274. autoMoving = false;
  275. autoMovingTime = 0;
  276. } else {
  277. autoMovingTime += Time.deltaTime;
  278. }
  279. }
  280. }
  281. void InitAutoStrategy() {
  282. autoStrategy = true;
  283. this.willStayTime = 3 + Random.value * 4;
  284. RandomWillMoveTime();
  285. }
  286. void PuaseAutoStrategy() {
  287. this.autoStrategy = false;
  288. }
  289. void ResumeAutoStrategy() {
  290. this.autoStrategy = true;
  291. RandomWillStayTime();
  292. RandomWillMoveTime();
  293. this.autoMoving = false;
  294. this.autoMovingTime = 0;
  295. }
  296. void RandomWillStayTime() {
  297. this.willStayTime = Random.value * 2 + 10;
  298. }
  299. void RandomWillMoveTime() {
  300. this.willMoveTime = Random.value * 2 + 3;
  301. }
  302. //逃跑远离猎人
  303. void RunAwayFromHunter()
  304. {
  305. Vector3 backVec = GetPointerHunterToMe();
  306. SetDestination(transform.position + backVec.normalized * 5);
  307. }
  308. [System.NonSerialized] public float baseDistance = 10;
  309. [System.NonSerialized] public float baseDistanceMoveRange = 3;
  310. //随机移动
  311. void RandomMove() {
  312. Vector3 standardPointer = animalsBaseT.forward;
  313. Vector3 pointerWithLen = standardPointer.normalized * (baseDistance + Random.value * baseDistanceMoveRange);
  314. pointerWithLen = Quaternion.AngleAxis(Random.Range(-22f, 22f), Vector3.up) * pointerWithLen;
  315. Vector3 newPos = animalsBaseT.position + pointerWithLen;
  316. // GameObject.Find("BBAA").transform.position = newPos;
  317. // GameObject.Find("BBBA").transform.LookAt(GameObject.Find("BBAA").transform);
  318. SetDestination(newPos);
  319. }
  320. //停留
  321. void Stay() {
  322. if (moving) {
  323. this.agent.destination = this.agent.nextPosition;
  324. OnReachDestination();
  325. }
  326. }
  327. //当前动画索引
  328. [System.NonSerialized] public int curAnimIndex = -1;
  329. void playAniEat() {
  330. ap.play(curAnimIndex = 0, WrapMode.Loop);
  331. }
  332. bool isAniEat() {
  333. return curAnimIndex == 0;
  334. }
  335. void playAniDie() {
  336. ap.play(curAnimIndex = 2, WrapMode.Once);
  337. }
  338. bool isAniDie() {
  339. return curAnimIndex == 2;
  340. }
  341. void playAniJump() {
  342. ap.play(curAnimIndex = 7, WrapMode.Loop);
  343. }
  344. bool isAniJump() {
  345. return curAnimIndex == 7;
  346. }
  347. bool IsAniForIdle() {
  348. return curAnimIndex == 0 || curAnimIndex == 4 || curAnimIndex == 5 || curAnimIndex == 6;
  349. }
  350. int[] idleIndexes = {0, 4, 5, 6};
  351. void playAniForIdle() {
  352. curAnimIndex = idleIndexes[Random.Range(0, idleIndexes.Length)];
  353. ap.play(curAnimIndex, WrapMode.Loop);
  354. }
  355. //委托
  356. public System.Action<Rabbit> onDie;
  357. }