Rabbit.cs 11 KB

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