ArrowSync.cs 11 KB

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