ArrowSync.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  59. {
  60. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  61. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  62. }
  63. else
  64. {
  65. StartCoroutine(showTrail(value));
  66. }
  67. }
  68. IEnumerator showTrail(bool value)
  69. {
  70. this.transform.Find("EF_tuowei_bullet").gameObject.SetActive(value);
  71. yield return new WaitForEndOfFrame();
  72. this.transform.Find("EF_tuowei_bullet/GB_flame_FX_1").gameObject.SetActive(true);
  73. }
  74. #endregion
  75. Quaternion rotation;
  76. Vector3 position;
  77. float mySpeed;
  78. bool hasPlayHitAudio = false;
  79. bool hasPlayHitEffect = false;
  80. string[] hitTargetAnimalInfo;
  81. bool needCheckHitAnimal = false;//需要检测是否击中了动物?
  82. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  83. ArrowCamera arrowCameraComp;
  84. [System.NonSerialized] public bool isHit;
  85. [System.NonSerialized] public bool hasDoneNextShoot;
  86. [System.NonSerialized] public bool canUseSideCamera;
  87. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  88. public void SetSyncData(SyncData syncData, bool apply = false) {
  89. rotation.x = syncData.rx;
  90. rotation.y = syncData.ry;
  91. rotation.z = syncData.rz;
  92. rotation.w = syncData.rw;
  93. position.x = syncData.px;
  94. position.y = syncData.py;
  95. position.z = syncData.pz;
  96. canUseSideCamera = syncData.cs;
  97. isHit = syncData.ih;
  98. hasDoneNextShoot = syncData.dns;
  99. hitTargetAnimalInfo = syncData.htai;
  100. mySpeed = syncData.sp;
  101. if (apply) {
  102. transform.position = position;
  103. transform.rotation = rotation;
  104. if (!isHit || !hasDoneNextShoot) {
  105. needCheckHitAnimal = true;
  106. // Debug.Log("sync data:" + GlobalData.MyDeviceMode);
  107. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  108. {
  109. //激活镜头
  110. Transform cameraTF = this.transform.Find("Camera");
  111. cameraTF.gameObject.SetActive(true);
  112. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  113. arrowCameraComp.SetArrowSync(this);
  114. //射出的声音
  115. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  116. }
  117. //拖尾
  118. activeEffectTrail(true);
  119. }
  120. else if (isHit) {
  121. hasPlayHitAudio = true;
  122. hasPlayHitEffect = true;
  123. activeEffectCyclone(false);
  124. activeEffectTrail(false);
  125. }
  126. } else {
  127. if (arrowCameraComp && isHit && needCheckHitAnimal) {
  128. needCheckHitAnimal = false;
  129. if (arrowCameraComp.arrowCameraTemplate == null) {
  130. needCheckHitAnimal = true;
  131. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  132. bool hasBlockByTree = false;
  133. if (syncData.HasArrowCameraTemplate3()) {
  134. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  135. }
  136. // if (hitTargetAnimalInfo != null) {
  137. // //因为击中的是动物,这里就使用射中时第一帧的数据,可避免跟主机的表现产生差异
  138. // position = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[4]);
  139. // rotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[5]);
  140. // }
  141. UpdatePosAndRotImmediate();
  142. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  143. if (hitTargetAnimalInfo != null) {
  144. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  145. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  146. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  147. foreach (var item in targetAnimals) {
  148. if (item.GetOnlineID() == onlineID) {
  149. //因为击中的是动物,箭挂在动物身上,之后不需要用网络数据来同步表现,而且这样表现力更好
  150. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  151. transform.localPosition = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[2]);
  152. transform.localRotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[3]);
  153. hasAttachAnything = true;
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if (syncData.HasArrowCameraTemplate3()) {
  161. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate != null && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  162. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  163. }
  164. }
  165. }
  166. if (isHit && !hasPlayHitAudio) {
  167. hasPlayHitAudio = true;
  168. int hitType = syncData.ht;
  169. // Debug.Log("sync data isHit hitType:" + hitType);
  170. if (hitType == Arrow.HitType.TargetInRing || hitType == Arrow.HitType.TargetOutRing) {
  171. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  172. {
  173. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  174. }
  175. else {
  176. AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  177. }
  178. //AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  179. } else if (hitType == Arrow.HitType.Animal) {
  180. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  181. {
  182. AudioMgr.ins.PlayArrowEnter();
  183. }
  184. else
  185. {
  186. AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  187. }
  188. } else if (hitType == Arrow.HitType.NotTarget) {
  189. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  190. {
  191. AudioMgr.ins.PlayCheer(false);
  192. }
  193. else
  194. {
  195. AudioMgr.ins.PlayArrowEnter();
  196. }
  197. }
  198. }
  199. if (isHit && !hasPlayHitEffect) {
  200. hasPlayHitEffect = true;
  201. activeEffectBomb(true);
  202. activeEffectCyclone(false);
  203. activeEffectTrail(false);
  204. }
  205. }
  206. public class SyncData {
  207. [System.NonSerialized] public bool inited;
  208. static int autoID;
  209. public int id;
  210. public float rx;
  211. public float ry;
  212. public float rz;
  213. public float rw;
  214. public float px;
  215. public float py;
  216. public float pz;
  217. public bool cs;
  218. public bool ih;
  219. public int ht;
  220. public float sp;
  221. public bool dns; //hasDoneNextShoot
  222. public string act3; // ArrowCameraTemplate3
  223. public string[] htai;
  224. public void SetData(Arrow arrow) {
  225. Quaternion r = arrow.transform.rotation;
  226. Vector3 p = arrow.Head().transform.position;
  227. rx = r.x;
  228. ry = r.y;
  229. rz = r.z;
  230. rw = r.w;
  231. px = p.x;
  232. py = p.y;
  233. pz = p.z;
  234. cs = arrow.canUseSideCamera;
  235. ih = arrow.isHit;
  236. ht = arrow.hitType;
  237. sp = arrow.mySpeed;
  238. dns = arrow.hasDoneNextShoot;
  239. htai = arrow.hitTargetAnimalInfo;
  240. if (!inited) {
  241. id = autoID++;
  242. if (autoID > 1000) autoID = 0;
  243. }
  244. inited = true;
  245. }
  246. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  247. act3 = hasBlockByTree + "," + quicklyNextShoot;
  248. }
  249. public bool HasArrowCameraTemplate3() {
  250. return act3 != null;
  251. }
  252. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  253. string[] strs = act3.Split(',');
  254. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  255. }
  256. }
  257. }