ArrowSync.cs 9.6 KB

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