Rabbit.cs 11 KB

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