ArrowSync.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. public Transform Head() {
  23. return transform.Find("Head");
  24. }
  25. #region ---------箭矢特效---------
  26. public void activeEffectCyclone(bool value)
  27. {
  28. if (isHit) {
  29. //加强校验,怕ArrowSync关了该特效,又被ArrowCameraTemplate打开这个特效
  30. value = false;
  31. }
  32. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  33. if (!value) return;
  34. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  35. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  36. DOTween.To(() => ps.minParticleSize, value => {
  37. ps.minParticleSize = value;
  38. ps.maxParticleSize = value;
  39. }, 0.4f, 0.6f);
  40. DOTween.To(() => ps1.minParticleSize, value => {
  41. ps1.minParticleSize = value;
  42. ps1.maxParticleSize = value;
  43. }, 0.8f, 0.6f);
  44. }
  45. void activeEffectBomb(bool value)
  46. {
  47. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  48. }
  49. void activeEffectTrail(bool value)
  50. {
  51. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  52. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  53. }
  54. #endregion
  55. Quaternion rotation;
  56. Vector3 position;
  57. float mySpeed;
  58. bool hasPlayHitAudio = false;
  59. bool hasPlayHitEffect = false;
  60. string[] hitTargetAnimalInfo;
  61. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  62. ArrowCamera arrowCameraComp;
  63. [System.NonSerialized] public bool isHit;
  64. [System.NonSerialized] public bool hasDoneNextShoot;
  65. [System.NonSerialized] public bool canUseSideCamera;
  66. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  67. public void SetSyncData(SyncData syncData, bool apply = false) {
  68. rotation.x = syncData.rx;
  69. rotation.y = syncData.ry;
  70. rotation.z = syncData.rz;
  71. rotation.w = syncData.rw;
  72. position.x = syncData.px;
  73. position.y = syncData.py;
  74. position.z = syncData.pz;
  75. canUseSideCamera = syncData.cs;
  76. bool oldIsHit = isHit;
  77. isHit = syncData.ih;
  78. hasDoneNextShoot = syncData.dns;
  79. hitTargetAnimalInfo = syncData.htai;
  80. mySpeed = syncData.sp;
  81. if (apply) {
  82. transform.position = position;
  83. transform.rotation = rotation;
  84. if (!isHit || !hasDoneNextShoot) {
  85. //激活镜头
  86. Transform cameraTF = this.transform.Find("Camera");
  87. cameraTF.gameObject.SetActive(true);
  88. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  89. arrowCameraComp.SetArrowSync(this);
  90. //射出的声音
  91. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  92. //拖尾
  93. activeEffectTrail(true);
  94. }
  95. else if (isHit) {
  96. hasPlayHitAudio = true;
  97. hasPlayHitEffect = true;
  98. activeEffectCyclone(false);
  99. activeEffectTrail(false);
  100. }
  101. } else {
  102. if (!oldIsHit && isHit) {
  103. if (arrowCameraComp) {
  104. if (arrowCameraComp.arrowCameraTemplate == null) {
  105. isHit = false;
  106. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  107. bool hasBlockByTree = false;
  108. if (syncData.HasArrowCameraTemplate3()) {
  109. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  110. }
  111. UpdatePosAndRot(1f);
  112. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  113. if (hitTargetAnimalInfo != null) {
  114. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  115. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  116. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  117. foreach (var item in targetAnimals) {
  118. if (item.GetOnlineID() == onlineID) {
  119. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  120. hasAttachAnything = true;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. if (syncData.HasArrowCameraTemplate3()) {
  129. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  130. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  131. }
  132. }
  133. }
  134. if (isHit && !hasPlayHitAudio) {
  135. hasPlayHitAudio = true;
  136. int hitType = syncData.ht;
  137. if (hitType == 1 || hitType == 2) {
  138. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  139. AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  140. } else if (hitType == 4 || hitType == 5) {
  141. AudioMgr.ins.PlayArrowEnter();
  142. } else if (hitType == 3) {
  143. AudioMgr.ins.PlayCheer(false);
  144. }
  145. }
  146. if (isHit && !hasPlayHitEffect) {
  147. hasPlayHitEffect = true;
  148. activeEffectBomb(true);
  149. activeEffectCyclone(false);
  150. activeEffectTrail(false);
  151. }
  152. }
  153. public class SyncData {
  154. [System.NonSerialized] public bool inited;
  155. static int autoID;
  156. public int id;
  157. public float rx;
  158. public float ry;
  159. public float rz;
  160. public float rw;
  161. public float px;
  162. public float py;
  163. public float pz;
  164. public bool cs;
  165. public bool ih;
  166. public int ht;
  167. public float sp;
  168. public bool dns; //hasDoneNextShoot
  169. public string act3; // ArrowCameraTemplate3
  170. public string[] htai;
  171. public void SetData(Arrow arrow) {
  172. Quaternion r = arrow.transform.rotation;
  173. Vector3 p = arrow.Head().transform.position;
  174. rx = r.x;
  175. ry = r.y;
  176. rz = r.z;
  177. rw = r.w;
  178. px = p.x;
  179. py = p.y;
  180. pz = p.z;
  181. cs = arrow.canUseSideCamera;
  182. ih = arrow.isHit;
  183. ht = arrow.hitType;
  184. sp = arrow.mySpeed;
  185. dns = arrow.hasDoneNextShoot;
  186. htai = arrow.hitTargetAnimalInfo;
  187. if (!inited) {
  188. id = autoID++;
  189. if (autoID > 1000) autoID = 0;
  190. }
  191. inited = true;
  192. }
  193. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  194. act3 = hasBlockByTree + "," + quicklyNextShoot;
  195. }
  196. public bool HasArrowCameraTemplate3() {
  197. return act3 != null;
  198. }
  199. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  200. string[] strs = act3.Split(',');
  201. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  202. }
  203. }
  204. }