ArrowSync.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class ArrowSync : MonoBehaviour
  6. {
  7. void Awake()
  8. {
  9. this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  10. }
  11. void Update()
  12. {
  13. if (!hasAttachAnything) {
  14. UpdatePosAndRot(Time.deltaTime * 15);
  15. }
  16. }
  17. void UpdatePosAndRot(float t) {
  18. transform.position = Vector3.Lerp(transform.position, position, t);
  19. Head().position = transform.position;
  20. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
  21. }
  22. void UpdatePosAndRotImmediate() {
  23. transform.position = position;
  24. Head().position = transform.position;
  25. transform.rotation = rotation;
  26. }
  27. public Transform Head() {
  28. return transform.Find("Head");
  29. }
  30. #region ---------箭矢特效---------
  31. public void activeEffectCyclone(bool value)
  32. {
  33. if (isHit) {
  34. //加强校验,怕ArrowSync关了该特效,又被ArrowCameraTemplate打开这个特效
  35. value = false;
  36. }
  37. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  38. if (!value) return;
  39. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  40. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  41. DOTween.To(() => ps.minParticleSize, value => {
  42. ps.minParticleSize = value;
  43. ps.maxParticleSize = value;
  44. }, 0.4f, 0.6f);
  45. DOTween.To(() => ps1.minParticleSize, value => {
  46. ps1.minParticleSize = value;
  47. ps1.maxParticleSize = value;
  48. }, 0.8f, 0.6f);
  49. }
  50. void activeEffectBomb(bool value)
  51. {
  52. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  53. }
  54. void activeEffectTrail(bool value)
  55. {
  56. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  57. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  58. }
  59. #endregion
  60. Quaternion rotation;
  61. Vector3 position;
  62. float mySpeed;
  63. bool hasPlayHitAudio = false;
  64. bool hasPlayHitEffect = false;
  65. string[] hitTargetAnimalInfo;
  66. bool needCheckHitAnimal = false;//需要检测是否击中了动物?
  67. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  68. ArrowCamera arrowCameraComp;
  69. [System.NonSerialized] public bool isHit;
  70. [System.NonSerialized] public bool hasDoneNextShoot;
  71. [System.NonSerialized] public bool canUseSideCamera;
  72. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  73. public void SetSyncData(SyncData syncData, bool apply = false) {
  74. rotation.x = syncData.rx;
  75. rotation.y = syncData.ry;
  76. rotation.z = syncData.rz;
  77. rotation.w = syncData.rw;
  78. position.x = syncData.px;
  79. position.y = syncData.py;
  80. position.z = syncData.pz;
  81. canUseSideCamera = syncData.cs;
  82. isHit = syncData.ih;
  83. hasDoneNextShoot = syncData.dns;
  84. hitTargetAnimalInfo = syncData.htai;
  85. mySpeed = syncData.sp;
  86. if (apply) {
  87. transform.position = position;
  88. transform.rotation = rotation;
  89. if (!isHit || !hasDoneNextShoot) {
  90. needCheckHitAnimal = true;
  91. //激活镜头
  92. Transform cameraTF = this.transform.Find("Camera");
  93. cameraTF.gameObject.SetActive(true);
  94. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  95. arrowCameraComp.SetArrowSync(this);
  96. //射出的声音
  97. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  98. //拖尾
  99. activeEffectTrail(true);
  100. }
  101. else if (isHit) {
  102. hasPlayHitAudio = true;
  103. hasPlayHitEffect = true;
  104. activeEffectCyclone(false);
  105. activeEffectTrail(false);
  106. }
  107. } else {
  108. if (arrowCameraComp && isHit && needCheckHitAnimal) {
  109. needCheckHitAnimal = false;
  110. if (arrowCameraComp.arrowCameraTemplate == null) {
  111. isHit = false;
  112. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  113. bool hasBlockByTree = false;
  114. if (syncData.HasArrowCameraTemplate3()) {
  115. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  116. }
  117. if (hitTargetAnimalInfo != null) {
  118. //因为击中的是动物,这里就使用射中时第一帧的数据,可避免跟主机的表现产生差异
  119. SyncDataUtil.StrToVec3(hitTargetAnimalInfo[4], position);
  120. SyncDataUtil.StrToQuat(hitTargetAnimalInfo[5], rotation);
  121. }
  122. UpdatePosAndRotImmediate();
  123. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  124. if (hitTargetAnimalInfo != null) {
  125. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  126. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  127. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  128. foreach (var item in targetAnimals) {
  129. if (item.GetOnlineID() == onlineID) {
  130. //因为击中的是动物,箭挂在动物身上,之后不需要用网络数据来同步表现,而且这样表现力更好
  131. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  132. transform.localPosition = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[2]);
  133. transform.localRotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[3]);
  134. hasAttachAnything = true;
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. if (syncData.HasArrowCameraTemplate3()) {
  142. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  143. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  144. }
  145. }
  146. }
  147. if (isHit && !hasPlayHitAudio) {
  148. hasPlayHitAudio = true;
  149. int hitType = syncData.ht;
  150. if (hitType == Arrow.HitType.TargetInRing || hitType == Arrow.HitType.TargetOutRing) {
  151. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  152. //AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  153. } else if (hitType == Arrow.HitType.Animal) {
  154. AudioMgr.ins.PlayArrowEnter();
  155. } else if (hitType == Arrow.HitType.NotTarget) {
  156. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
  157. {
  158. AudioMgr.ins.PlayCheer(false);
  159. }
  160. else
  161. {
  162. AudioMgr.ins.PlayArrowEnter();
  163. }
  164. }
  165. }
  166. if (isHit && !hasPlayHitEffect) {
  167. hasPlayHitEffect = true;
  168. activeEffectBomb(true);
  169. activeEffectCyclone(false);
  170. activeEffectTrail(false);
  171. }
  172. }
  173. public class SyncData {
  174. [System.NonSerialized] public bool inited;
  175. static int autoID;
  176. public int id;
  177. public float rx;
  178. public float ry;
  179. public float rz;
  180. public float rw;
  181. public float px;
  182. public float py;
  183. public float pz;
  184. public bool cs;
  185. public bool ih;
  186. public int ht;
  187. public float sp;
  188. public bool dns; //hasDoneNextShoot
  189. public string act3; // ArrowCameraTemplate3
  190. public string[] htai;
  191. public void SetData(Arrow arrow) {
  192. Quaternion r = arrow.transform.rotation;
  193. Vector3 p = arrow.Head().transform.position;
  194. rx = r.x;
  195. ry = r.y;
  196. rz = r.z;
  197. rw = r.w;
  198. px = p.x;
  199. py = p.y;
  200. pz = p.z;
  201. cs = arrow.canUseSideCamera;
  202. ih = arrow.isHit;
  203. ht = arrow.hitType;
  204. sp = arrow.mySpeed;
  205. dns = arrow.hasDoneNextShoot;
  206. htai = arrow.hitTargetAnimalInfo;
  207. if (!inited) {
  208. id = autoID++;
  209. if (autoID > 1000) autoID = 0;
  210. }
  211. inited = true;
  212. }
  213. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  214. act3 = hasBlockByTree + "," + quicklyNextShoot;
  215. }
  216. public bool HasArrowCameraTemplate3() {
  217. return act3 != null;
  218. }
  219. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  220. string[] strs = act3.Split(',');
  221. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  222. }
  223. }
  224. }