ArrowSync.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. UpdatePosAndRot(Time.deltaTime * 15);
  14. }
  15. void UpdatePosAndRot(float t) {
  16. transform.position = Vector3.Lerp(transform.position, position, t);
  17. Head().position = transform.position;
  18. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
  19. }
  20. public Transform Head() {
  21. return transform.Find("Head");
  22. }
  23. #region ---------箭矢特效---------
  24. public void activeEffectCyclone(bool value)
  25. {
  26. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  27. if (!value) return;
  28. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  29. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  30. DOTween.To(() => ps.minParticleSize, value => {
  31. ps.minParticleSize = value;
  32. ps.maxParticleSize = value;
  33. }, 0.4f, 0.6f);
  34. DOTween.To(() => ps1.minParticleSize, value => {
  35. ps1.minParticleSize = value;
  36. ps1.maxParticleSize = value;
  37. }, 0.8f, 0.6f);
  38. }
  39. void activeEffectBomb(bool value)
  40. {
  41. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  42. }
  43. void activeEffectTrail(bool value)
  44. {
  45. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  46. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  47. }
  48. #endregion
  49. Quaternion rotation;
  50. Vector3 position;
  51. float mySpeed;
  52. bool hasPlayHitAudio = false;
  53. bool hasPlayHitEffect = false;
  54. ArrowCamera arrowCameraComp;
  55. [System.NonSerialized] public bool isHit;
  56. [System.NonSerialized] public bool hasDoneNextShoot;
  57. [System.NonSerialized] public bool canUseSideCamera;
  58. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  59. public void SetSyncData(SyncData syncData, bool apply = false) {
  60. rotation.x = syncData.rx;
  61. rotation.y = syncData.ry;
  62. rotation.z = syncData.rz;
  63. rotation.w = syncData.rw;
  64. position.x = syncData.px;
  65. position.y = syncData.py;
  66. position.z = syncData.pz;
  67. canUseSideCamera = syncData.cs;
  68. bool oldIsHit = isHit;
  69. isHit = syncData.ih;
  70. hasDoneNextShoot = syncData.dns;
  71. mySpeed = syncData.sp;
  72. if (apply) {
  73. transform.position = position;
  74. transform.rotation = rotation;
  75. if (!isHit || !hasDoneNextShoot) {
  76. //激活镜头
  77. Transform cameraTF = this.transform.Find("Camera");
  78. cameraTF.gameObject.SetActive(true);
  79. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  80. arrowCameraComp.SetArrowSync(this);
  81. //射出的声音
  82. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  83. //拖尾
  84. activeEffectTrail(true);
  85. }
  86. else if (isHit) {
  87. hasPlayHitAudio = true;
  88. hasPlayHitEffect = true;
  89. activeEffectCyclone(false);
  90. activeEffectTrail(false);
  91. }
  92. } else {
  93. if (!oldIsHit && isHit) {
  94. if (arrowCameraComp) {
  95. if (arrowCameraComp.arrowCameraTemplate == null) {
  96. isHit = false;
  97. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  98. bool hasBlockByTree = false;
  99. if (syncData.HasArrowCameraTemplate3()) {
  100. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  101. }
  102. UpdatePosAndRot(1f);
  103. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  104. }
  105. }
  106. }
  107. if (syncData.HasArrowCameraTemplate3()) {
  108. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  109. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  110. }
  111. }
  112. }
  113. if (isHit && !hasPlayHitAudio) {
  114. hasPlayHitAudio = true;
  115. int hitType = syncData.ht;
  116. if (hitType == 1 || hitType == 2) {
  117. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  118. AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  119. } else if (hitType == 4 || hitType == 5) {
  120. AudioMgr.ins.PlayArrowEnter();
  121. } else if (hitType == 3) {
  122. AudioMgr.ins.PlayCheer(false);
  123. }
  124. }
  125. if (isHit && !hasPlayHitEffect) {
  126. hasPlayHitEffect = true;
  127. activeEffectBomb(true);
  128. activeEffectCyclone(false);
  129. activeEffectTrail(false);
  130. }
  131. }
  132. public class SyncData {
  133. [System.NonSerialized] public bool inited;
  134. static int autoID;
  135. public int id;
  136. public float rx;
  137. public float ry;
  138. public float rz;
  139. public float rw;
  140. public float px;
  141. public float py;
  142. public float pz;
  143. public bool cs;
  144. public bool ih;
  145. public int ht;
  146. public float sp;
  147. public bool dns; //hasDoneNextShoot
  148. public string act3; // ArrowCameraTemplate3
  149. public void SetData(Arrow arrow) {
  150. Quaternion r = arrow.transform.rotation;
  151. Vector3 p = arrow.Head().transform.position;
  152. rx = r.x;
  153. ry = r.y;
  154. rz = r.z;
  155. rw = r.w;
  156. px = p.x;
  157. py = p.y;
  158. pz = p.z;
  159. cs = arrow.canUseSideCamera;
  160. ih = arrow.isHit;
  161. ht = arrow.hitType;
  162. sp = arrow.mySpeed;
  163. dns = arrow.hasDoneNextShoot;
  164. if (!inited) {
  165. id = autoID++;
  166. if (autoID > 1000) autoID = 0;
  167. }
  168. inited = true;
  169. }
  170. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  171. act3 = hasBlockByTree + "," + quicklyNextShoot;
  172. }
  173. public bool HasArrowCameraTemplate3() {
  174. return act3 != null;
  175. }
  176. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  177. string[] strs = act3.Split(',');
  178. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  179. }
  180. }
  181. }