ArrowSync.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. /* 联机镜像-箭 */
  6. public class ArrowSync : MonoBehaviour
  7. {
  8. void Awake()
  9. {
  10. //箭模型平时属于ArmBow层,因为ArmBow层在飞行镜头中不被渲染,所以箭出去后要切换layer
  11. this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  12. }
  13. void Update()
  14. {
  15. if (!hasAttachAnything) {
  16. UpdatePosAndRot(Time.deltaTime * 15);
  17. }
  18. }
  19. void UpdatePosAndRot(float t) {
  20. transform.position = Vector3.Lerp(transform.position, position, t);
  21. Head().position = transform.position;
  22. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
  23. }
  24. void UpdatePosAndRotImmediate() {
  25. transform.position = position;
  26. Head().position = transform.position;
  27. transform.rotation = rotation;
  28. }
  29. public Transform Head() {
  30. return transform.Find("Head");
  31. }
  32. #region ---------箭矢特效---------
  33. public void activeEffectCyclone(bool value)
  34. {
  35. if (isHit) {
  36. //加强校验,怕ArrowSync关了该特效,又被ArrowCameraTemplate打开这个特效
  37. value = false;
  38. }
  39. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  40. if (!value) return;
  41. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  42. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  43. DOTween.To(() => ps.minParticleSize, value => {
  44. ps.minParticleSize = value;
  45. ps.maxParticleSize = value;
  46. }, 0.4f, 0.6f);
  47. DOTween.To(() => ps1.minParticleSize, value => {
  48. ps1.minParticleSize = value;
  49. ps1.maxParticleSize = value;
  50. }, 0.8f, 0.6f);
  51. }
  52. void activeEffectBomb(bool value)
  53. {
  54. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  55. }
  56. void activeEffectTrail(bool value)
  57. {
  58. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  59. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  60. }
  61. #endregion
  62. Quaternion rotation;
  63. Vector3 position;
  64. float mySpeed;
  65. bool hasPlayHitAudio = false;
  66. bool hasPlayHitEffect = false;
  67. string[] hitTargetAnimalInfo;
  68. bool needCheckHitAnimal = false;//需要检测是否击中了动物?
  69. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  70. ArrowCamera arrowCameraComp;
  71. [System.NonSerialized] public bool isHit;
  72. [System.NonSerialized] public bool hasDoneNextShoot;
  73. [System.NonSerialized] public bool canUseSideCamera;
  74. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  75. public void SetSyncData(SyncData syncData, bool apply = false) {
  76. rotation.x = syncData.rx;
  77. rotation.y = syncData.ry;
  78. rotation.z = syncData.rz;
  79. rotation.w = syncData.rw;
  80. position.x = syncData.px;
  81. position.y = syncData.py;
  82. position.z = syncData.pz;
  83. canUseSideCamera = syncData.cs;
  84. isHit = syncData.ih;
  85. hasDoneNextShoot = syncData.dns;
  86. hitTargetAnimalInfo = syncData.htai;
  87. mySpeed = syncData.sp;
  88. if (apply) {
  89. transform.position = position;
  90. transform.rotation = rotation;
  91. if (!isHit || !hasDoneNextShoot) {
  92. needCheckHitAnimal = true;
  93. //激活镜头
  94. Transform cameraTF = this.transform.Find("Camera");
  95. cameraTF.gameObject.SetActive(true);
  96. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  97. arrowCameraComp.SetArrowSync(this);
  98. //射出的声音
  99. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  100. //拖尾
  101. activeEffectTrail(true);
  102. }
  103. else if (isHit) {
  104. hasPlayHitAudio = true;
  105. hasPlayHitEffect = true;
  106. activeEffectCyclone(false);
  107. activeEffectTrail(false);
  108. }
  109. } else {
  110. if (arrowCameraComp && isHit && needCheckHitAnimal) {
  111. needCheckHitAnimal = false;
  112. if (arrowCameraComp.arrowCameraTemplate == null) {
  113. isHit = false;
  114. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  115. bool hasBlockByTree = false;
  116. if (syncData.HasArrowCameraTemplate3()) {
  117. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  118. }
  119. // if (hitTargetAnimalInfo != null) {
  120. // //因为击中的是动物,这里就使用射中时第一帧的数据,可避免跟主机的表现产生差异
  121. // position = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[4]);
  122. // rotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[5]);
  123. // }
  124. UpdatePosAndRotImmediate();
  125. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  126. if (hitTargetAnimalInfo != null) {
  127. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  128. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  129. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  130. foreach (var item in targetAnimals) {
  131. if (item.GetOnlineID() == onlineID) {
  132. //因为击中的是动物,箭挂在动物身上,之后不需要用网络数据来同步表现,而且这样表现力更好
  133. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  134. transform.localPosition = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[2]);
  135. transform.localRotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[3]);
  136. hasAttachAnything = true;
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. if (syncData.HasArrowCameraTemplate3()) {
  144. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  145. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  146. }
  147. }
  148. }
  149. if (isHit && !hasPlayHitAudio) {
  150. hasPlayHitAudio = true;
  151. int hitType = syncData.ht;
  152. if (hitType == Arrow.HitType.TargetInRing || hitType == Arrow.HitType.TargetOutRing) {
  153. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  154. //AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  155. } else if (hitType == Arrow.HitType.Animal) {
  156. AudioMgr.ins.PlayArrowEnter();
  157. } else if (hitType == Arrow.HitType.NotTarget) {
  158. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  159. {
  160. AudioMgr.ins.PlayCheer(false);
  161. }
  162. else
  163. {
  164. AudioMgr.ins.PlayArrowEnter();
  165. }
  166. }
  167. }
  168. if (isHit && !hasPlayHitEffect) {
  169. hasPlayHitEffect = true;
  170. activeEffectBomb(true);
  171. activeEffectCyclone(false);
  172. activeEffectTrail(false);
  173. }
  174. }
  175. public class SyncData {
  176. [System.NonSerialized] public bool inited;
  177. static int autoID;
  178. public int id;
  179. public float rx;
  180. public float ry;
  181. public float rz;
  182. public float rw;
  183. public float px;
  184. public float py;
  185. public float pz;
  186. public bool cs;
  187. public bool ih;
  188. public int ht;
  189. public float sp;
  190. public bool dns; //hasDoneNextShoot
  191. public string act3; // ArrowCameraTemplate3
  192. public string[] htai;
  193. public void SetData(Arrow arrow) {
  194. Quaternion r = arrow.transform.rotation;
  195. Vector3 p = arrow.Head().transform.position;
  196. rx = r.x;
  197. ry = r.y;
  198. rz = r.z;
  199. rw = r.w;
  200. px = p.x;
  201. py = p.y;
  202. pz = p.z;
  203. cs = arrow.canUseSideCamera;
  204. ih = arrow.isHit;
  205. ht = arrow.hitType;
  206. sp = arrow.mySpeed;
  207. dns = arrow.hasDoneNextShoot;
  208. htai = arrow.hitTargetAnimalInfo;
  209. if (!inited) {
  210. id = autoID++;
  211. if (autoID > 1000) autoID = 0;
  212. }
  213. inited = true;
  214. }
  215. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  216. act3 = hasBlockByTree + "," + quicklyNextShoot;
  217. }
  218. public bool HasArrowCameraTemplate3() {
  219. return act3 != null;
  220. }
  221. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  222. string[] strs = act3.Split(',');
  223. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  224. }
  225. }
  226. }