ArrowSync.cs 11 KB

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