Rabbit.cs 11 KB

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