ArrowSync.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
  22. //这里处理应该联网的时候拖尾,跟随有一点误差位置
  23. if (guadiantuowei)
  24. {
  25. Head().position = transform.position + new Vector3(0, 0, 0.78f);//这里之前应该是只是同步父节点的,所以可能有点显示问题
  26. guadiantuowei.localPosition = new Vector3(Head().localPosition.x, Head().localPosition.y, guadiantuowei.localPosition.z);
  27. }
  28. else if (tuowei) {
  29. //旧的特效加个 -0.78f
  30. Head().position = transform.position + new Vector3(0, 0, -0.78f);//这里之前应该是只是同步父节点的,所以可能有点显示问题
  31. tuowei.localPosition = new Vector3(Head().localPosition.x, Head().localPosition.y, tuowei.localPosition.z);
  32. }
  33. }
  34. void UpdatePosAndRotImmediate() {
  35. transform.position = position;
  36. Head().position = transform.position;
  37. transform.rotation = rotation;
  38. }
  39. public Transform Head() {
  40. return transform.Find("Head");
  41. }
  42. #region ---------箭矢特效---------
  43. public void activeEffectCyclone(bool value)
  44. {
  45. if (isHit) {
  46. //加强校验,怕ArrowSync关了该特效,又被ArrowCameraTemplate打开这个特效
  47. value = false;
  48. }
  49. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  50. if (!value) return;
  51. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  52. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  53. DOTween.To(() => ps.minParticleSize, value => {
  54. ps.minParticleSize = value;
  55. ps.maxParticleSize = value;
  56. }, 0.4f, 0.6f);
  57. DOTween.To(() => ps1.minParticleSize, value => {
  58. ps1.minParticleSize = value;
  59. ps1.maxParticleSize = value;
  60. }, 0.8f, 0.6f);
  61. }
  62. void activeEffectBomb(bool value)
  63. {
  64. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  65. }
  66. Transform guadiantuowei;
  67. Transform tuowei;
  68. void activeEffectTrail(bool value)
  69. {
  70. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  71. {
  72. //this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  73. //this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  74. guadiantuowei = this.transform.Find("guadiantuowei");
  75. if (guadiantuowei == null)
  76. {
  77. tuowei = this.transform.Find("EF_tuowei");
  78. tuowei.gameObject.SetActive(value);
  79. //this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  80. tuowei.Find("Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  81. }
  82. else
  83. {
  84. guadiantuowei.gameObject.SetActive(value);
  85. }
  86. }
  87. else
  88. {
  89. StartCoroutine(showTrail(value));
  90. }
  91. }
  92. IEnumerator showTrail(bool value)
  93. {
  94. this.transform.Find("EF_tuowei_bullet").gameObject.SetActive(value);
  95. yield return new WaitForEndOfFrame();
  96. this.transform.Find("EF_tuowei_bullet/GB_flame_FX_1").gameObject.SetActive(true);
  97. }
  98. #endregion
  99. Quaternion rotation;
  100. Vector3 position;
  101. float mySpeed;
  102. public float mySyncSpeed => mySpeed;
  103. bool hasPlayHitAudio = false;
  104. bool hasPlayHitEffect = false;
  105. string[] hitTargetAnimalInfo;
  106. bool needCheckHitAnimal = false;//需要检测是否击中了动物?
  107. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  108. ArrowCamera arrowCameraComp;
  109. [System.NonSerialized] public bool isHit;
  110. [System.NonSerialized] public bool hasDoneNextShoot;
  111. [System.NonSerialized] public bool canUserLockCamera;
  112. [System.NonSerialized] public bool canUseSideCamera;
  113. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  114. public void SetSyncData(SyncData syncData, bool apply = false) {
  115. rotation.x = syncData.rx;
  116. rotation.y = syncData.ry;
  117. rotation.z = syncData.rz;
  118. rotation.w = syncData.rw;
  119. position.x = syncData.px;
  120. position.y = syncData.py;
  121. position.z = syncData.pz;
  122. canUserLockCamera = syncData.cl;
  123. canUseSideCamera = syncData.cs;
  124. isHit = syncData.ih;
  125. hasDoneNextShoot = syncData.dns;
  126. hitTargetAnimalInfo = syncData.htai;
  127. mySpeed = syncData.sp;
  128. if (apply) {
  129. transform.position = position;
  130. transform.rotation = rotation;
  131. if (!isHit || !hasDoneNextShoot) {
  132. needCheckHitAnimal = true;
  133. // Debug.Log("sync data:" + GlobalData.MyDeviceMode);
  134. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  135. {
  136. //激活镜头
  137. Transform cameraTF = this.transform.Find("Camera");
  138. cameraTF.gameObject.SetActive(true);
  139. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  140. arrowCameraComp.SetArrowSync(this);
  141. //射出的声音
  142. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  143. }
  144. //拖尾
  145. activeEffectTrail(true);
  146. }
  147. else if (isHit) {
  148. hasPlayHitAudio = true;
  149. hasPlayHitEffect = true;
  150. activeEffectCyclone(false);
  151. activeEffectTrail(false);
  152. }
  153. } else {
  154. if (arrowCameraComp && isHit && needCheckHitAnimal) {
  155. needCheckHitAnimal = false;
  156. if (arrowCameraComp.arrowCameraTemplate == null) {
  157. needCheckHitAnimal = true;
  158. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  159. bool hasBlockByTree = false;
  160. if (syncData.HasArrowCameraTemplate3()) {
  161. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  162. }
  163. // if (hitTargetAnimalInfo != null) {
  164. // //因为击中的是动物,这里就使用射中时第一帧的数据,可避免跟主机的表现产生差异
  165. // position = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[4]);
  166. // rotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[5]);
  167. // }
  168. UpdatePosAndRotImmediate();
  169. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  170. if (hitTargetAnimalInfo != null) {
  171. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  172. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  173. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  174. foreach (var item in targetAnimals) {
  175. if (item.GetOnlineID() == onlineID) {
  176. //因为击中的是动物,箭挂在动物身上,之后不需要用网络数据来同步表现,而且这样表现力更好
  177. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  178. transform.localPosition = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[2]);
  179. transform.localRotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[3]);
  180. hasAttachAnything = true;
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. if (syncData.HasArrowCameraTemplate3()) {
  188. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate != null && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  189. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  190. }
  191. }
  192. }
  193. if (isHit && !hasPlayHitAudio) {
  194. hasPlayHitAudio = true;
  195. int hitType = syncData.ht;
  196. // Debug.Log("sync data isHit hitType:" + hitType);
  197. if (hitType == Arrow.HitType.TargetInRing || hitType == Arrow.HitType.TargetOutRing) {
  198. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  199. {
  200. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  201. }
  202. else {
  203. AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  204. }
  205. //AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  206. } else if (hitType == Arrow.HitType.Animal) {
  207. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  208. {
  209. AudioMgr.ins.PlayArrowEnter();
  210. }
  211. else
  212. {
  213. AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  214. }
  215. } else if (hitType == Arrow.HitType.NotTarget) {
  216. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  217. {
  218. AudioMgr.ins.PlayCheer(false);
  219. }
  220. else
  221. {
  222. AudioMgr.ins.PlayArrowEnter();
  223. }
  224. }
  225. }
  226. if (isHit && !hasPlayHitEffect) {
  227. hasPlayHitEffect = true;
  228. activeEffectBomb(true);
  229. activeEffectCyclone(false);
  230. activeEffectTrail(false);
  231. }
  232. }
  233. public class SyncData {
  234. [System.NonSerialized] public bool inited;
  235. static int autoID;
  236. public int id;
  237. public float rx;
  238. public float ry;
  239. public float rz;
  240. public float rw;
  241. public float px;
  242. public float py;
  243. public float pz;
  244. public bool cl;
  245. public bool cs;
  246. public bool ih;
  247. public int ht;
  248. public float sp;
  249. public bool dns; //hasDoneNextShoot
  250. public string act3; // ArrowCameraTemplate3
  251. public string[] htai;
  252. public void SetData(Arrow arrow) {
  253. Quaternion r = arrow.transform.rotation;
  254. Vector3 p = arrow.Head().transform.position;
  255. rx = r.x;
  256. ry = r.y;
  257. rz = r.z;
  258. rw = r.w;
  259. px = p.x;
  260. py = p.y;
  261. pz = p.z;
  262. cs = arrow.canUseSideCamera;
  263. cl = arrow.canUserLockCamera;
  264. ih = arrow.isHit;
  265. ht = arrow.hitType;
  266. sp = arrow.mySpeed;
  267. dns = arrow.hasDoneNextShoot;
  268. htai = arrow.hitTargetAnimalInfo;
  269. if (!inited) {
  270. id = autoID++;
  271. if (autoID > 1000) autoID = 0;
  272. }
  273. inited = true;
  274. }
  275. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  276. act3 = hasBlockByTree + "," + quicklyNextShoot;
  277. }
  278. public bool HasArrowCameraTemplate3() {
  279. return act3 != null;
  280. }
  281. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  282. string[] strs = act3.Split(',');
  283. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  284. }
  285. }
  286. }