ArrowSync.cs 10 KB

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